【发布时间】:2017-11-03 13:38:23
【问题描述】:
我正在学习一个需要保存属于某个对象的精灵的教程。 我正在创建一个项目数据库,当然希望该项目附带正确的图像。这就是我建立我的项目数据库的方式:
项目对象
[System.Serializable]
public class ItemObject{
public int id;
public string title;
public int value;
public string toolTip;
public bool stackable;
public string category;
public string slug;
public Sprite sprite;
public ItemObject(int id, string title, int value, string toolTip, bool stackable, string category, string slug)
{
this.id = id;
this.title = title;
this.value = value;
this.toolTip = toolTip;
this.stackable = stackable;
this.category = category;
this.slug = slug;
sprite = Resources.Load<Sprite>("items/" + slug);
}
项目数据
[System.Serializable]
public class ItemData {
public List<ItemObject> itemList;
}
然后我保存/加载 ItemData.itemList。这很好用。如您所见,我使用“slug”来加载我的项目的精灵,slug 基本上是一个代码友好的名称(例如:golden_hat),然后我的 Sprite 使用相同的名称。
这也很好用,但是 Unity 保存了 Sprite InstanceID,它总是在启动时更改,所以如果我退出 Unity 并加载我的数据,它会得到错误的图像。
我的 Json:
{
"itemList": [
{
"id": 0,
"title": "Golden Sword",
"value": 111,
"toolTip": "This is a golden sword",
"stackable": false,
"category": "weapon",
"slug": "golden_sword",
"sprite": {
"instanceID": 13238
}
},
{
"id": 1,
"title": "Steel Gloves",
"value": 222,
"toolTip": "This is steel gloves",
"stackable": true,
"category": "weapon",
"slug": "steel_gloves",
"sprite": {
"instanceID": 13342
}
}
]
}
所以我猜这样做是行不通的,还有其他方法可以在 Json 中保存 Sprite 吗?还是每次使用“slug”时我都必须在运行时加载正确的 Sprite?
【问题讨论】:
-
您不应该在游戏中保存精灵。您应该使用 AssetBundles 或 Resources API。至于保存,保存AssetBundles的path或者Resources文件夹
-
@Programmer 谢谢,我暂时保存资源文件夹的路径。并开始研究 AssetBundles。