代码实例:
using UnityEngine;
using System.Collections; public class CursorController : MonoBehaviour
{
/// <summary>
/// 一般鼠标样式
/// </summary>
public Texture2D cursorMouseNormol;
/// <summary>
/// 单击鼠标样式
/// </summary>
public Texture2D cursorMouseClick; public float cursorWidth;
public float cursorHeight;
/// <summary>
/// 是否按下鼠标
/// </summary>
private bool isMouseClick = false;
// Use this for initialization
void Start()
{
//关闭系统默认鼠标显示
Screen.showCursor = false;
cursorWidth = cursorMouseNormol.width;
cursorHeight = cursorMouseNormol.height;
} // Update is called once per frame
void Update()
{
//鼠标左键单击切换鼠标样式
if (Input.GetMouseButtonDown())
{
isMouseClick = true;
}
else if (Input.GetMouseButtonUp())
{
isMouseClick = false;
}
} void OnGUI()
{
if (isMouseClick)
{
GUI.DrawTexture(new Rect(Input.mousePosition.x - cursorWidth / 2f, Screen.height - Input.mousePosition.y - cursorHeight / 2f, cursorWidth, cursorHeight), cursorMouseClick);
}
else
{
GUI.DrawTexture(new Rect(Input.mousePosition.x - cursorWidth / 2f, Screen.height - Input.mousePosition.y - cursorHeight / 2f, cursorWidth, cursorHeight), cursorMouseNormol);
}
}
}
PS:屏幕坐标系:左上角为原点
鼠标坐标系:左下角为原点