🤔
- 背包是大多类型游戏的必要功能之一,也是游戏设计中比较重要的一个元素。
- 这里是一个简单的背包代码演示,不多介绍背包的具体设计及用户体验。
该背包使用ugui搭建,结构如下图
添加背包物品类AddEquip.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AddEquip : MonoBehaviour {
public GameObject equipPrefab;
Transform kanpsack;
public GameObject itemPrefab;
void Awake()
{
kanpsack = GameObject.Find("Knapsack").transform;
}
void Start () {
Sprite[] allSprs = Resources.LoadAll<Sprite> ("");
//循环添加物品
for (int i = 0; i < allSprs.Length; i++)
{
//Sprite sp = Resources.Load<Sprite>(i.ToString());
GameObject g = Instantiate<GameObject>(equipPrefab, transform);
g.GetComponent<Image>().sprite = allSprs[i];
int indx = i;
g.GetComponent<Button>().onClick.AddListener(delegate() { AddEquips(allSprs[indx]); } );
}
}
//物品选择
void AddEquips(Sprite sp)
{
for (int i = 0; i < kanpsack.childCount; i++)
{
Transform cell = kanpsack.GetChild(i);
//上面有物品,
if (cell.childCount > 0)
{
//判断所选的物品是否重复,重复就数量++
if (cell.GetChild(0).GetComponent<Image>().sprite.name == sp.name)
{
Text text = cell.GetChild(0).GetComponentInChildren<Text>();
if(text.text == ""){
text.text = "1";
}
int count = int.Parse(text.text);
count++;
text.text = count.ToString();
return;
}
}
}
//到了这步说明,所选的物品之前没有选取过
for (int i = 0; i < kanpsack.childCount; i++)
{
Transform cell = kanpsack.GetChild(i);
if (cell.childCount ==0)
{
GameObject go = Instantiate<GameObject>(itemPrefab, cell);
go.transform.localPosition = Vector3.zero;
go.GetComponent<Image>().sprite = sp;
go.transform.GetChild(0).gameObject.SetActive(true);
return;
}
}
}
}
背包物品移动类ChangeEquipScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//强制添加组件
[RequireComponent(typeof(CanvasGroup))]
public class ChangeEquipScript : MonoBehaviour,IBeginDragHandler,IEndDragHandler,IDragHandler {
Transform canvas;//Canvas
private Transform originalParent;//拖拽物品的原父物体
private CanvasGroup canvasGroup;
Vector3 dis;//鼠标位置到拖拽物品的距离
void Awake()
{
canvas = GameObject.Find("Canvas").transform;
}
void Start()
{
canvasGroup = GetComponent<CanvasGroup>();
}
//开始拖拽
public void OnBeginDrag(PointerEventData eventData)
{
originalParent = transform.parent;
//使拖拽的物品位于其它UI之上
transform.SetParent(canvas);
dis = transform.position - Input.mousePosition;
//不阻止射线透过当前拖拽的物品,检测下方的UI
canvasGroup.blocksRaycasts = false;
}
//拖拽中
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition + dis;
}
//结束拖拽
public void OnEndDrag(PointerEventData eventData)
{
//获取鼠标进入的游戏物体
Transform obj;
//判断鼠标是否进入UI外
if (eventData.pointerEnter == null)
{
transform.SetParent(originalParent);
transform.localPosition = Vector3.zero;
canvasGroup.blocksRaycasts = true;
return;
}
else
{
obj = eventData.pointerEnter.transform;
}
switch (obj.tag)
{
case "item":
{
//交换两个游戏物品
Transform t = obj.parent;
obj.SetParent(originalParent);
transform.SetParent(t);
obj.localPosition = Vector3.zero;
}
break;
case "cell":
{
//判断Cell上是否有物品
if (obj.childCount == 1)
{
//有装备
Transform child = obj.GetChild(0);
child.SetParent(originalParent);
transform.SetParent(obj);
child.localPosition = Vector3.zero;
}
else
{
//无装备
//直接将拖拽的物品放置在cell上
transform.SetParent(obj);
}
}
break;
default:
{
//移除范围后,重置
transform.SetParent(originalParent);
}
break;
}
transform.localPosition = Vector3.zero;
canvasGroup.blocksRaycasts = true;
}
}