Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Is there any hack, to set a custom cursor for editor window?

Discussion in 'Scripting' started by petarjnordeus, Dec 27, 2022.

  1. petarjnordeus

    petarjnordeus

    Joined:
    Feb 3, 2018
    Posts:
    7
    this works
    Code (CSharp):
    1. EditorGUIUtility.AddCursorRect(new Rect(20, 20, 140, 40), MouseCursor.Pan);
    but this doesnt
    Code (CSharp):
    1. EditorGUIUtility.AddCursorRect(new Rect(0,0,50,50), MouseCursor.CustomCursor);
    this thing only works for GameView
    Code (CSharp):
    1. Cursor.SetCursor(Resources.Load<Texture2D>("image") as Texture2D, new Vector2(20,20), CursorMode.Auto);
    is there any workaround to load custom images for the mouse cursor? thank you
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    Well, there's no straight forward way to render a software cursor in an editor window. The AddCursorRect method allows you to select an actual hardware cursor that is supported by the OS. If you want to use a software cursor in the Unity editor when on a windows system, it might be possible to somehow include or load a custom invisible hardware cursor using the Cursor class from the WSA subnamespace as explained for the CustomCursor setting. The ressource ID is an actual ressource inside the Unity editor exe or some other binary file. Once you have successfully hidden the hardware cursor for your window, you can simply display a GUI.Box with your desired image at the end of the OnGUI method at the cursor position. That gives you your software cursor.

    If you can't hide the cursor that way, it should be possible to reach out to the Windows API and actively hide the hardware cursor yourself.
     
  3. petarjnordeus

    petarjnordeus

    Joined:
    Feb 3, 2018
    Posts:
    7
    ok after lots of trial and error, im back to answer my own question :D

    the SetCursor method does work, but only under the Repaint event. plus you have to combine it with EditorGUIUtility.AddCursorRect() method. here is the code:

    Code (CSharp):
    1.  
    2. private void OnGUI()
    3. {
    4.       e = Event.current;
    5.  
    6.       switch (e.type)
    7.       {
    8.           case EventType.MouseLeaveWindow:
    9.               cursorIsOut = true;
    10.               break;
    11.  
    12.           case EventType.MouseEnterWindow:
    13.               cursorIsOut = false;
    14.               break;
    15.       }
    16.  
    17.       Repaint();
    18.  
    19.       if (e.type == EventType.Repaint && !cursorIsOut)
    20.       {
    21.           // PlayerSettings.defaultCursor = cursor_normal;
    22.           Cursor.SetCursor(cursor_normal, new Vector2(10, 8), CursorMode.Auto);
    23.           EditorGUIUtility.AddCursorRect(new Rect(0, 0, position.width, position.height), MouseCursor.CustomCursor);
    24.       }
    25.  
    26.       if (e.type == EventType.Repaint && cursorIsOut)
    27.       {
    28.           // PlayerSettings.defaultCursor = null;
    29.           Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    30.       }
    31. }
    32.  
     
    GamerXP likes this.