zoukankan      html  css  js  c++  java
  • Unity3D之储存背包界面里面的物品

    1存储:遍历整个背包的格子。将有子物体的格子给储存下来

      public void SaveInventory()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Slot slot in slotList)
            {
                if (slot.transform.childCount > 0)
                {
                    ItemUI itemUI = slot.transform.GetChild(0).GetComponent<ItemUI>();
                    sb.Append(itemUI.Item.ID + ","+itemUI.Amount+"-");
                }
                else
                {
                    sb.Append("0-");
                }
            }
            PlayerPrefs.SetString(this.gameObject.name, sb.ToString());
        }

    2加载:

    public void LoadInventory()
        {
            if (PlayerPrefs.HasKey(this.gameObject.name) == false) return;
            string str = PlayerPrefs.GetString(this.gameObject.name);
            //print(str);
            string[] itemArray = str.Split('-');
            for (int i = 0; i < itemArray.Length-1; i++)
            {
                string itemStr = itemArray[i];
                if (itemStr != "0")
                {
                    //print(itemStr);
                    string[] temp = itemStr.Split(',');
                    int id = int.Parse(temp[0]);
                    Item item = InventoryManager.Instance.GetItemById(id);
                    int amount = int.Parse(temp[1]);
                    for (int j = 0; j < amount; j++)
                    {
                        slotList[i].StoreItem(item);
                    }
                }
            }
        }

  • 相关阅读:
    VS2017常用快捷键
    浅谈JS之setTimeout与setInterval
    你真的了解foreach吗?
    IEnumerable和IEnumerator详解
    Cesium坐标系及坐标转换详解
    NPM常用指令
    ASP.NET Web API 2系列(三):查看WebAPI接口的详细说明及测试接口
    【nginx】安装nginx
    【redis】本地连接服务器的redis教程
    Mysql5.7及以上版本 ONLY_FULL_GROUP_BY报错的解决方法
  • 原文地址:https://www.cnblogs.com/weiqiangwaideshijie/p/6841762.html
Copyright © 2011-2022 走看看