[英]Storing class information in a dictionary or something similar
我很抱歉,如果这是一个重复的问题,但我进行了搜索,但找不到我想要的信息。 如果您也知道要引用的问题,请链接我!
但是无论如何,我有一个创建类的函数
private Item CreateItem(string name, bool stackable, int amount, string discription)
{
Item item = new Item(name, stackable, amount, discription);
return item;
}
然后我有另一个功能来查找统计信息
private Item findItemStats(string name)
{
if (name == "Gold")
return CreateItem(name, false, 0, "Gold Bar");
return null;
}
这也是即时消息用来添加商品的原因
internal void addItem(string name)
{
var item = findItemStats(name);
if (item == null)
Debug.LogError("Item not found!");
Instance.itemsToAdd.Add(item);
if (!inventory())
return;
if (inventory().activeInHierarchy)
{
placeItemsOnInventory();
sortItems();
}
我的问题是,有什么更好的方法来存储和检索项目统计数据。 我曾经托管一台私有服务器,并且在该服务器上,项目统计信息存储在.txt(或json w / e)中,然后将具有一个类,用于获取该数据并将其放置到被调用的项目中。 只是想知道执行此操作的一种方法,或者是将数据存储在单独的类/文件中且易于访问和放置项目数据的方法。
这可能是一个相当广泛的话题,取决于您的需求。 如果只是在本地保存内容,最简单的选择是使用PlayerPrefs
PlayerPrefs.SetInt("Player Score", 10);
PlayerPrefs.Save();
//And to fetch:
var playerScore = PlayerPrefs.GetInt("Player Score");
对于更复杂的东西,您可以将数据序列化为XML,JSON,二进制,CSV或要导入的任何数据等数据格式。这是二进制的示例。
public void SaveData()
{
if (!Directory.Exists("Saves"))
Directory.CreateDirectory("Saves");
BinaryFormatter formatter = new BinaryFormatter();
FileStream saveFile = File.Create("Saves/save.binary");
LocalCopyOfData = PlayerState.Instance.localPlayerData;
formatter.Serialize(saveFile, LocalCopyOfData);
saveFile.Close();
}
public void LoadData()
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream saveFile = File.Open("Saves/save.binary", FileMode.Open);
LocalCopyOfData = (PlayerStatistics)formatter.Deserialize(saveFile);
saveFile.Close();
}
另外,您可以使用工具将sqlite db集成到项目中。 此代码看起来像.net中的标准db连接。
string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
对于需要持久保存并在多台计算机上可用的数据。 您可能需要考虑将数据托管在适当的数据库或云托管的数据存储服务上。 一些例子:
yields lots of other goodies as well. Googling 产生了许多其他好处。 天空是极限!〜
Unity具有可用于存储数据的可脚本化对象,并且这些对象被存储在资产文件夹中,因此易于访问。
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.