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).
Editorclass. Normally, when a script derives from
Editorand you want to start building your project, the build will fail. This is because the class
Editoris 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.csdefines the item object itself. This class is used when a new object of type
Itemis generated (like we do in the Inventory UI by clicking Create Item):
ItemEditor.csgets 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 xxxand a closing statement
Inventoryclass, we won't be able to do the following:
SerializedPropertyis 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:
ItemEditoris instantiated and the method
OnPreferencesWindowGUIof 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
ItemEditorserialized property variables to the according
Itemvariable values (like itemName, sprite, price etc.).
OnPreferencesWindowGUIdoes 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.PropertyFieldwith the serialized property as the value. Example:
Itemclass (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 prefaband
public bool canBeSoldvariables. 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.
ItemEditorclass (ItemEditor.cs). Here we do more things:
SerializedPropertyvariable to the Properties section:
SerializedPropertyvariable the value of the property value of the serialized object in the method