Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. 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
  8. 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

    tokamac

    Joined:
    Jun 4, 2013
    Posts:
    8
    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:
    [CustomEditor(typeof(Handle))] 
    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):
    1.  
    2. [CustomEditor(typeof(GameObject))]
    3.  
    4. public class HexMapEditor : Editor {
    5.  
    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. }
    22.  
    23.  
    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:

    upload_2018-5-9_20-14-39.png

    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
    typeof(SceneView)
    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

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    498
    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: https://docs.unity3d.com/ScriptReference/Handles.PositionHandle.html
    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:
    https://code.tutsplus.com/tutorials/how-to-add-your-own-tools-to-unitys-editor--active-10047
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    284
    For an application like that I'd recommend the use of an
    EditorWindow
    , not an
    Editor
    . 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
    GameObject
    type, which understandably broke some things).
    EditorWindows allow you to create entirely new windows to use. You can then use their built in
    EditorWindow.OnSceneGUI
    method or the undocumented
    SceneView.onSceneGUIDelegate
    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.