1、第一個直接改變鼠標樣式
1 public var cursorTexture:Texture2D; 2 private var changeFlag = false; 3 function Update(){ 4 if(changeFlag){ 5 Cursor.SetCursor(cursorTexture,Vector2.zero,CursorMode.Auto); 6 }else{ 7 Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto); 8 } 9 } 10 11 function OnGUI(){ 12 if(GUI.Button(Rect(10,10,100,50),"hand")){ 13 changeFlag = true; 14 } 15 if(GUI.Button(Rect(120,10,100,50),"arrow")){ 16 changeFlag = false; 17 } 18 }
2、隱藏鼠標,在鼠標位置放一新圖片
1 public var cursorTexture:Texture2D; 2 private var changeFlag = false; 3 4 function OnGUI(){ 5 if(GUI.Button(Rect(10,10,100,50),"hand")){ 6 changeFlag = true; 7 Screen.showCursor = false; 8 } 9 if(GUI.Button(Rect(120,10,100,50),"arrow")){ 10 changeFlag = false; 11 Screen.showCursor = true; 12 } 13 if(changeFlag){ 14 var mousePos = Input.mousePosition; 15 GUI.DrawTexture(Rect(mousePos.x,Screen.height - mousePos.y,cursorTexture.width,cursorTexture.height),cursorTexture); 16 } 17 18 }
需要注意幾點的是:用第一種方法中的鼠標圖片要修改圖片導入的屬性,即在inspector中的Texture Type要改為Cursor,否則鼠標會顯示不正常。
第二種方法中,在unity編輯器中運行的時候,當改變鼠標樣式后,默認的鼠標箭頭樣式不會消失,但是發布之后就沒有什么問題了