简体   繁体   English

如何获取当前光标作为 Texture2D Unity C#

[英]How to get current cursor as Texture2D Unity C#

I have a problem, I want to get the current cursor, and get it as a texture2D in Unity.我有一个问题,我想获取当前光标,并将其作为 Unity 中的 texture2D 获取。

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.例如,如果用户将他的光标更改为一只猫,我希望在 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).我试图在谷歌中搜索这个,但我得到的只是这个,它是在 2009 年发布的,并且代码不起作用(如果您想知道,它会说“句柄不代表图标”)。

Well the steps to follow that comes to my mind are:好吧,我想到的步骤是:

  1. Check what cursor is active in Windows检查 Windows 中哪个光标处于活动状态
  2. Read that image as a texture in Unity在 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也许你可以在这里找到如何使用类似的东西来做第一步,但我不知道

Any solution with win32 API would be platform dependend and don't work on another platforms.任何使用 win32 API 的解决方案都将依赖于平台,并且不能在其他平台上运行。 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.您一定不要忘记在播放器设置中分配默认光标。

谢谢你们,但我想通了,我发现了这一点,并使用 SepehrM 答案将光标转换为位图,并仅使用它将位图转换为纹理 2D,再次感谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM