Unity 鼠標個性化


  最初的版本,API是可以直接設置鼠標顯示與否的,新版本就改了,先上下舊店的版本的;

  1.思路:

  •   在某些游戲下,經常會隱藏鼠標,或者有絢麗的動畫來代替鼠標顯示。
  •   原理就是將鼠標隱藏(不顯示)起來,設置一個sprite的坐標為鼠標坐標即可。當然代碼要放到 Update里才行。
  •   注意事項:此腳本不能掛在Camera上,否則會出現鼠標閃爍的情況。

  順帶貼上一個之前的腳本:

  

實例代碼:

public dfGUICamera Camera;
public dfSlicedSprite sprite;
public float value = 0f;

void Update()
{
//隱藏鼠標
Screen.showCursor = false;
sprite.transform.position = Camera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z));
}

  2.首先,用來做鼠標個性化的圖片,格式要是Cursor;見下圖:

  

   第二步:腳本掛載,寫腳本;將默認的和點擊時的鼠標圖,拖拽進去即可;

    public Texture2D norT;
    public Texture2D pressT;
    private Vector2 hotspot;
    void Start () {
        //Screen.showCursor = false;//outofDate
        //Cursor.SetCursor(norT, hotspot, CursorMode.Auto);
    }
    private void Update()
    { 
        if (Input.GetMouseButton(0))
        {
            Cursor.SetCursor(pressT, Vector2.zero, CursorMode.Auto);
            //Cursor.SetCursor(pressT, hospot, CursorMode.Auto);
            //hotspot:The offset from the top left of the texture to use as the target point (must // be within the bounds of the cursor). 默認選擇Vector2.zero
            //從紋理頂部的偏移量作為目標點(必須//位於游標的范圍內)
        }
        else
        {
            Cursor.SetCursor(norT, Vector2.zero, CursorMode.Auto);
        }
    } 
 

  簡單說下, 上面的方法里實現了鼠標左鍵點擊就會把鼠標的圖設置成對應的圖片。

       代碼里cursorMode = CursorMode.ForceSoftware;這個模式是說當鼠標改變了以后,鼠標移到Unity窗口外(例如你打開一個別的軟件)Unity里的鼠標依然不會消失而且是設定的圖標。

    若cursorMode = CursorMode.Auto這種模式,鼠標樣式只有一個,當你移到Unity窗口外,Unity里就沒有鼠標了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM