Switching to hardware cursors

Since I switched eDriven.Gui to Unity 4 quite recently, I finally found the time to check out the new cursor API.

The API itself is pretty simple and basic:

// the texture
var cursorTexture: Texture2D;
// cursor mode
var cursorMode: CursorMode = CursorMode.Auto;
// the offset (distance from top left of the image in pixels)
var hotSpot: Vector2 = Vector2.zero;
function OnMouseEnter () {
    // setting the custom cursor
    Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
function OnMouseExit () {
    // restoring the default cursor (passing null)
    Cursor.SetCursor(null, Vector2.zero, cursorMode);

The switching to a new API went without a problem, except the small issue of working in editor but not on build:

The WebPlayer build had no cursor displayed, and the Windows build had a strange quadratic noisy image instead of the cursor.

Then I found out about the texture importer Cursor setting and that fixed the problem.

I just thought that it would be helpful if this would be written in bold somewhere, so here I go:

You have to change the texture importer setting to Cursor, for each and every image in the project used as a cursor.

Texture importer cursor type

Texture importer cursor type

New cursors are working as a charm as they eliminate the lag between a real mouse position and the cursor image position found with software cursors.

Applications now seems faster and more responsive.

Hardware cursors

Hardware cursors

eDriven.Gui will keep the possibility to render the cursor to a cursor stage, but adds the additional two modes of hardware/software rendering provided by Unity.

So, the new eDriven.Gui CursorManager mode enumeration is as follows:

/// The enumeration for the cursor rendering mode
public enum CursorRenderingMode
	/// Uses the Unity cursor with CursorMode.Auto
	/// Uses the Unity cursor with CursorMode.ForceSoftware
	/// Uses the eDriven.Gui cursor rendering to a cursor stage

Additionally, eDriven.Gui keeps its current API that is a bit more complex than Unity’s one, implementing:

1. Cursor priorities
2. Cursor animations

Overall, the Cursor API is a simple but great addition to Unity, making it a perfect RIA platform.

Posted in News