I’m working on an inventory system. I have a slot gameobject for every slot I have in my inventory.
Now I want to change the given item while in edit mode and have it in play mode aswell. Here is my current code.
[ExecuteInEditMode, SelectionBase]
public class InventorySlot : MonoBehaviour
{
[HideInInspector]
public Image spriteImage;
[HideInInspector]
public Text amountText;
private InventoryItem _item = null;
public InventoryItem item{ get { return _item; } set { _item = value; UpdateItem(); } }
void Start()
{
UpdateItem();
}
public void UpdateItem()
{
if(_item != null)
{
SetSprite(_item.sprite);
SetAmount(_item.amount);
}
else
{
SetSprite(null);
SetAmount(0);
}
}
(...)
}
[CustomEditor(typeof(InventorySlot))]
public class InventorySlotEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
InventorySlot inventorySlot = (InventorySlot)target;
InventoryItem newItem = EditorGUILayout.ObjectField("Item", inventorySlot.item, typeof(InventoryItem), false) as InventoryItem;
if(inventorySlot.item != newItem)
{
// Item changed
inventorySlot.item = newItem != null ? newItem.Clone() : null;
}
if(inventorySlot.item != null)
{
int amount = EditorGUILayout.IntField("Amount", inventorySlot.item.amount);
if(amount != inventorySlot.item.amount)
{
// Amount changed
inventorySlot.item.amount = amount;
inventorySlot.UpdateItem();
}
}
}
}
The thing is, in edit mode everything works fine. But when I press play, my set item gets defaulted to null.
How can I force unity to save my changes?
Thanks.