- Home /
Custom Cursor is hidden by UI
My custom mouse cursor is going behind the UI elements, instead of being in-front.
the mouse cursor object just has a transform, sprite renderer, and a simple mouse script that simply updates the position of the image and hides the real cursor.
The mouse cursor is a game object I've placed up a canvas with no other objects, and I have set that canvas sort order to as high as possible 32,767. all the other canvas's sort order is under 30. However this the image is still going behind all the other stuff for some reason.
Any help would be greatly appreciated.
Answer by strangecloudsworkmail · Jan 01, 2019 at 09:25 PM
Ok, so I figured it out. What I had was a gameobject with: "Transform, sprite renderer, and my custom FollowMouse script." What I needed was a rect transform, Image, and the script on my cursor object. The parent canvas should be set to "Overlay." I had to do a bit of tweaking to my script to get the point to follow the cursor properly, but the image is appearing on top everything now.
Hi - I am having the same problem. I made the changes as you did, but my cursor is for some weird reason stuck in the corner of the screen. Guess it has something to do with the sprite renderer. So my question is - what did you change in your script in order for it to work?
The fix here is to set transform.position to Input.mousePosition;
You might find examples of this... Camera.main.ScreenToWorldPoint(Input.mousePosition);
Unfortunately this won't work as expected and you'll end up with a cursor stuck at the corner of your screen.
So instead, in your GameCursor script just use public class GameCursor : $$anonymous$$onoBehaviour { // Start is called before the first frame update void Start() { Cursor.visible = false; }
// Update is called once per frame
void Update()
{
// Notice this won't work
// Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Instead just use this
transform.position = Input.mousePosition;
}
}
You'll probably also want to uncheck the "Raycast Target" on your Image Component since it will detect any onClick events at the Cursor Game Object itself rather than registering to your UI Elements underneath (ie. buttons)
Answer by LogicBlade · Oct 09, 2020 at 01:30 AM
I was able to get my custom cursor to appear by making my cursor gameObject a child of the canvas.
Your answer
Follow this Question
Related Questions
5.3 : Display particles over an Image component 1 Answer
UI object drawing order is different between Unity Editor and Standalone version 1 Answer
Canvas override sorting issue. 1 Answer
What sets Canvas.renderOrder and why would it not respect the sorting order? 0 Answers
4.6 UI sort order in fornt of IMGUI/OnGUI possible? 2 Answers