/// <summary> /// 背包数据存档. /// </summary> public void ObjectToJson(List <GameObject> slotsList) { List <InventoryItem> tempList = new List <InventoryItem>(slotsList.Count); // 遍历物品槽数据. for (int i = 0; i < slotsList.Count; ++i) { Transform tempTransform = slotsList[i].GetComponent <Transform>().Find("InventoryItem"); if (tempTransform != null) { InventoryItemController iic = tempTransform.GetComponent <InventoryItemController>(); InventoryItem item = new InventoryItem(iic.ItemId, iic.GetComponent <Image>().sprite.name, iic.ItemNum, iic.ItemBar); tempList.Add(item); } } // 转换为Json数据. string jsonStr = JsonMapper.ToJson(tempList); string jsonPath = Application.persistentDataPath + "/JsonData/InventoryJsonData.txt"; // 更新Json文件. File.Delete(jsonPath); StreamWriter sw = new StreamWriter(jsonPath); sw.Write(jsonStr); sw.Close(); }
/// <summary> /// 初始化复制的物品 /// </summary> /// <param name="go"></param> private void InitNewItem(GameObject go, int num, string tag) { InventoryItemController tempCtrl = go.GetComponent <InventoryItemController>(); RectTransform rect = go.GetComponent <RectTransform>(); rect.localPosition = Vector3.zero; //位置初始化 tempCtrl.InitId(id); //id初始化 tempCtrl.InitName(); //名称初始化,去掉clone tempCtrl.InitNum(num); //数量文本初始化 tempCtrl.GetComponent <CanvasGroup>().blocksRaycasts = true; if (tag == "InVentorySlot") { tempCtrl.ResetSpriteSize(go.GetComponent <RectTransform>(), 85, 85); } else { tempCtrl.ResetSpriteSize(go.GetComponent <RectTransform>(), 70, 62); } }