黑暗之光 Day2
1.鼠标点击UI检测
UICamera.isOverUI
2.鼠标指针管理
public class CussorManager : MonoBehaviour {
public static CussorManager instance; // 单例
public Texture2D cursorAttack; // 存储鼠标贴图
public Texture2D cursorLockTarget;
public Texture2D cursorNormal;
public Texture2D cursorNpcTalk;
public Texture2D cursorPick;
void Start()
{
instance = this;
}
public void SetNormal() // 设置鼠标为正常
{
Cursor.SetCursor(cursorNormal, Vector2.zero, CursorMode.Auto);
}
public void SetNpcTalk() // 设置鼠标为 Npc_talk
{
Cursor.SetCursor(cursorNpcTalk, Vector2.zero, CursorMode.Auto);
}
}3.读取物品文件
1 public TextAsset objectsInfoText;
2 private Dictionary<int, ObjectInfo> objectsInfoDict = new Dictionary<int, ObjectInfo>();
3
4 // 从文件中读取物品信息
5 void ReadInfo()
6 {
7 string text = objectsInfoText.text; // 获取文本内容
8 string[] objectsArr = text.Split('\n'); // 以换行符分割字符
9 for (int i = 0; i < objectsArr.Length; ++i) // 遍历每一行
10 {
11 string[] proArr = objectsArr[i].Split(','); // 以逗号分割属性
12 ObjectInfo info = new ObjectInfo(); // 物品属性
13 info.id = int.Parse(proArr[0]);
14 info.name = proArr[1];
15 info.iconName = proArr[2];
16 string type = proArr[3];
17 info.type = ObjectType.Material;
18 switch (type)
19 {
20 case "Drug":
21 info.type = ObjectType.Drug;
22 break;
23 case "Equip":
24 info.type = ObjectType.Equipment;
25 break;
26 case "Mat":
27 info.type = ObjectType.Material;
28 break;
29 }
30 if (info.type == ObjectType.Drug) // 物品是药品
31 {
32 info.hp = int.Parse(proArr[4]);
33 info.mp = int.Parse(proArr[5]);
34 info.sellPrice = int.Parse(proArr[6]);
35 info.buyPrice = int.Parse(proArr[7]);
36 }
37 objectsInfoDict.Add(info.id, info); // 加入字典
38 }
39 }
40
41 // 根据 ID 查找物品信息
42 public ObjectInfo GetObjectInfoById(int id)
43 {
44 ObjectInfo info;
45 objectsInfoDict.TryGetValue(id, out info);
46 return info;
47 }4.物品拖拽
继承UIDragDropItem类。
5.模拟拾取物品放入背包
1 // 根据 id 拾取物品
2 public void GetId(int id)
3 {
4 InventoryItemGrid grid = null;
5 // 查找是否已有该物品
6 foreach (InventoryItemGrid temp in itemGridList)
7 {
8 if (temp.GetId() == id) // 已有
9 {
10 grid = temp;
11 break;
12 }
13 }
14 if (grid != null)
15 {
16 grid.PlusNum(); // 该物品数目+1
17 }
18 else
19 {
20 // 查找是否有空格子
21 foreach (InventoryItemGrid temp in itemGridList)
22 {
23 if (temp.GetId() == 0) // 有空格子
24 {
25 grid = temp; break;
26 }
27 }
28 if (grid != null) // 往空格子里放物品
29 {
30 GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItemPrefab);
31 itemGo.transform.localPosition = Vector3.zero;
32 itemGo.GetComponent<UISprite>().depth = 4;
33 grid.SetId(id); // 改变各自装的物品
34 }
35 }
36 }// 根据 id 更换格子物品
public void SetId(int id, int num = )
{
info = ObjectsInfo.instance.GetObjectInfoById(id); // 根据 id 查找物品
this.id = id;
this.num = num;
InventoryItem item = GetComponentInChildren<InventoryItem>();
item.SetIconName(info.iconName);
numLabel.enabled = true;
numLabel.text = num.ToString();
}6.物体拖拽实现
protected override void OnDragDropRelease(GameObject surface)
{
print(this.tag);
base.OnDragDropRelease(surface);
if (surface != null)
{
if (surface.tag == Tags.inventoryItemGrid)
{
if (surface != this.transform.parent.gameObject) // 拖拽到空格子
{
InventoryItemGrid oldGrid = this.transform.parent.GetComponent<InventoryItemGrid>();
InventoryItemGrid newGrid = surface.GetComponent<InventoryItemGrid>();
newGrid.SetId(oldGrid.GetId(), oldGrid.GetNum());
oldGrid.ClearInfo();
}
}
else if (surface.tag == Tags.inventoryItem) // 拖拽到有物体的格子
{
InventoryItemGrid grid1 = this.transform.parent.GetComponent<InventoryItemGrid>();
InventoryItemGrid grid2 = surface.transform.parent.GetComponent<InventoryItemGrid>();
int id = grid1.GetId();
int num = grid1.GetNum();
grid1.SetId(grid2.GetId(), grid2.GetNum());
grid2.SetId(id, num);
}
}
ResetPos();
}
void ResetPos()
{
this.transform.localPosition = Vector3.zero;
}