- Home /
Custom Editor Window to control other objects
Hello everybody,
I am just starting to explore the Editor classes in Unity so this part is fairly (read: completely) new to me.
I would like to create a custom window that has all sorts of sliders and control that modify various objects in my scene.
I have managed to do this quite easily, but I am wondering if what I do is right, and if there is a more efficient / elegant way.
What I have done is this:
Step 1: Created an editor script:
public class EditorTest : EditorWindow
Step 2: OnEnable, I am looking for game objects in the scene and extracting their script component, for example:
GameObject go = GameObject.Find( "Sphere" );
if( go != null ) sphere = go.GetComponent<SphereScript>();
Step 3: Then, in OnGUI I am creating an edit control for a given field for each object, like:
if( sphere != null ) {
sphere.x = EditorGUILayout.Slider ("X Position", sphere.x, -3, 3);
}
Is this right? Am I in the right direction? Any better recommended way of doing this?
I should also note that I prefer not to make any changes in my original game objects, I just want this "extension" so that I can edit the scene from one big window, instead of several per-object inspector windows.
I always feel a bit uneasy about leaning on GameObject.Find, but that relates more to client code in game. I suppose anything is fair game when it comes to editor features - you can always buy a more powerful computer. So if you use Find, or the tag equivalent, then it really comes down to whether you have imposed suitable conventions on your game scenes to ensure your windows will be of use to you.
Regarding the edit controls, you might like to consider looking at SerializedObject and Property, so as to be able to take advantage of undo features; if your scenes are of any complexity then you'll be glad of it later.
@phodges Thank you for that. You hit the nail right on its head. One of the reasons I asked this question is that I am also trying to avoid GameObject.Find.
I am using serialized objects here and there, but still, it lives in a particular object's inspector. But I am at a stage where I need to balance all sorts of gameplay parameters, and thinking what would be the best way to do so.