As I promised in my latest post I'm going to share my experience about writing a tool in Unity.At first I didn't have any idea about adding editor parts to Unity and was a little confused about it but the good news is it's really easy.
Our tool adds a EditorWindow to Unity that enables you make and edit of Hierarchical Finite State Machine in a graphical manner.
It starts with making a script class and inherit it from EditorWindow. First important point about making this class is that Unity assumes all of your editor codes are in Editor folder of your main Assets folder. classes that you make in this folder can see every other class of your project but other classes outside can't see them. and it seems rational because it shouldn't be any trace of your editor classes in the final executable that you publish to users and they don't necessarily have Unity installed on their systems !
After making the Window class you can assign a menu item that opens your window. Take a look at this for a simple example of doing this task.
And this is start of your job to show whatever you want in the window.Unity calls your window's OnGUI() method every time an event fires in editor and you should update everything that you want to be shown in your window. Events can be everything like MouseDown, MouseUp, KeyDown, KeyUp, Repaint ...
If an event is intended to make a change to some part of your window (like clicking one button) and not affecting other parts (like any control behind the button) you can call Event.current.Use(). It means you used the event and it's not valid any more.
I used this event system to make a DraggableComponent and use it wherever i needed something to be draggable.
As you can see in the image below using 8 DraggableComponents I made a ResizableComponent.
Each one of these DraggableComponents are a resize handle and with this method you can even change the mouse cursor when mouse enters their areas.
Next part of my tool was saving the result in a file and for some reason I needed to serialize a class type in it to load an object with that type later but type isn't serializable and you can't just save it's full name as a string because if somebody change the class's name later you can't use it any more.So I searched for the solution that Unity uses itself for saving this kind of data and I found asset GUID.
GUID is a 128 bit unique id for any asset that exist in Assets folder of a unity project.it dosn't change over time and you can use it for accessing that asset.
Take a look at this and this and remember that Asset GUID and InstanceID are two different things with different usages. InstanceID is an 32 bit id that will be assigned to any object that loads into memory at runtime and remains valid for that object just until finishing current run but you can't use it for accessing object from one run of unity to another.
my tool is still under construction.It needs a good API for drawing some lines and curves.I used a class that I've got from one of my friends but it doesn't work very well and needs some revise.
I hope this post be useful for anyone who want's to start editor scripting in Unity.