- Home /
Creating Hardware Mouse Cursor Texture at Runtime
I'm creating a texture at runtime to use as a mouse cursor. Using something like :
Texture2D moddedMouse = new Texture2D(mouseCursor.width, mouseCursor.height, mouseCursor.format, false, false);
Color32[] bitmap = mouseCursor.GetPixels32();
//stuff that edits the mouse cursor bitmap here
moddedMouse.SetPixels32(bitmap);
Cursor.SetCursor(moddedMouse, Vector2.one, CursorMode.Auto);
It works but Unity gives me this warning:
Invalid texture used for cursor - check importer settings
UnityEngine.Cursor:SetCursor(Texture2D, Vector2, CursorMode)
Globals:UpdateCursorColor() (at Assets/Globals.cs:273)
uStartMenu:Start() (at Assets/GUI/UGUI/uStartMenu.cs:50)
because Cursor.SetCursor() doesn't like a texture that isn't flagged as a "Cursor" type from the inspector. But I have no idea how to make a procedural texture "count" as a Cursor type. And I cant just create a Cursor and edit it at runtime because you cant set Cursor textures as read/write enabled.
Anyone know of a fix for this?
Answer by saschandroid · Oct 24, 2016 at 08:45 AM
Import the cursor texture you want to edit as Texture Type: Advanced. Enable read/write and alpha is transparency. Disable mipmaps and override color format to be ARGB 32 bit. Now you can edit and set it as cursor texture at runtime without warnings (tested unity 5.4.1f)
Thanks! It works with new Texture2Ds created from code as well, I was just forgetting to set "alpha is transparency".
Bah. Never$$anonymous$$d apparently Texture2D.alphaIsTransparency only exists in the editor. It breaks building a standalone build. I can still fix it I'll have to create a bunch of textures in the editor :(
https://issuetracker.unity3d.com/issues/texture2d-dot-alphaistransparency-causes-build-to-fail
Your answer
![](https://koobas.hobune.stream/wayback/20220612095253im_/https://answers.unity.com/themes/thub/images/avi.jpg)