C# 保存并加载库存
我已经创建了库存,一切正常。现在我应该实现库存的保存和加载(在文件中)。然而,我发现自己陷入了如何继续的困境。我正在考虑创建一个清单数据脚本来获取可序列化的数据,然后保存它。我没有使用脚本对象。你能给我一些建议吗?下面是库存代码C# 保存并加载库存,c#,unity3d,save,load,inventory,C#,Unity3d,Save,Load,Inventory,我已经创建了库存,一切正常。现在我应该实现库存的保存和加载(在文件中)。然而,我发现自己陷入了如何继续的困境。我正在考虑创建一个清单数据脚本来获取可序列化的数据,然后保存它。我没有使用脚本对象。你能给我一些建议吗?下面是库存代码 public class Inventory2 : MonoBehaviour { public bool inventoryEnabled; public GameObject inventory, slotHolder; priva
public class Inventory2 : MonoBehaviour
{
public bool inventoryEnabled;
public GameObject inventory, slotHolder;
private Transform[] slot;
public int level;
void Start()
{
level = SceneManager.GetActiveScene().buildIndex;
GetAllSlots();
}
void Update()
{
if (Input.GetAxis("Inventory") != 0)
{
inventoryEnabled = !inventoryEnabled;
}
if(inventoryEnabled)
{
inventory.SetActive(true);
}
else
{
inventory.SetActive(false);
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Clues")
{
AddClue(other.GetComponent<Clue2>());
}
}
public void AddClue(Clue2 clue)
{
Text description = clue.GetComponent<Text>();
for (int i = 0; i < 2; i++)
{
if(slot[i].GetComponent<Slot2>().empty == true && clue.pickedUp == false)
{
slot[i].GetComponent<Slot2>().clue = clue;
slot[i].GetComponent<Slot2>().descriptionFinal = description;
slot[i].GetComponent<Slot2>().empty = false;
clue.GetComponent<Clue2>().pickedUp = true;
}
}
}
public void GetAllSlots()
{
slot = new Transform[Clue2.objects];
for(int i = 0; i < Clue2.objects; i++)
{
slot[i] = slotHolder.transform.GetChild(i);
slot[i].GetComponent<Slot2>().empty = true;
}
}
}
公共类清单2:单一行为
{
启用公共布尔目录;
公共游戏对象目录,slotHolder;
专用转换[]槽;
公共智力水平;
void Start()
{
level=SceneManager.GetActiveScene().buildIndex;
GetAllSlots();
}
无效更新()
{
if(Input.GetAxis(“库存”)!=0)
{
inventoryEnabled=!inventoryEnabled;
}
如果(已启用资源清册)
{
inventory.SetActive(true);
}
其他的
{
inventory.SetActive(false);
}
}
公共空对撞机(对撞机其他)
{
if(other.tag==“线索”)
{
AddClue(other.GetComponent());
}
}
公共无效添加线索(Clue2线索)
{
Text description=clue.GetComponent();
对于(int i=0;i<2;i++)
{
if(slot[i].GetComponent().empty==true&&clue.pickedUp==false)
{
slot[i].GetComponent().clue=clue;
插槽[i].GetComponent().descriptionFinal=description;
插槽[i].GetComponent().empty=false;
clue.GetComponent().pickedUp=true;
}
}
}
public void GetAllSlots()
{
slot=新变换[Clue2.objects];
对于(int i=0;i
公共类Slot2:单一行为
{
公共俱乐部2;
公共布尔空;
公共文本descriptionFirst,descriptionFinal;
无效唤醒()
{
}
无效更新()
{
如果(线索)
{
this.getComponentChildren().text=descriptionFinal.text;
}
其他的
{
this.getComponentChildren().text=descriptionFirst.text;
空=真;
}
}
}
公共类俱乐部2:单一行为
{
公共静态int对象=0;
公共静态int objectsCollected=0;
公共文本描述;
公共学校选择;
公共游戏对象cluepopupanel、canvasCluesPanel;
公共画布画布;
公共静态bool-activeClue=false;
无效唤醒()
{
对象++;
}
无效更新()
{
if(canvasCluesPanel.gameObject.activeSelf==true&&Input.GetAxis(“拾取”)!=0)
{
activeClue=true;
cluepopupanel.gameObject.GetComponent().text=description.text;
}
}
专用空对撞机(对撞机其他)
{
如果(other.tag==“玩家”)
{
if(canvasCluesPanel.gameObject.activeSelf==false)
{
canvasCluesPanel.gameObject.SetActive(true);
}
}
}
私人空间OnTriggerStay(碰撞器其他)
{
if(other.tag==“Player”&&activeClue==true)
{
cluepopupanel.gameObject.SetActive(true);
cluepopupanel.getComponentChildren().text=this.GetComponent().text;
canvasCluesPanel.gameObject.SetActive(false);
}
if(other.tag==“Player”&&activeClue==false)
{
cluepopupanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(true);
}
}
私有无效OnTriggerExit(碰撞器其他)
{
if(other.tag==“Player”&&activeClue==true)
{
cluepopupanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(false);
activeClue=false;
if(对象已收集<对象)
{
objectsCollected++;
}
}
其他的
{
cluepopupanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(false);
activeClue=false;
}
canvasHUD.gameObject.SetActive(true);
}
}
保存/加载数据的方法有很多种。
通常,它包括将数据序列化到某个存储位置(内存、硬盘、网络服务器等),然后读取数据
我发现最简单的解决方案是使用JSON序列化(这是一个很好的解决方案)
假设我的库存存储为列出库存
使用JsonObjectAttribute(MemberSerialization.OptIn)
标记项
类(以避免序列化从MonoBehavior
继承的内容),并使用JsonPropertyAttribute
装饰希望存储的每个属性
然后,您可以将数据序列化到PlayerPrefs
(它基本上是在本地设备上存储数据的抽象)并从那里加载回来
伪代码:
private const string PlayerStatePlayerPrefKey = "PlayerSaveData";
void SavePlayerState(PlayerState state)
{
var serializedState = JsonConvert.Serialize(state);
PlayerPrefs.SetString(PlayerStatePlayerPrefKey, serializedState);
PlayerPrefs.Save();
}
PlayerState LoadPlayerState()
{
var serializedState = PlayerPrefs.GetString(PlayerStatePlayerPrefKey, null);
if (serializedState == null)
return new PlayerState();
return JsonConvert.DeserializeObject<PlayerState>(serializedState);
}
转向
private void OnTriggerEnter(Collider other)
{
if (!other.tag == "Player" || !canvasCluesPanel.gameObject.activeSelf)
return;
canvasCluesPanel.gameObject.SetActive(true);
}
制作游戏非常复杂,拥有更干净、更易维护的代码将极大地帮助您的开发
祝你好运保存/加载数据的方法很多。 通常,它包括将数据序列化到某个存储位置(内存、硬盘、网络服务器等),然后读取数据 我发现最简单的解决方案是使用JSON序列化(这是一个很好的解决方案) 假设我的库存存储为
列出库存
您可以使用JsonObje标记Item
类
private const string PlayerStatePlayerPrefKey = "PlayerSaveData";
void SavePlayerState(PlayerState state)
{
var serializedState = JsonConvert.Serialize(state);
PlayerPrefs.SetString(PlayerStatePlayerPrefKey, serializedState);
PlayerPrefs.Save();
}
PlayerState LoadPlayerState()
{
var serializedState = PlayerPrefs.GetString(PlayerStatePlayerPrefKey, null);
if (serializedState == null)
return new PlayerState();
return JsonConvert.DeserializeObject<PlayerState>(serializedState);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (canvasCluesPanel.gameObject.activeSelf == false)
{
canvasCluesPanel.gameObject.SetActive(true);
}
}
}
private void OnTriggerEnter(Collider other)
{
if (!other.tag == "Player" || !canvasCluesPanel.gameObject.activeSelf)
return;
canvasCluesPanel.gameObject.SetActive(true);
}
[System.Serializable]
public class SlotData
{
public bool containsItem = false;
public string Description;
//other possible elements
public int amount;
}
[System.Serializable]
public class InventoryData
{
public SlotData[] inventorySlots;
}
public void PrepareToSave()
{
//setup the inventory
var yourInventory = new InventoryData();
//fill all the slots, you must also calculate their amount
yourInventory.inventorySlots = new SlotData[CalculateYourAmount];
//fill all the slots here
for (int i= 0; i < CalculateYourAmount; i++)
{
//fill all the slots
yourInventory.inventorySlots[i] = CreateTheSlot();
}
//this go to the next step
SaveYourInventory(yourInventory);
}