- Home /
Where can I learn about editor programming?
There are several features I wish to build in the editor but I'm finding the usage of static functions for menu items a bit bewildering. I understand only the very basics of static functions but as soon as I want to do anything with any real data or references outside of the function itself I run into problems..
Answer by SteveFSP · Nov 07, 2010 at 04:20 PM
I haven't found any thorough documentation on extending the Unity editor beyond what Joshan mentioned: Extending the Unity Editor. But that article and the scripting reference may be all you need after you understand a couple of core concepts. (That's how it turned out for me, at least.)
In one of your comments you seem to indicate you are trying to create a custom inspector, so that is what I will concentrate on in my answer.
Here are some core concepts that may help you get started:
Associating a Custom Editor with a Unity Object
When you create a custom inspector by extending the 'Editor' class, you need to associate it with another class that inherits from a Unity object. (That is key, that it extends a Unity object.) In the article, it may not be obvious, but the LookAtPoint extends MonoBehaviour. So it qualifies.
// The attribute is what tells the Unity editor when to use the custom inspector.
// In this case, use this custom inspector for LookAtPoint.
@CustomEditor (LookAtPoint)
class LookAtPointEditor
extends Editor
{
// Since this class extends 'Editor', it can be a custom inspector, assuming
// you have placed it in the Editor folder.
}
The 'target' Field is the Object Being Edited
To gain access to the object that is being edited, use the 'target' field. If necessary, you can recast it to the class of the actual object. E.g. Recast to LookAtPoint. (This is important to know if you are using C#.)
Remember that you have access to everything within the target object. So if the object is a MonoBehaviour, you can use it to query the rest of the scene.
Intermixing GUI Layout and Code that Does Stuff
Once you get to the point where you can experiment, understanding the use of static methods to generate GUI elements should fall into place. Since the concepts behind GUI scripting is similar both in-game and in-editor, you can get a better understanding of it via the GUI Scripting Guide
An important item to remember is that you can intermix the code that does stuff with the GUI layout code.
if (GUILayout.Button("Build Mesh")
&& EditorUtility.DisplayDialog("Are you sure?"
, "This will permanently change the mesh."
, "Build mesh now."
, "No, I am not ready."))
{
// If the button is pressed and the OK option is selected in the dialog,
// then call a horribly complex method that builds a mesh.
BuildMesh(myMeshData);
}
Hope this helps.
Answer by Skjalg · Feb 08, 2012 at 02:57 PM
There is a great tutorial up at http://active.tutsplus.com/tutorials/workflow/how-to-add-your-own-tools-to-unitys-editor/comment-page-1/#comment-50637
Answer by jashan · Aug 05, 2010 at 10:59 AM
A good starting point for editor programming is: Extending the Editor.
If you're having trouble with static methods, one solution that I'm usually using is instantiating the objects as needed and then working with them. Depending on what kinds of objects you're dealing with, you might prefer to get references to the objects instead of creating new ones.
Cool cheers. I already tried the example in the tutorial/help file, and i just don't understand it. At no point can I read in what object the inspector is supposed to show up in 'yay shiny inspectors' I never get :( . The page says that the inspector created is there all the time but when I cut and paste the code exactly there is nothing showing up in the inspector and the help file doesnt say where I am supposed to find it. And thats where the tutorial ends...so all in all its pretty poor. Thanks anyway.
Are you sure you're putting your scripts into the Editor/-folder in your project's Assets folder? So the path would be Assets/Editor/? Probably you do because otherwise you'd get a compilation error but I thought I'd better ask ;-)