My Inventory system is now fully functional. Thanks to those who helped me put it together!

I am going to give away the major components to the inventory system’s code, as a special thanks to the people in this forum who helped me put it together. Thanks for the advice and tips you gave to making it all possible!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class PlayerPrefabScript : MonoBehaviour {
    [System.Serializable]
    public class Inventory {
        public List<Item> items;
    }
    public Inventory inventory;
    public Item itemInHand;

   public Rect GUIArea = new Rect(0, 0, 0, 0);

    void OnGUI () {
        GUILayout.BeginArea(GUIArea);
        GUILayout.BeginVertical();
        foreach(Item item in inventory.items){
            if(item != null){
                if(GUILayout.Button(item.ItemID, GUILayout.Width(50), GUILayout.Height(50))){
                    itemInHand = item;
                }
            }
        }
        GUILayout.EndArea();
        GUILayout.EndVertical();
    }
    void OnSaveInv () {
        string SavedInv = JsonUtility.ToJson(inventory);
        PlayerPrefs.SetString("SavedInv", SavedInv);
    }
    void OnLoadInv () {
        string LoadedInv = PlayerPrefs.GetString("SavedInv");
        Inventory LoadedItems = JsonUtility.FromJson<Inventory>(LoadedInv);
        inventory = LoadedItems;
    }
}

I will be updating the code later, as the above code is only part of the origional prototype.

2 Likes

The List refers to a sepperate script that lists all of the attributes an item in the game can have.

Thanks for giving back.

Even if you are using OnGUI :wink:

I figure if OnGUI() is good enough to write the entire Unity3D editor in, it’s good enough for MY little games…

I wish it was more performant and controllable though, to be sure.

1 Like

True. Except the Unity Editor is not a real time application.

You can make it work. It just gets painful for complex UI. And it has a dozen gotchas waiting under the surface.

Yeah, I have a love-hate relationship with OnGUI().

The fact that if you use OnGUI(), then 100% of your UI can be viewed essentially in one location and source controlled as a single unit is really the big draw.

Having to flip and twiddle between handler classes and the delegates that you hook up to editor-placed UI stuff just feels irritating and fragile, especially with the disconnects between the scope of scripts, script lifetimes, scenes and prefabs, and having to scatter your changes all over the place to change even something trivial.

1 Like

Actually let me expand on that: all UI gets painful for complex UI. All UI systems out there except the bash prompt.

1 Like