简体   繁体   中英

Change mouse cursor icon in C#

I am building a game for Windows PCs and I need to change the cursor icon when the user is over clickable UI elements. I have found this command Cursor.SetCursor(texture2d, vector2) but it has some lag.

When the mouse is over the UI elements it changes but it has some delay which is really annoying (This is what other users said about that anyway).

After some reading, I learned that Unity basically just changes the cursor icon in the software level. ie Hides the cursor and displays my image, and make it follow the cursor position.

My question is: How can I change the icon in hardware level, again, in windows builds only.

When I searched "Changing mouse cursor in C# ", I have found the windows.forms option (which doesn't work in unity) and a c++ code but it wasn't full (Only methods' names), and I don't know how to run it in C#…

The SetCursor not work very good on all Windows app, but is the right way to do it.

Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);

Another way is to make a Little fake hidding the mouse cursor and making a GUI cursor for your desire case. You can add more conditions for each event you like to customize.

var OnMouseEnterCursor:Texture2D;
var cursorSizeX: int = 32;  // your cursor width
var cursorSizeY: int = 32;  // your cursor height
var MouseEnterCond = false;

function OnMouseEnter()
{
 MouseEnterCond = true;
 Screen.showCursor = false;
}

function OnMouseExit()
{
 MouseEnterCond = false;
 Screen.showCursor = true;
}

function OnGUI()
{
    if(MouseEnterCond )
    {
     GUI.DrawTexture (Rect(Input.mousePosition.x-cursorSizeX/2 + cursorSizeX/2, (Screen.height-Input.mousePosition.y)-cursorSizeY/2 + cursorSizeY/2, cursorSizeX, cursorSizeY),OnMouseEnterCursor);
    }
}

If there is a way to enforce hardware cursors in Unity the world has not found it yet.

Unity's own documentation explains it pretty well: A hardware cursor is preferred on supported platforms through the function which you have yourself found (remember to set the cursor-mode to auto), but it will automatically, and uncontrollably, fall back on software for unsupported platforms and resolutions.

An important thing to note is that there is a special field (which the reference only peripherally mentioned) in the project settings/player settings menu called the "Default Cursor". This is the only supported hardware cursor on eg windows store apps.

And remember to set your textures to type "cursor" in their configurations,.

Finally, keep in mind that windows only supports the 32x32px size . This may force Unity's hand when selecting the render type.

The default cursor settings in the player settings worked in the editor but it did not show up in a build. Also limiting the texture size to 32x32. I resolved this issue by using Cursor.SetCursor method in a script to change the cursor.

[SerializeField] Texture2D cursor;

void Start() {
       Cursor.SetCursor(cursor, Vector3.zero, CursorMode.ForceSoftware);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM