Just tried my game on a new monitor for the first time. The cursor shrinks to almost 1/3 its size! (It’s a much larger monitor).
Is there a way to set the cursor’s size relative to the game itself, or the screen on which its being played?
Thanks!
Just tried my game on a new monitor for the first time. The cursor shrinks to almost 1/3 its size! (It’s a much larger monitor).
Is there a way to set the cursor’s size relative to the game itself, or the screen on which its being played?
Thanks!
Yes! Depends on the cursor. If it is in the UI, then use anchors and force the cursor size to always be a preset fraction of either the width or the height.
If the cursor is in a sprite, it should already be the same relative size, unless you’re changing your camera settings in the build.
Hm… I’m not sure I know how to answer that.
I’m using the default cursor:
https://docs.unity3d.com/ScriptReference/PlayerSettings-defaultCursor.html
Which one would this be?
Oh, apologies… never used that API. I always make my own cursors as floating images inside a canvas so that I can have direct better control. Perhaps someone else here is familiar with the built in cursor above and can chime in.
Found an answer here:
Actually I came back here to say this solved the issue partly… but this cursor does not work with UI elements.
So I’m still lost on how to use a cursor with UI elements too.
What does this mean, you mean it no longer interacts with them and yet it did before?
Or do you mean the UI elements don’t grow with the cursor??
I would NOT expect the UI elements to grow with the above fix, but you can set your CanvasScaler to grow with the screen if you like, just the usual UI Scaling choices.
So basically if I use the default cursor, this creates a cursor that does not scale relative to a screen size.
If I use the custom cursor (as seen in the video) it only shows up in the regular game, and does not appear over the UI elements.
Oh that’s probably just a UI sorting order and sorting layer issue. Run the game, press pause and see, then go fiddle with the layering.
You might have to use either a separate non-root Canvas in your hierarchy above the cursor to force its layer to be above all else. See docs on UI layering / sorting order.
CAUTION: the layer of a GameObject has nothing to do withe Sorting Layer and Sorting Order of a UI element.
I think that’s the main issue I’m having - the gameobject cursor cannot be used for both the game and UI.
My cursor is the main mechanic of my game, so I might not be as nit picky if not. The question is how to use a custom, well-proportioned (size) cursor for both in-game and UI. I genuinely cannot find an answer for this anywhere! Face palm.
I feel your pain. I just went through several different cursor solutions. Using a separate camera to render the cursor independent of the rest of the game, having the cursor be a sprite in the screen, having the cursor in the same canvas as UI elements, and creating a canvas on top of your UI elements. None of these solutions allow for a customizable cursor that is able to exist above all other elements in the scene and interact with the UI. If anyone has a solution, I would love to hear it.
Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:
https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037
In short, as far as the Standard Rendering Pipeline,
The default 3D Renderers draw stuff according to Z depth - distance from camera.
SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties
UI Canvas Renderers draw in linear transform sequence, like a stack of papers
If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:
There may be more than one solution to try.
For instance, you may even use multiple co-located cameras (along with different Layers and LayerMasks drawn to different-depth Cameras) to more-explicitly control apparent draw ordering.
Additional reading in the official docs:
And SortingGroups can also be extremely helpful in certain circumstances:
Other rendering pipelines may have other ways of layering (HDRP and URP). Go see the latest docs for details.
There’s lots of third party open source packages available as well, such as this:
https://github.com/mob-sakai/ParticleEffectForUGUI
You can watch the mouse yourself and generate events with your own custom InputModule and pretty much any “normally-written” UGUI setup will accept the inputs. Check the Unity docs for more, but here’s an example of what that looks like. Obviously you REMOVE the InputModule that came with the scene already and use this.
using UnityEngine;
using UnityEngine.EventSystems;
// @kurtdekker
public class TestInputModule : StandaloneInputModule
{
// this bangs on the input module
public void ClickAt(float x, float y)
{
bool b, bb;
Input.simulateMouseWithTouches = true;
var pointerData = GetTouchPointerEventData(new Touch()
{
position = new Vector2(x, y),
}, out b, out bb);
ProcessTouchPress(pointerData, true, true);
}
// hook this into the button so you can confirm it gets clicked
public void ButtonCallback()
{
Debug.Log( "Clicked!");
}
void Update()
{
// press C to click center of screen; if a button is there it will fire.
if(Input.GetKeyDown(KeyCode.C))
{
ClickAt(Screen.width / 2, Screen.height / 2);
}
}
}
The custom cursor in my current project is honestly just a UI Toolkit panel that has a sort order to put it in front of everything else, with a visual element that follows the regular mouse cursor (of which is hidden). Nothing more complicated than that.
The same thing is no doubt possible with uGUI as well.
I believe the original issue that the OP was encountering was a bug I fixed a few years ago: Unity Issue Tracker - [Windows] Max Mouse cursor size is limited when using Cursor.SetCursor() and cannot be increased past a certain threshold
We used to not properly scale the cursor by the monitor DPI settings and thus on larger monitors it always looked much smaller than the default Windows cursor.
If you need it to be even larger than that, you can use Cursor.SetCursor with CursorMode.ForceSoftware, which should maintain the original texture size. That said, using any kind of software solution instead of a hardware cursor will add a couple frames of latency (as in, your mouse input that you receive in game will be 2 frames ahead of what the user sees on the screen) so if your game needs accurate clicks, I do not recommend it.