Join Jesse Freeman for an in-depth discussion in this video Work with variables, part of Building Custom Tools to Extend the Unity IDE.
- [Voiceover] Now, let's talk about more complex types. You can open up any of the scripts attached to a Game Object by simply clicking on the script name in the Inspector. Let's double-click on Hello World to open it back up in MonoDevelop. Let's take a look at adding a Game Object as a field to our new class. We'll create a public GameObject called target. Let's save our code and go back into Unity. As you can see, once Unity recompiled the code, the Target field showed up in our Custom Inspector.
By default, it's going to be set to a value of None, and in parenthesis, you'll see that it's typed to a Game Object. Let's select the little circle next to the input field and open up the Selection window. The Selection window is comprised of two tabs, the Assets, which come, from your Project folder, and the Scene itself. Since we don't have any Game Objects in our Asset folder, let's select the Scene tab and select the New Sprite that we created. You'll see that it's now set as the Target in the Inspector.
Likewise, you can drag any Game Object onto this input field. Here, we've dragged the Main Camera and now that is the Target. This'll work with strings as well. Let's go back over to MonoDevelop and create a new public string called message, and we'll type in Hello World. If we save this and go back to the Inspector, you'll now see the string Hello World is there, and we can go in and edit that as well. Let's take a look at something a little bit more complicated like an array.
Here, we can create a public string array and we'll call this strings. Now, we don't need to initialize it. If we go back into the editor, Unity will automatically create the array for us. If you click the arrow, you'll see that our Strings array has a Size of zero. If we set this to something like three, it'll automatically create three elements for us. This is all saved internally as part of your project. Unity serializes our object and saves it as metadata along with each of the Game Objects we edit.
This allows you to go through and configure all the values of an array of Strings for example, play it in your game, and still have the values without having to store that or hard coding it in the class itself. You can expose all different kinds of types of arrays. Let's go back in and let's create an int array, we'll create a Vector3 array, and let's create an array of GameObjects. We'll save our code and go back into Unity.
As you can see, we now have arrays for each of these different types, and as we continue to increase the Size of the array, the Inspector will automatically create the correct input field based on the type. Here, our Positions will give us inputs for Vector 3s, and if we increase the Size of our Game Objects, we'll get input fields that allow us to select multiple Game Objects from our Scene.
At any time, if you wanna reset these arrays, simple change the values back to zero. Here, I'm gonna set all these to zero and close them out. Likewise, if I wanted to remove the Target, I could simply click on the circle next to the input field and select None from the options. Now, our Hello World script is reset to its default values. Let's save our changes.
- Exposing variables to the Inspector
- Providing attributes to Inspector fields
- Extending the base editor
- Creating custom UI elements
- Modifying the layout of a custom editor
- Building a custom window
- Saving and loading data
- Creating a reorderable list
- Skinning custom inspectors
- Exporting assets as Unity packages