- Home /
Cursor.setCursor hotSpot to center of image
I have a couple of custom cursors in my game's shop. All of them need to have the hotSpot in the center of the image, but I've tinkered a lot with the hotSpot argument and I cant seem to get it away from the default top-left. Please help. Thanks.
Have you tried "new Vector2(16,16) for your hotspot? Assu$$anonymous$$g you are using 32x32 texture for the cursor.
Answer by Cherno · Apr 28, 2014 at 01:17 PM
image.width / 2 and image.height / 2 should do the job. Also, be sure to have your cursor images imported as cursors in the import settings.
Perfect and couldn't be more succinct. Thank you so much.
Answer by cowhi · Oct 09, 2014 at 12:58 PM
Here is a full working example in C#:
using UnityEngine;
using System.Collections;
public class MouseScript : MonoBehaviour {
public Texture2D cursorTexture;
private Vector2 cursorHotspot;
// initialize mouse with a new texture with the
// hotspot set to the middle of the texture
// (don't forget to set the texture in the inspector
// in the editor)
void Start () {
cursorHotspot = new Vector2 (cursorTexture.width / 2, cursorTexture.height / 2);
Cursor.SetCursor(cursorTexture, cursorHotspot, CursorMode.Auto);
}
// To check where your mouse is really pointing
// track the mouse position in you update function
void Update () {
Vector3 currentMouse = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay (currentMouse);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
Debug.DrawLine (ray.origin, hit.point);
}
}
Hey man, thanks, it worked. I had an issue because in my game I use only one cursor and I can't set it's hotspot in the inspector. Cherno said this, too, but you provided the script for me. Thanks!