I was following a tutorial where I need to save the sprite that belongs to an object. I am creating a database of items and would of course want the correct image to come with the item. This is how I built my Item database:
ItemObject
[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);
}
ItemData
[System.Serializable]
public class ItemData {
public List<ItemObject> itemList;
}
I then Save/Load ItemData.itemList. That works great. As you can see I use "slug" to load the sprite of my Item, slug is basically a code-friendly name (ex: golden_hat), then I have the same name for my Sprite.
This also works great, but Unity saves the Sprite InstanceID, which always changes on start, so if I exit Unity and load my data, it will get the wrong Image.
My 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
}
}
]
}
So I'm guessing it wont work to do it this way, is there some other way to save a Sprite in Json? Or do I have to load the correct Sprite at runtime every time using my "slug"?