首页 > 代码库 > 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之个性化鼠标
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。