簡體   English   中英

如何獲取當前光標作為 Texture2D Unity C#

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

我有一個問題,我想獲取當前光標,並將其作為 Unity 中的 texture2D 獲取。

當我說當前光標時,我的意思是用戶擁有的當前光標。 例如,如果用戶將他的光標更改為一只貓,我希望在 Unity 中擁有相同的貓光標。 這就是為什么我不只是在線搜索默認光標的原因。

我試圖在谷歌中搜索這個,但我得到的只是這個,它是在 2009 年發布的,並且代碼不起作用(如果您想知道,它會說“句柄不代表圖標”)。

好吧,我想到的步驟是:

  1. 檢查 Windows 中哪個光標處於活動狀態
  2. 在 Unity 中將該圖像作為紋理讀取
  3. 將紋理應用到光標

我看到的問題是這會因操作系統而異,因此您很難使其與所有操作系統兼容。

我無法讀取當前的活動光標,所以我的回答不完整。 也許有人將能夠完成缺少的內容:

//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);
}

也許你可以在這里找到如何使用類似的東西來做第一步,但我不知道

任何使用 win32 API 的解決方案都將依賴於平台,並且不能在其他平台上運行。 因此,您可以添加自己的自定義光標管理器。

//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);
        }

    }
}

您一定不要忘記在播放器設置中分配默認光標。

謝謝你們,但我想通了,我發現了這一點,並使用 SepehrM 答案將光標轉換為位圖,並僅使用它將位圖轉換為紋理 2D,再次感謝 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM