unity背包
文件下载地址:背包.7z
角色信息类
当该类属性被更改时通知所有需要显示的界面
1 using UnityEngine; 2 using System; 3 4 public enum InfoType 5 { 6 All 7 } 8 9 public class PlayerInfo : MonoBehaviour { 10 11 private static PlayerInfo _instance = null; 12 private string _name; 13 14 #region 封装 15 public string Name 16 { 17 get 18 { 19 return _name; 20 } 21 22 set 23 { 24 _name = value; 25 } 26 } 27 28 public static PlayerInfo Instance 29 { 30 get 31 { 32 return _instance; 33 } 34 35 set 36 { 37 _instance = value; 38 } 39 } 40 #endregion 41 42 public delegate void OnPlayerInfoChangedEvent(InfoType type); 43 public static OnPlayerInfoChangedEvent onPlayerInfoChangedEvent; 44 45 private void Awake() 46 { 47 Instance = this; 48 } 49 50 private void Start() 51 { 52 Init(); 53 } 54 55 private void Init() 56 { 57 Name = "132"; 58 if(onPlayerInfoChangedEvent!=null) 59 onPlayerInfoChangedEvent(InfoType.All); 60 } 61 }
panelTr是字体框的大小
基于左上角的
if 右边足够容纳描述界面
X = 格子X + 格子宽度
if 格子在 第一象限
if 鼠标到底部的高度 大于 描述界面的高度
Y = 鼠标Y
else
Y = 底部到描述界面的高度 + 描述界面偏移
else
if 鼠标到顶部的高度 大于 描述界面的高度
Y = 格子Y + 描述界面的高度 + 描述界面偏移
else
Y = 顶部
else
X = 格子X - 描述界面宽度 - 描述界面偏移
1 public void Move(Vector3 position, Vector2 latticeSize) 2 { 3 Vector2 pos; 4 if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, position, null, out pos)) 5 return; 6 7 //屏幕一半 8 Vector2 canvasHalf = new Vector2(canvasSize.x / 2f, canvasSize.y / 2f); 9 //鼠标坐标 10 Vector2 mousePos = new Vector2(Input.mousePosition.x - canvasHalf.x - deviation, Input.mousePosition.y - canvasHalf.y); 11 //界面大小 12 Vector2 panelSize = new Vector2(panelTr.sizeDelta.x + deviation * 2, panelTr.sizeDelta.y + deviation * 2); 13 //右边和上面可用大小 14 Vector2 availableSize = new Vector2(canvasHalf.x - pos.x - latticeSize.x, canvasSize.y - Input.mousePosition.y); 15 16 if (availableSize.x > panelSize.x)//当右边足够容纳界面 17 SetPanelPosition(pos, canvasHalf, latticeSize, panelSize, mousePos, availableSize, pos.x + latticeSize.x + deviation); 18 else 19 SetPanelPosition(pos, canvasHalf, latticeSize, panelSize, mousePos, availableSize, pos.x - panelTr.sizeDelta.x - deviation); 20 } 21 22 private void SetPanelPosition(Vector2 pos, Vector2 canvasHalf, Vector2 latticeSize, Vector2 panelSize, Vector2 mousePos, Vector2 availableSize, float x) 23 { 24 25 if (pos.y <= 0)//下 26 { 27 if (availableSize.y > panelSize.y)//足够容纳界面 28 { 29 transform.localPosition = new Vector3(x, mousePos.y + panelTr.sizeDelta.y + deviation, 0); 30 } 31 else 32 { 33 34 transform.localPosition = new Vector3(x, canvasHalf.y - deviation, 0); 35 } 36 37 } 38 else//上 39 { 40 if (Input.mousePosition.y > panelSize.y) 41 { 42 transform.localPosition = new Vector3(x, mousePos.y - deviation, 0); 43 } 44 else{ 45 transform.localPosition = new Vector3(x, panelSize.y - canvasHalf.y - deviation, 0); 46 } 47 48 } 49 }
isClick 阻止第二次按时触发的up事件
目标格子物品设置为当前移动物品
if 目标格子是空的
删除原来的格子的物品
else
设置原来格子的物品为目标格子的物品
1 public void Swap(InventoryItemUI UI) 2 { 3 if (!CheckType(UI)) 4 { 5 Clear(); 6 return; 7 } 8 9 InventoryItem tmp = UI.InventoryItem; 10 UI.InventoryItem = itemUI.InventoryItem; 11 UI.InventoryItem.Count = itemUI.InventoryItem.Count; 12 if (tmp== null) 13 { 14 itemUI.InventoryItem = null; 15 } 16 else 17 { 18 itemUI.InventoryItem = tmp; 19 itemUI.InventoryItem.Count = tmp.Count; 20 } 21 onInventorySwap(); 22 isClick = true; 23 Clear(); 24 }
if 当前物品类型 != 格子类型
return false
if 当前物品类型 == 装备类型
if 这是能放任何装备的格子
return true
if 装备类型 != 目标格子所能放的类型
return false
1 private bool CheckType(InventoryItemUI UI) 2 { 3 if (itemUI.InventoryItem.Inventory.KnapsackSaveType != UI.KnapsackSaveType) 4 { 5 return false; 6 } 7 if (itemUI.InventoryItem.Inventory.KnapsackSaveType == KnapsackType.Equip) 8 { 9 if(UI.EquipmentType == EquipType.All) 10 { 11 return true; 12 } 13 if (itemUI.InventoryItem.Inventory.EquipmentType != UI.EquipmentType) 14 { 15 return false; 16 } 17 else 18 { 19 return true; 20 } 21 } 22 return true; 23 }

浙公网安备 33010602011771号