gpt4 book ai didi

c# - 将值存储到 android 设备

转载 作者:行者123 更新时间:2023-11-29 19:12:27 27 4
gpt4 key购买 nike

<分区>

我正在为我的游戏创建库存系统,问题是我需要创建系统来将多个值存储到我的设备。

我知道有 PlayerPrefs 但我只能存储 2 个值(让我们说第一个是键,第二个是值),但我需要像表这样的东西。所以我必须在一行中存储这样的东西:

inventorySlot
itemID
amount

这是简单的 3 大小值,但我还需要存储 4、5 或更多。

那么我该如何实现呢?

这是当前代码,其中的问题是无法存储到 json。查看 Debug.Log 末尾的注释以查看它显示的内容。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryModel
{
public List<InventoryItem> InventoryItems;

public InventoryModel()
{
InventoryItems = new List<InventoryItem>();
}
}

public class InventoryItem
{
public int inventorySlot;
public int itemID;
public float amount;
}


public class Inventory : MonoBehaviour
{
private int inventorySlots = 5;

public Text itemSlot1;

void Start ()
{
AddItemToInventory(1, 0, 2000);
LoadInventory();
}

void Update ()
{

}

public static void AddItemToInventory(int slot, int itemID, float amount)
{
InventoryModel im = new InventoryModel();
InventoryItem ii = new InventoryItem();
ii.inventorySlot = slot;
ii.itemID = itemID;
ii.amount = amount;

im.InventoryItems.Add(ii);

foreach(InventoryItem ia in im.InventoryItems)
{
Debug.Log(ia.itemID + "-" + ia.amount + "-" + ia.inventorySlot); //Display 0-2000-1
}
string jsonString = JsonUtility.ToJson(im);
Debug.Log(jsonString); //Display {}
PlayerPrefs.SetString("PlayerInventory", jsonString);
}

private void LoadInventory()
{
foreach(InventoryItem ii in LoadInventoryFromPref().InventoryItems)
{
//Doesnt enter this loop
Debug.Log(ii.inventorySlot);
Debug.Log(ii.itemID);
Debug.Log(ii.amount);
Debug.Log("====");
}
}

private static InventoryModel LoadInventoryFromPref()
{
string test = PlayerPrefs.GetString("PlayerInventory");
Debug.Log(test); //display {}
return JsonUtility.FromJson<InventoryModel>(test);
}

private static int GetMaxInventorySlots()
{
if(PlayerPrefs.HasKey("MIS"))
{
return PlayerPrefs.GetInt("MIS");
}
else
{
return 0;
}
}
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com