Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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: