Related Posts Plugin for WordPress, Blogger...

5-19 Save And Load Inventory State Data

本章要來儲存背包內狀態,包含背包內的格子、箱子的格子、角色裝備的格子、鍛造的格子,但商店的狀態不用儲存。首先,請在介面上佈置好保存跟載入的按鈕,因為這個步驟沒有很難,就不特地講解了。如果不懂的話,可以先看看之前的文章。
5-6 Knapsack User Interface

接著來修改InventorySystem.cs的程式碼吧,存檔方式很簡單,採用CSV格式,用逗號分隔每個slot的資料。若該Slot有放置物品,則會紀錄物品ID跟數量;若該Slot沒有物品,則會記錄數字0。儲存後的資料大致上會如下圖的形式:

CSV格式的可讀性較差,或者以後有更複雜的資料形式也會更不容易儲存,那大家就可以考慮改成JSON或XML。

以下提供InventorySystem.cs的原始碼,主要新增了兩個方法,SaveInventory跟LoadInventory,使用Unity提供的PlayerPrefs方法儲存資料。
【Unity】遊戲存檔功能(PlayerPrefs)
https://jerrard-liu.blogspot.tw/2015/05/SaveData.html

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Text;

namespace RPG.Inventory{
 public class InventorySystem : MonoBehaviour {

  [SerializeField] ToolTip toolTip;
  [SerializeField] ItemUI pickedItem;
  [SerializeField] CharacterProperty characterProperty;

  public bool isPickedItem = false;
  ItemList itemList;
  ForgeFormulaList forgeFormulaList;
  Slot[] slotList;

  int coinAmount = 100;
  Text coinText;

  void Start () {
   ParseItemsJson ();
   ParseForgeFormulaJson ();
   slotList = GetComponentsInChildren ();
   coinText = GameObject.Find ("Coin").GetComponentInChildren();
   coinText.text = coinAmount.ToString();
   InitialVendorItem ();
  }

  void Update(){
   PressGToAddItem ();
   MovePickedItemByMousePosition ();
   DiscardPickedItem ();
  }

  public void SaveInventory(){
   StringBuilder saveData = new StringBuilder ();
   foreach (Slot slot in slotList) {
    if (slot is VendorSlot) {
     // 如果是商店類型的Slot則不需要儲存狀態,所以儲存為沒有物品的0
     saveData.Append ("0-");
    } else if (slot.transform.childCount > 0) {
     // Slot內有物品,儲存ID跟數量
     ItemUI itemUI = slot.GetComponentInChildren ();
     saveData.Append (itemUI.Item.ID + "," + itemUI.Amount + "-");
    } else {
     // Slot內沒有物品,儲存0
     saveData.Append ("0-");
    }
   }
   PlayerPrefs.SetString (this.gameObject.name, saveData.ToString());
   PlayerPrefs.SetInt ("CoinAmount", coinAmount);
  }

  public void LoadInventory(){
   if (PlayerPrefs.HasKey (this.gameObject.name)) {
    string savaData = PlayerPrefs.GetString (this.gameObject.name);
    print (savaData);
    string[] itemArray = savaData.Split ('-');
    // Length - 1是因為最後一個沒有資料
    for (int i = 0; i < itemArray.Length - 1; i++) {
     if (itemArray [i] != "0") {
      string[] itemInfo = itemArray [i].Split (',');
      // 取得物品的ID跟數量
      int id = int.Parse (itemInfo[0]);
      int amount = int.Parse (itemInfo[1]);
      Item item = GetItemByID (id);
      // 將物品生成在指定的Slot
      for (int j = 0; j < amount; j++) {
       slotList [i].StoreItem (item);
      }
     }
    }
   }
   if (PlayerPrefs.HasKey ("CoinAmount")) {
    coinAmount = PlayerPrefs.GetInt ("CoinAmount");
    coinText.text = coinAmount.ToString ();
   }
  }

  private bool ConsumeCoin(int amount){
   if (coinAmount > 0) {
    coinAmount -= amount;
    coinText.text = coinAmount.ToString ();
    return true;
   }
   return false;
  }

  private void EarnCoin(int amount){
   coinAmount += amount;
   coinText.text = coinAmount.ToString ();
  }

  public void BuyItem(Item item){
   bool isSuccess = ConsumeCoin (item.BuyPrice);
   if (isSuccess) {
    StoreItem (item);
   }
  }

  public void SellItem(int sellAmount){
   int sellPrice = pickedItem.Item.SellPrice * sellAmount;
   ReducePickedItem (sellAmount);
   EarnCoin (sellPrice);
  }

  public void PickupItem(Item item, int amount){
   pickedItem.SetItem (item, amount);
   pickedItem.Show ();
   toolTip.Hide ();
   isPickedItem = true;
  }

  public void ReducePickedItem(int amount){
   pickedItem.ReduceAmount(amount);
   if (pickedItem.Amount <= 0) {
    isPickedItem = false;
    pickedItem.Hide ();
   }
  }

  public void WearingEquipment(ItemUI equipmentToWear){
   if ((equipmentToWear.Item is Equipment) == false) {
    return;
   }

   foreach(Slot slot in slotList){
    EquipmentSlot equipmentSlot = (slot as EquipmentSlot);
    if(equipmentSlot && equipmentSlot.EquipmentTypeIsEqual (equipmentToWear.Item)){
     if (equipmentSlot.transform.childCount > 0) {
      // 人物已配戴裝備了,將裝備與背包的裝備交換
      equipmentSlot.GetComponentInChildren ().ExchangeItem(equipmentToWear);
     } else {
      // 人物未配戴裝備,直接穿上
      equipmentSlot.StoreItem (equipmentToWear.Item);
      // 銷毀背包內的裝備
      Destroy (equipmentToWear.gameObject);
     }
     break;
    }
   }
   UpdatePropertyText ();
  }

  public ItemUI GetPickedItem(){
   return pickedItem;
  }

  public Slot[] GetSlotList(){
   return slotList;
  }

  public void ShowToolTip(string content){
   toolTip.Show (content);
  }

  public void HideToolTip(){
   toolTip.Hide ();
  }

  public void UpdatePropertyText(){
   characterProperty.UpdatePropertyText ();
  }

  public bool StoreItem(int id){
   Item item = GetItemByID (id);
   return StoreItem (item);
  }

  public bool StoreItem(Item item){
   if (item == null) {
    return false;
   }

   // 若物品的儲存容量為1,則將該物品直接放進空的Slot
   if (item.Capacity == 1) {
    return StoreItemInEmptySlot (item);
   }
   // 將相同的Item放在同一個Slot
   return StoreItemInSameSlot(item);
  }

  private bool StoreItemInSameSlot(Item item){
   foreach (Slot slot in slotList) {
    // 避開VendorSlot
    if (slot is VendorSlot) {
     continue;
    }
    if (slot.transform.childCount >= 1 && slot.GetItemID () == item.ID &&
     slot.IsFilled () == false) {
     // 將新Item與同一個Item放在一起
     slot.StoreItem (item);
     return true;
    }
   }
   // 若背包內不存在相同的Item,則放進空的Slot
   return StoreItemInEmptySlot (item);
  }

  private bool StoreItemInEmptySlot(Item item){
   foreach (Slot slot in slotList) {
    // 避開VendorSlot
    if (slot is VendorSlot) {
     continue;
    }
    if (slot.transform.childCount == 0) {
     // 將Item存進該Slot
     slot.StoreItem (item);
     return true;
    }
   }
   Debug.LogError ("No Empty Slot.");
   return false;
  }

  public void ForgeItem(){
   List materialsIDList = new List ();
   List forgeSlotList = new List ();
   // 取得Forge Slot內的Item
   foreach (Slot slot in slotList) {
    ForgeSlot forgeSlot = (slot as ForgeSlot);

    if (forgeSlot && slot.transform.childCount > 0) {
     ItemUI currentItemUI = slot.transform.GetComponentInChildren ();
     for (int i = 0; i < currentItemUI.Amount; i++) {
      materialsIDList.Add (currentItemUI.Item.ID);
      forgeSlotList.Add (forgeSlot);
     }
    } 
   }
   // 檢查item組合是否匹配任一鍛造公式
   foreach (ForgeFormula formula in forgeFormulaList.ForgeFormulaEntityList) {
    if (formula.MatchFormula (materialsIDList)) {
     // 符合公式,存入鍛造物品到背包內
     StoreItem (formula.ResultID);
     // 消耗材料
     ConsumeMaterials(formula, forgeSlotList);
     break;
    }
   }
  }

  private void ConsumeMaterials(ForgeFormula formula, List forgeSlotList){
   List requireList = formula.GetRequireList();
   foreach (int ID in requireList) {
    foreach (ForgeSlot forgeSlot in forgeSlotList) {
     if (forgeSlot.transform.childCount > 0 && forgeSlot.GetItemID() == ID) {
      ItemUI itemUI = forgeSlot.GetComponentInChildren ();
      itemUI.ReduceAmount (1);
      if (itemUI.Amount <= 0) {
       DestroyImmediate (itemUI.gameObject);
      }
      break;
     }
    }
   }
  }

  private void PressGToAddItem(){
   // 測試程式碼,手動生成物品
   if (Input.GetKeyDown (KeyCode.G)) {
    StoreItem (Random.Range(1,18));
   }
  }

  private void InitialVendorItem(){
   Item[] vendorItemList = new Item[10];
   // 手動初始化要放入商店的物品
   vendorItemList [0] = GetItemByID (1);
   vendorItemList [1] = GetItemByID (2);
   vendorItemList [2] = GetItemByID (3);
   vendorItemList [3] = GetItemByID (4);
   vendorItemList [4] = GetItemByID (5);
   vendorItemList [5] = GetItemByID (6);
   vendorItemList [6] = GetItemByID (7);
   vendorItemList [7] = GetItemByID (8);
   vendorItemList [8] = GetItemByID (1);
   vendorItemList [9] = GetItemByID (2);
   foreach (Item item in vendorItemList) {
    foreach (Slot slot in slotList) {
     // 限定Slot類型為VendorSlot
     VendorSlot vendorSlot = (slot as VendorSlot);
     // 如果同類型物品疊加在一起
     if (vendorSlot && slot.transform.childCount >= 1 
      && slot.GetItemID () == item.ID &&
      slot.IsFilled () == false) {
      slot.StoreItem (item);
      break;
     } // 如果沒有同類物品,則放入空Slot
     else if (vendorSlot && slot.transform.childCount == 0) {
      slot.StoreItem (item);
      break;
     }
    }
   }
  }

  private void MovePickedItemByMousePosition(){
   if (isPickedItem) {
    // 將滑鼠座標轉換成Canvas上的座標
    Vector2 position;
    Canvas canvas = GetComponentInParent ();
    RectTransformUtility.ScreenPointToLocalPointInRectangle (
     canvas.transform as RectTransform,
     Input.mousePosition,
     null,
     out position);
    pickedItem.SetLocalPosition (position);
   }
  }

  private void DiscardPickedItem(){
   // 處理物品丟棄
   // IsPointerOverGameObject(-1)判斷滑鼠左鍵是否有碰到GameObject
   if (pickedItem && Input.GetMouseButtonDown (0) 
    && EventSystem.current.IsPointerOverGameObject(-1) == false) {
    // TODO 跟Slot.cs的PickupAllSlotItem相衝突,若用DestroyImmediate會導致此方法被呼叫
    isPickedItem = false;
    pickedItem.Hide ();
   }
  }

  private Item GetItemByID(int id){
   foreach (Item entity in itemList.ConsumableEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   foreach (Item entity in itemList.EquipmentEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   foreach (Item entity in itemList.MaterialEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   return null;
  }

  private void ParseItemsJson(){
   // 從Resource資料夾中讀取Items.json
   TextAsset json = Resources.Load ("Items");
   // 解析Json格式
   itemList = JsonUtility.FromJson (json.text);
  }

  private void ParseForgeFormulaJson(){
   TextAsset json = Resources.Load ("ForgeFormula");
   forgeFormulaList = JsonUtility.FromJson (json.text);
  }
 }
}

撰寫完程式碼以後,回到我們介面的儲存跟載入按鈕,將InventorySystem拉進按鈕的On Click事件,選擇對應的方法,如下圖。


然後執行遊戲測試看看吧。

確定保存跟載入都可以使用。


留言