- Home /
GUI with own mouse
Hi, I turned off the system mouse with
Screen.showCursor = false;
and made my own mousepointer with a texture thats follow the mouse.
var mousePos : Vector3 = Input.mousePosition;
var pos : Rect = Rect(mousePos.x,Screen.height - mousePos.y,cursorImage.width,cursorImage.height);
GUI.Label(pos,cursorImage);
But now, my mouse its behind all GUI elements. Is there something to put the mouse over the GUI?
Answer by Eric5h5 · Jun 08, 2010 at 07:16 AM
Put the mouse cursor code in its own script, and set GUI.depth for that script so it's drawn first. By the way, you should use Event.current.mousePosition instead of Input.mousePosition. That way you don't have to translate between screen space and GUI space.
Wow... all this time and I didn't know that property existed, heh... guess you learn something new every day ;)
Haha, just came across that little beauty... fantastic.
i see .. that's why Input.mousePosition not give exact position
When I use Event.current.mousePosition when I move the mouse up, it ends up going down, and the same happens when I move down, and I have the same exact script as klaus. Left and right work just fine though.
Answer by qJake · Jun 08, 2010 at 06:51 AM
Draw the mouse cursor last (or first), and make sure your GUI is all in one script. GUI is rendered in order of statement execution, so if it's behind everything else, put it at the opposite end of the script.
If you have multiple scripts that draw the GUI, unfortunately there's little you can do to control the order of script execution. From what I've read, it seems the script execution order is somewhat random, though you could try placing your script inside one of Unity's special folders, such as the Plugins/ folder. This MAY cause it to compile and run first, but I'm not sure, and I wouldn't bank on it.
Edit: As Eric said, you can also change the GUI.depth property to change the GUI layering.
Answer by PaulUsul · Jun 08, 2010 at 10:34 AM
I have had little luck with depth, but windows work great for the cursor :)
GUI.Window((int)WindowsID.MouseCursor, cursorRect, DrawCursor, "", GUIStyle.none); GUI.BringWindowToFront((int)WindowsID.MouseCursor);
private void DrawCursor(int id) { GUI.DrawTexture(new Rect(0, 0, cursorRect.width, cursorRect.height), currentCursor, ScaleMode.ScaleToFit, true); }
Or else you could implement a GUI draw order script. Only thing is that you get all your OnGUI in one method, dunno if that is good or bad..
GUI.depth works perfectly. If you're having problems, it's probably because you were trying to use it in the same OnGUI function in the same script, when in fact it's for use in different scripts.
Argh perfect is a really big word or your new and you don't seem new. If you look at the "What's new in Unity 2.5" http://unity3d.com/unity/whats-new/unity-2.5 You'll see that under "Other Fixes:" it says "GUI.depth now works again".. soo whether it works, really depends on the version :P