首页 > 代码库 > Unity3d之个性化鼠标

Unity3d之个性化鼠标

代码实例:

 1 using UnityEngine; 2 using System.Collections; 3  4 public class CursorController : MonoBehaviour 5 { 6     /// <summary> 7     /// 一般鼠标样式 8     /// </summary> 9     public Texture2D cursorMouseNormol;10     /// <summary>11     /// 单击鼠标样式12     /// </summary>13     public Texture2D cursorMouseClick;14 15     public float cursorWidth;16     public float cursorHeight;17     /// <summary>18     /// 是否按下鼠标19     /// </summary>20     private bool isMouseClick = false;21     // Use this for initialization22     void Start()23     {24         //关闭系统默认鼠标显示25         Screen.showCursor = false;26         cursorWidth = cursorMouseNormol.width;27         cursorHeight = cursorMouseNormol.height;28     }29 30     // Update is called once per frame31     void Update()32     {33         //鼠标左键单击切换鼠标样式34         if (Input.GetMouseButtonDown(0))35         {36             isMouseClick = true;37         }38         else if (Input.GetMouseButtonUp(0))39         {40             isMouseClick = false;41         }42     }43 44     void OnGUI()45     {46         if (isMouseClick)47         {48             GUI.DrawTexture(new Rect(Input.mousePosition.x - cursorWidth / 2f, Screen.height - Input.mousePosition.y - cursorHeight / 2f, cursorWidth, cursorHeight), cursorMouseClick);49         }50         else51         {52             GUI.DrawTexture(new Rect(Input.mousePosition.x - cursorWidth / 2f, Screen.height - Input.mousePosition.y - cursorHeight / 2f, cursorWidth, cursorHeight), cursorMouseNormol);53         }54     }55 }

PS:屏幕坐标系:左上角为原点

       鼠标坐标系:左下角为原点

Unity3d之个性化鼠标