2

I have a problem, I want to get the current cursor, and get it as a texture2D in Unity.

When I say current cursor, I mean the current cursor the user has got. For example, if the user changes his cursor to a cat, I want to have the same cat cursor in Unity. That is the reason why I don't just search online for the default cursor.

I have tried to search in google for this, but all I got is this , it was posted in 2009, and the code doesn't work(it says "Handle doesn't represent a ICON" if you were wondering).

2
  • 1
    Show us your code, what have you tried yourself? Commented Oct 23, 2017 at 14:30
  • @MeanGreen Well, i really don't know where to start. But as i said in my question, I have searched in google, and this was the only result i could find, and i tried Tarsier answer, and it makes a "Handle doesn't represent a ICON" error. Commented Oct 23, 2017 at 15:39

3 Answers 3

2

Well the steps to follow that comes to my mind are:

  1. Check what cursor is active in Windows
  2. Read that image as a texture in Unity
  3. Apply the texture to the cursor

The problem I see is this will change from OS to OS, so it will be hard for you to make it compatible with all Operative System.

I wasnt able to read the current active cursor, so my answer is uncomplete. Maybe someone will be able to complete what is missing:

//This is the part I am not sure how to complete
//String currentCursor = 

//Here is where Windows store the cursors, you need to point to the one the 
//user is using
String path = "C:\Windows\Cursors"+currentCursor;

//Here you load that image as a texture
Texture2D cursorTexture = new Texture2D(16, 16);
cursorTexture.LoadImage(File.ReadAllBytes(path));

public CursorMode cursorMode = CursorMode.Auto;
public Vector2 hotSpot = Vector2.zero;

//You apply the texture to the cursor in Unity
void Start()
{
    Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
}

Maybe you can find here how to do the first step using something similar to this, however I don´t know it

Sign up to request clarification or add additional context in comments.

Comments

1

Any solution with win32 API would be platform dependend and don't work on another platforms. So you can add your own custom cursor manager.

//container for cursor data
[System.Serializable]
public struct CustomCursor
{
    public Texture2D Texture;
    public Vector2 HotSpot;
    public CursorMode Mode;
}

//container for all cursor you used in you project
[System.Serializable]
public class CursorsHolder
{
    [SerializeField]
    private CustomCursor defaultCursor;
    [SerializeField]
    private CustomCursor cursorA;
    [SerializeField]
    private CustomCursor cursorB;
    [SerializeField]
    private CustomCursor cursorC;

    public CustomCursor DefaultCursor { get { return defaultCursor; } }
    public CustomCursor CursorA { get { return cursorA; } }
    public CustomCursor CursorB { get { return cursorB; } }
    public CustomCursor CursorC { get { return cursorC; } }

    public void InitializeDefault(CustomCursor defaultCursor)
    {
        this.defaultCursor = defaultCursor;
    }
}

public interface ICursorHandler
{
    Texture2D CurrentCursor { get; }
    void SetCursor(CustomCursor newCursor);
}

//Manager that cached last setted cursor
public class CursorHandler
{
    private CustomCursor currentCursor;

    public CustomCursor CurrentCursor { get { return currentCursor; } }

    public void SetCursor(CustomCursor newCursor)
    {
        currentCursor = newCursor;
        Cursor.SetCursor(currentCursor.Texture, currentCursor.HotSpot, currentCursor.Mode);
        Debug.LogFormat("{0}", currentCursor.Texture);
    }
}


//Main script for cursor management usage
public class MyScript : MonoBehaviour
{
    [SerializeField]
    private CursorsHolder cursorsData;

    ICursorHandler cursorHandler = new CursorHandler();

    private void Awake()
    {
        cursorsData.InitializeDefault(new CustomCursor() { Texture = PlayerSettings.defaultCursor, HotSpot = Vector2.zero, Mode = CursorMode.ForceSoftware });

        cursorHandler.SetCursor(cursorsData.DefaultCursor);     
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            cursorHandler.SetCursor(cursorsData.CursorA);
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            cursorHandler.SetCursor(cursorsData.CursorB);
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            cursorHandler.SetCursor(cursorsData.CursorC);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            cursorHandler.SetCursor(cursorsData.DefaultCursor);
        }

    }
}

You must not forget assign default cursor in player settings.

Comments

1

Thank you guys, but i figured it out,i found out about this and used SepehrM answer to convert the cursor to bitmap, and just used this to convert the bitmap to a texture2D, Thanks again :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.