Hey,
I will be brief… how does Cursor.SetCursor works exactly? I’ve read the documentation here, here and here, but it works completely unreliably.
First I tried setting the cursor once and see what happens:
public Texture2D tex;
public Vector2 offset;
private void Start()
{
Cursor.SetCursor(tex, offset, CursorMode.Auto);
}
Result? Nothing happens. Then looking through some questions on forums and stuff it occurred to me that maybe, for some reason, it needs to be set every frame as lots of people suggest using things like OnMouseOver, so I tried the following:
public Texture2D tex;
public Vector2 offset;
private void Update()
{
Cursor.SetCursor(tex, offset, CursorMode.Auto);
}
Which, actually, worked! Cool awesome so easy! That is… until you try to actually work with the cursor in interesting ways… for example, in my game, when equipping ranged weapons, I want the cursor to be a crosshair, or melee weapons, a sword, or mouse over menu items, a pointer… so I tried the following code:
public int cursorOverride = -1;
public int defaultCursorIndex;
public Texture2D cursorMenu;
public Texture2D cursorSword;
private Vector2 defaultCursorOffset = new Vector2(0, 0);
public Texture2D cursorCrosshair;
private Vector2 cursorCrosshairOffset = new Vector2(32, 32);
private void Update()
{
if (cursorOverride == -1)
SetCursor(defaultCursorIndex);
else
SetCursor(cursorOverride);
}
private void SetCursor(int index)
{
if (index == 0)
{
Cursor.SetCursor(cursorMenu, defaultCursorOffset, CursorMode.Auto);
}
else if (index == 1)
{
Cursor.SetCursor(cursorSword, defaultCursorOffset, CursorMode.Auto);
}
else if (index == 2)
{
Cursor.SetCursor(cursorCrosshair, cursorCrosshairOffset, CursorMode.Auto);
}
}
Its extremely simple logic, and the logic is correct… the appropriate IF ELSE line gets called when I want it to… BUT… the cursor doesnt change! In Unity Editor, I have to click outside of the play area a few times, and get back to the play area and SOMETIMES the mouse is updated with the new texture… sometimes it doesnt… when exporting the project for web player (which is my target platform) nothing happens at all (both on Mac and PC)… really, I don’t know what Im doing wrong… it seems to be so simple, how can it be bugging?
Any ideas?
PS: My images are tagged as “Cursor” on the import settings, and I just dragged them to the script to make sure the script knew the images.
Thanks in advance,
Best regards,
Allan