Reputation: 429
I'm trying to create an editor window that's able to create in game items. It's supposed to add the item to a list of items that doesn't get cleared every time you play the game.
Is it possible to do this without an external XML/JSON file to save the data in.
I currently have this in my editor window script, ItemEquipable(type, price) is a basic class that just has a type and a price variable:
if(GUILayout.Button("Create"))
ItemList.items.Add(new ItemEquipable(type, price));
And the ItemList class, ItemEquipable extends the base Item class.
using UnityEngine;
using System.Collections.Generic;
public class ItemList:MonoBehaviour {
public static List<Item> items;
}
I've attached the ItemList to a GameObject that's in the scene.
I've seen Custom editor: How to initialise a new array element? but that script extends Editor and I'm extending EditorWindow, so I don't have access to a target variable.
Update:
Okay so I've tried making the ItemList class serializable
using UnityEngine;
using System;
using System.Collections.Generic;
[Serializable]
public class ItemList:MonoBehaviour {
public static List<Item> items;
}
And making a singleton out of it
using UnityEngine;
using System.Collections.Generic;
public class ItemList:Singleton<ItemList> {
public List<Item> items;
}
With the Singleton being your basic singleton class/code
Buth neither one is working.
Upvotes: 1
Views: 4975
Reputation: 429
So the way I solved it is by saving the array to a prefab every time you create an item.
UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Prefabs/Items/Item Manager.prefab");
PrefabUtility.ReplacePrefab(Selection.activeGameObject, prefab);
AssetDatabase.Refresh();
I've split up the ItemList classes into multiple lists:
[Serializable]
public class ItemList:MonoBehaviour {
[SerializeField] private List<ItemEquipable> equipableItems = new List<ItemEquipable>();
[SerializeField] private List<ItemWeapon> weaponItems = new List<ItemWeapon>();
[SerializeField] private List<ItemPower> powerItems = new List<ItemPower>();
[SerializeField] private List<ItemSpecial> specialItems = new List<ItemSpecial>();
public List<ItemEquipable> EquipableItems { get { return equipableItems; } }
public List<ItemWeapon> WeaponItems { get { return weaponItems; } }
public List<ItemPower> PowerItems { get { return powerItems; } }
public List<ItemSpecial> SpecialItems { get { return specialItems; } }
}
And every item is serialized, as well as it's fields:
[Serializable]
public class Item {
public static Item item = new Item();
[SerializeField] private ItemType itemType;
[SerializeField] private string itemName;
[SerializeField] private string itemDescription;
}
The static item variable is used in the editor to prevent cluttering my editor script with hundreds of variables:
Item.item.Type = (Item.ItemType)EditorGUILayout.EnumPopup("Item Type", Item.item.Type);
To set these variables I've just used getters and setters for each field in the item classes.
This might not be the most efficient way of doing it, but it works.
Upvotes: 0
Reputation: 190
The item array is not marked as Serializable so Unity can't save it when you launch the game. The solution is to mark ItemList
with the attribute [Serializable]
, or you can just use a List<Item>
directly.
Upvotes: 0