• 黑暗之光 Day2


    1. 鼠标点击UI检测

    UICamera.isOverUI

    2. 鼠标指针管理

     1 public class CussorManager : MonoBehaviour {
     2     public static CussorManager instance;       // 单例
     3 
     4     public Texture2D cursorAttack;              // 存储鼠标贴图
     5     public Texture2D cursorLockTarget;
     6     public Texture2D cursorNormal;
     7     public Texture2D cursorNpcTalk;
     8     public Texture2D cursorPick;
     9 
    10     void Start()
    11     {
    12         instance = this;
    13     }
    14 
    15     public void SetNormal()                     // 设置鼠标为正常
    16     {
    17         Cursor.SetCursor(cursorNormal, Vector2.zero, CursorMode.Auto);
    18     }
    19 
    20     public void SetNpcTalk()                    // 设置鼠标为 Npc_talk
    21     {
    22         Cursor.SetCursor(cursorNpcTalk, Vector2.zero, CursorMode.Auto);
    23     }
    24 }

    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('
    ');                // 以换行符分割字符
     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 = 1)
    {
        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. 物体拖拽实现

     1 protected override void OnDragDropRelease(GameObject surface)
     2     {
     3         print(this.tag);
     4         base.OnDragDropRelease(surface);
     5         if (surface != null)
     6         {
     7             if (surface.tag == Tags.inventoryItemGrid)              
     8             {
     9                 if (surface != this.transform.parent.gameObject)    // 拖拽到空格子
    10                 {
    11                     InventoryItemGrid oldGrid = this.transform.parent.GetComponent<InventoryItemGrid>();
    12                     InventoryItemGrid newGrid = surface.GetComponent<InventoryItemGrid>();
    13                     newGrid.SetId(oldGrid.GetId(), oldGrid.GetNum());
    14                     oldGrid.ClearInfo();
    15                 }
    16             }
    17             else if (surface.tag == Tags.inventoryItem)             // 拖拽到有物体的格子
    18             {
    19                 InventoryItemGrid grid1 = this.transform.parent.GetComponent<InventoryItemGrid>();
    20                 InventoryItemGrid grid2 = surface.transform.parent.GetComponent<InventoryItemGrid>();
    21                 int id = grid1.GetId();
    22                 int num = grid1.GetNum();
    23                 grid1.SetId(grid2.GetId(), grid2.GetNum());
    24                 grid2.SetId(id, num);
    25             }
    26         }
    27 
    28         ResetPos();
    29     }
    30 
    31     void ResetPos()
    32     {
    33         this.transform.localPosition = Vector3.zero;
    34     }
  • 相关阅读:
    生成客户端信任的证书文件
    postgresql Streaming Replication监控与注意事项
    采用pacemaker+corosync实现postgresql双机热备、高可用方案
    51nod1305(简单逻辑)
    51nod1091(贪心)
    51nod1009(1的数目)
    51nod1102(数塔)
    51nod1459(带权值的dijkstra)
    51nod1265(判断四个点是否共面)
    51node1264(判断线段相交)
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/unity3d05.html
Copyright © 2020-2023  润新知