Join Jesse Freeman for an in-depth discussion in this video Expose variables to the Inspector, part of Building Custom Tools to Extend the Unity IDE.
Now, let's create our own script and attach it to the sprite. We'll select this sprite, click Add Component, go down to New Script, and here we'll call this "Hello World" Let's also make sure that the language is set to C#, and then click Create an Ad. We now have our own custom script. As you can see, Unity's going to put it at the root of our folder by default. Let's drag it and put it into our Scripts folder to keep it organized. Unity is very good at keeping track of where references to files are.
So in this case, even though we moved the Hello World script into the scripts folder, you'll see that it still exists on our new sprite. As long as you move files around inside of the unity editor, it'll keep track of their references. If you were to do this outside of the editor, you would break the reference, and you would get an error for the missing script. Now, let's double-Click on our Hello World script and switch over to Monodevelop. As you can see, unity has automatically created a basic template for our script. We can delete the start and update method since we won't be using them in this course and simply focus on the fields that the Unity inspector will automatically expose for us.
Any public field that we put on a class will show up in the inspector with some caveats, and we'll go over those limitations later. To get started, let's create a new field for speed that we'll type to a float. And next, let's create a public Vector3 for our start position. Let's save our script and go back into unity. If we select our new sprite and take a look at our Hello World script in the inspector, you'll now see that speed and start position are both in the inspector.
As you can see, this is incredibly powerful. It allows us to easily configure values that are public on any of our scripts attached to game objects. Primitive types, such as numbers, booleans, strings, vectors, will all show up automatically in our inspector. Let's go ahead and 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