Using Sprite for Custom mouse Cursor using SetCursor

Hello! I’ve checked around for how to use a custom mouse cursor, I’ve gotten in to work with the Texture2D which it implies that it needs using this code:

	CursorMode cursorThing;
	Texture2D cursorText;

	void Start() {
		cursorThing = CursorMode.Auto;
		cursorText = new Texture2D(128, 128, TextureFormat.ARGB32, false);
		renderer.material.mainTexture = cursorText;
	}

	void Update () {
		Cursor.SetCursor(cursorText, Vector2.zero, cursorThing);
    }

Is there any way that I could cast this into a sprite instead? This way I wouldn’t have to work around adding another texture thingy for working with the same image that already is a sprite in my asset folder. Any tips on implementing this would be highgly appreciated :slight_smile:

2 Answers

2

For anyone interested, I figured it out myself by looking through some other threads that were answered in a similar way. This is how I ended up converting my sprite to a texture.

Firstly, you have to go to your Sprite in your asset folder. Change the “Texture Type” to “Advanced”. Make sure “Read/Write Enabled” is checked. Now we are able to manipulate this thing. Here is the code that makes this sprite function as a texture as well:

	//Office objects
	public Sprite office_Stamp;
	public Texture2D office_Stamp_Text;

	void Start () {
		//Office Object collector
		office_Stamp = Resources.LoadAssetAtPath<Sprite>("Assets/Sprites/Objects/Stamp.png");
	}
	
	void Update () {
		TexturizeSprites();
	}

	void TexturizeSprites() {
		//office_Stamp
		office_Stamp_Text = new Texture2D((int)office_Stamp.rect.width, (int)office_Stamp.rect.height );
		var pixels = office_Stamp.texture.GetPixels((int)office_Stamp.textureRect.x, 
		                                            (int)office_Stamp.textureRect.y, 
		                                            (int)office_Stamp.textureRect.width, 
		                                            (int)office_Stamp.textureRect.height );
		office_Stamp_Text.SetPixels( pixels );
		office_Stamp_Text.Apply();

I hope this can help someone in the future :slight_smile: Feel free to add a comment or something if there’s anything you feel you can improve.

Hi, in response to your answer…
i don’t think its necessary to write the pixel data into a new Texture2D object, you can simply access the texture via sprite.texture.

i’ve tested this using this function… no need to go into advanced options for your sprite.

hope it helps ^^

void SetCursor( Sprite sprite , Vector2 center ) {
     Cursor.SetCursor (sprite.texture, center, CursorMode.Auto);
}

change CurserMode to ForceSoftware if you’d like the cursor to be the same size as your sprite.

Hey, this seems to be a bug that's been reported (and reportedly fixed in Unity 5.4), but your solution works fine during testing inside the editor, but when a build is created the texture for the cursor gets corrupted as soon as it's changed in your way. Changing sprites to texture type "Cursor" fixed this for cursor specific textures, however using the texture directly from the sprite in the way you describe seems to corrupt the image when it's being tested in a build.