Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Custom Editor script, what is 'typeof' and other weirdness...

Discussion in 'Extensions & OnGUI' started by tokamac, May 9, 2018.

  1. tokamac


    Jun 4, 2013
    I want to write a custom editor script to allow me to edit a mesh in scene view (I have written MonoBehaviour that does it in Game mode, so I figure there is a way to do it in Edit mode ).

    I'm making progress, but the documentation is sparse and the going it tough.

    Part of the problem is, I don't formally understand what the typeof parameter means in the
    [CustomEditor(typeof( some class? ))] 
    declaration. (is this extending the Editors control over instances of those objects, or classes, or inheriting from them, or what?)

    I see the example in the documentation:
    but cannot make sense of the parameter is supposed to do and what it is for. (I understand what the keyword typeof means - that is not the issue).

    For example, First up: get a click on my mesh in scene view. I cobbled this script together from gems found on the Web:

    Code (CSharp):
    2. [CustomEditor(typeof(GameObject))]
    4. public class HexMapEditor : Editor {
    6.     public void OnSceneGUI()
    7.     {
    8.         Event e = Event.current;
    9.         int controlID = GUIUtility.GetControlID (FocusType.Passive);
    10.         switch (e.GetTypeForControl (controlID)) {
    11.             case EventType.MouseDown:
    12.                 if( e.button == 0 ){
    13.                     Debug.Log("Left MouseDown  > " + controlID + " > " + e.mousePosition);
    14.                 }else if( e.button == 1 ){
    15.                     Debug.Log("Right MouseDown  > " + controlID + " > " + e.mousePosition);
    16.                 }else if( e.button == 2 ){
    17.                     Debug.Log("Middle MouseDown  > " + controlID + " > " + e.mousePosition);
    18.                 }
    19.                 break;
    20.         }
    21. }
    And it only works - I get my clicks - when the value is 'GameObject'
    [CustomEditor(typeof(GameObject ))]

    However, when I implement this code, the Inspector changes - the 'Active' checkbox and menus at the top of the inspector are gone, and replaced with this:


    I implicitly understand why this happened, but that the same time it makes me want to poke my eyes out with a stick.

    I would have thought the Custom editor should be a
    is what I should use, but if I use that value, the clicks don't register in my script

    Does anyone have some insight they can lend me, a good tutorial that explains any of this, and/or how I else I can detect clicks on my mesh in Editor mode - or what the best, better or another way of doing this is?

    Thanks in advance!
  2. Johannski


    Jan 25, 2014
    CustomEditors are for customizing scripts in the inspector. This way you can show the serialized properties in a different way and add functionality to it. Since you're changing the custom editor of GameObject and don't draw anything, the buttons from unity's customeditor disappeared.
    Take a look at Handles:
    They have some neat examples of what you might want to achieve. They also create MonoBehaviours for their OnSceneView functionality, so you don't have this CustomEditor running on any GameObject (and therefore all the time). Otherwise you can also subscribe to SceneView.onSceneGUIDelegate to get updates from there.
    Here is a tutorial that might give you a better idea to get to your goal:
  3. Madgvox


    Apr 13, 2014
    For an application like that I'd recommend the use of an
    , not an
    . Editors are for overriding the default GUI for the inspector specifically (the CustomEditor attribute is for the purposes of attaching an editor script to a specific type of object. In your case, you specified that your script would be the "custom editor" for the
    type, which understandably broke some things).
    EditorWindows allow you to create entirely new windows to use. You can then use their built in
    method or the undocumented
    to recieve events for and draw on the scene.

    I can commiserate with your confusion, learning the editor ecosystem (especially Handles) is a bit of a steep climb, especially with the lack of clear documentation for editor functions.