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).
Well the steps to follow that comes to my mind are:
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.