Scripting a custom mouse cursor with the new uGUI

Hey guys,

I’m wondering if there’s a way to script a custom mouse cursor now using the new uGUI (Unity’s new gui system)? I currently have a custom mouse cursor working, but it’s with the old GUI, and I’m noticing some bad mouse input lag with my code currently.

I’d like to transition to the new GUI with a custom mouse cursor, so I was hoping maybe someone had figured out a decent way of getting a custom cursor with the new GUI.

Here’s my current custom mouse code using the old GUI system, and like I said, I’m noticing annoying lag with this:

function OnGUI() {
   
    //Custom cursor code (there is also a "showcursor" option to be enabled/disabled on Start().)
    //*Note:  I have to divide the mouse position by different amounts in order to compensate for any given cursor image (depending on its size)
    if(cursorShowAttack1 == true) {
        GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX1/4.2, Event.current.mousePosition.y-cursorSizeY1/5.3, cursorSizeX1, cursorSizeY1), attackCursor1);
    }
    else if(cursorShowFriendly1 == true) {
        GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX1/4.2, Event.current.mousePosition.y-cursorSizeY1/5.3, cursorSizeX1, cursorSizeY1), friendlyCursor1);
    }
    else if(cursorShowChop2 == true) {
        GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX1/4.2, Event.current.mousePosition.y-cursorSizeY1/5.3, cursorSizeX1 + 10, cursorSizeY1 + 10), chopTreeCursor1);
    }
    else {
        //GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX/2, Event.current.mousePosition.y-cursorSizeY/2, cursorSizeX, cursorSizeY), yourCursor);
        GUI.DrawTexture (Rect(Event.current.mousePosition.x-cursorSizeX1/4.2, Event.current.mousePosition.y-cursorSizeY1/5.3, cursorSizeX1, cursorSizeY1), yourCursor1);
    }
    
}



function Start () {
   
    //This statement is for getting a custom cursor to work in-game
    Cursor.visible = false;

}

Why are you not using the builtin hardware cursor?

What does a “builtin hardware” cursor allow you to do? What is the opposite of the builtin hardware cursor? Is that what my old code is using? This is something I need to learn. I thought I was using the builtin hardware cursor.

All I knew, when I researched how to do it with the old system, is that was the way I learned how to change the mouse. I don’t know what is the “best way” to do it. I would love to know the best (with good performance) way to do it.

I’m looking at your script reference link right now.

Ok, I started using that function, and it pretty much solved all the problems I’ve been having with my custom mouse cursor for the last few months lol.

Not only does it perform a lot better on my starting screen scene, it also allows the mouse to not be frozen in my loading screen scene, which was something I was very worried about as users would think their computer froze or the game crashed – when really it was just the scene loading.

You have helped me kill two birds with one stone here. Thank you!

I’d still like to know what I was using with my old code, in the OnGUI() function, and why that way was causing those behaviors.

This answer on stack overflow explains it pretty well. opengl - What is hardware cursor and how does it work? - Stack Overflow

here is an tutorial for youre question

lol. Thanks beno531, but that’s exactly what I posted in my first post. I’ve already done that video. Using that method I guess is what’s called using “Software mode”. I appreciate the suggestion though.

Hardware mode solves all of my problems, except for the fact that the mouse cursor is locked to 32x32 (I’m guessing that’s an operating system thing). Which is really small. But it performs great.

If there were a way to resize a hardware custom cursor, everything would be perfect.