Search Unity

What is the editor callback for mouse hover (mouse over)

Discussion in 'Documentation' started by Arthur_Gentz, Apr 12, 2021.

  1. Arthur_Gentz

    Arthur_Gentz

    Joined:
    Jan 11, 2021
    Posts:
    22
    I'm taking some inspiration from the unity open project (Chop-chop) state machine. States are comprised of scriptableobject actions. So I often need to open an action's script , done be clicking on either the scriptable object instance in the state inspector or transition table window, to take me to the action instance in the project window, then 'right click -> edit script', or globally searching for the script name in visual studio/rider. There are many scriptableobject instances, and their names don't always indicate what the script might be called, so I often need to repeat the above process -which has become tedious.
    Hence, I'd like to write a custom inspector that hooks into some 'OnMouseHover' callback -if it exists, to show a popup with the name of the action script, whenever the mouse hovers on any state action field in the inspector.

    I just need to know what that 'OnMouseHover' or 'OnMouseOver' method analogue is in the unity editor/inspector API. So who knows of a close enough callback like that?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Depends!

    If you're doing the default inspector stuff (imgui/OnInspectorGUI), it's really clunky. What I'd do is reserve a Rect, use EditorGUI.PropertyField to draw the property field inside that rect, and then check that against the mouse position. You'll probably only want to do that if Event.current.type is a mouse move event, as the rects have busted values during some of the events. Something like:

    Code (csharp):
    1. GUILayout.Label(string.Empty, options); // create the rect
    2. var rect = GUILayoutUtility.GetLastRect();
    3.  
    4. EditorGUI.ObjectField(rect, stateScriptableObject, ...);
    5.  
    6. if (Event.current.type == EventType.MouseMove && rect.Contains(Event.current.mousePosition)) {
    7.    // draw the thing
    8. }
    It might be better to just always draw a field with the script used next to the ScriptableObject's field. You can get a reference to the script asset used with MonoScript.FromScriptableObject, and then draw that with a ObjectField.



    If you're using the new UI Toolkit to draw it, you'd call visualElement.RegisterCallback<MouseEnterEvent>() and spawn your label.