目标:改变鼠标样式,使其变成小手
原理很简单,就是将原理的鼠标光标隐藏了,然后在OnGUI里在相应的鼠标位置绘制自己喜欢的鼠标图片就行了。
直接贴代码了:
- #pragma strict
- var myCursor : Texture2D;
- var myClickCursor : Texture2D;
- var cursorWidth : float;
- var cursorHeight : float;
- private var isClicked : boolean = false;
- function Start () {
- Screen.showCursor = false;
- }
- function Update () {
- if (Input.GetMouseButton(0))
- isClicked = true;
- else
- isClicked = false;
- }
- function OnGUI () {
- var mousePos = Input.mousePosition;
- if (isClicked)
- GUI.DrawTexture(Rect(mousePos.x - cursorWidth / 2, Screen.height - mousePos.y - cursorHeight / 2,
- cursorWidth, cursorHeight), myClickCursor);
- else
- GUI.DrawTexture(Rect(mousePos.x - cursorWidth / 2, Screen.height - mousePos.y - cursorHeight / 2,
- cursorWidth, cursorHeight), myCursor);
- }
点击时和未点击时用不同的图片样式。
OK, 试试效果吧!