DatabaseInventory
, which derives from IDatabase
. To display it within an editor window, you need to access this serialized object and transfer the values to the according property fields. I will cover this in a later tutorial when we make new editor windows step by step (one with scriptable objects, another without). Editor
class. Normally, when a script derives from Editor
and you want to start building your project, the build will fail. This is because the class Editor
is not available in built versions of the game, only in the Unity Editor. We have to place the script in a folder with the name Editor
, so it doesn't get included in the built game. But why the name Editor
? Unity uses so called Special folder names to tread a folder's content in a special way. See Unity Special Folders for more information. Assets --> Plugins --> Game Creator --> Inventory
, we have a bunch of folders. Animations are in the Animations folder, editor scripts in Editor folder, actions, conditions and many more scripts in Mono and so on. We need to change the following scripts: Item.cs
defines the item object itself. This class is used when a new object of type Item
is generated (like we do in the Inventory UI by clicking Create Item): ItemEditor.cs
gets executed when you click on Inventory in the sidebar of the Game Creator Preferences window, basically reading the content of the database and displaying it. In the next chapter, we will take a look at the structure of these scripts. It is better to have a certain understanding of how the scripts are structured before we continue. Item
, which contains the properties that can be set in the Inventory window and a method that allows us to create new item instances. It has properties like the name of the item, the description, if it can be sold, the price, item type and much more. There are two sections in this script. Properties and Constructor. Properties should be self-explanatory. If you open the script, we see that the Constructor (a method) section is only active in the Unity Editor. This is done with a so called define directive, here #if UNITY_EDITOR
. These directives allow us to i.e. execute certain code only in the editor (like in this case), on certain platforms only (like Windows or MacOS) or when using certain Unity versions (like Unity 2020.1). Each of these define directives need an opening statement #if xxx
and a closing statement #endif
. Inventory
class, we won't be able to do the following: SerializedProperty
variables. A SerializedProperty
is used when you want to access a value of a variable (property) of a serialized object. I will go deeper into this topic in a later tutorial. Finally, we have the Methods section of this class with all the methods. There are two particular methods to look for: OnEnable()
and PaintContent()
ItemEditor
is instantiated and the method OnPreferencesWindowGUI
of the same class gets called. When the objects gets instantiated, it automatically calls OnEnable()
, what reads the variables of the underlying database item of type Item
(which is now a serialized object) and sets the values of the ItemEditor
serialized property variables to the according Item
variable values (like itemName, sprite, price etc.). OnPreferencesWindowGUI
does some more stuff, but also calls PaintContent()
. This method paints the content of the window where the items are shown. Because we now have the serialized properties with the values of the item in the database, it is possible to simply add an EditorGUILayout.PropertyField
with the serialized property as the value. Example: serializedObject
refers to the item in the database and PROP_PREFAB
to the variable. serializedObject.FindProperty(PROP_PREFAB)
does nothing else than grab the value of the item object like we would with GameObject prefab = item.prefab
. Item
class (Item.cs). You can give any name you want. This is the first step to extend the inventory. Add it in the Properties section between the public GameObject prefab
and public bool canBeSold
variables. You can later put it anywhere in this section you like, but I do it this way for you to better understand. I include the previous and next one as well. ItemEditor
class (ItemEditor.cs). Here we do more things: Item
class: Item
class! In my case, my variable is called myVariable, that's why the constant string must also be myVariable. SerializedProperty
variable to the Properties section: SerializedProperty
variable the value of the property value of the serialized object in the method OnEnable()
: PaintContent()
: