unity鼠标光标样式改变

时间:2021-04-07 08:58:50

通过搜集网上鼠标光标形状改变,整理如下

1.将下面javascript代码,将它挂在任意对象上即可,然后将手形状的图片拖动面板中,这种方法比较简单,写的代码也比较少

var mouse : Texture;

Screen.showCursor=false;
function Update () {
}
function OnGUI()
{
var msPos = Input.mousePosition;
GUI.DrawTexture(Rect(msPos.x , Screen.height-msPos.y,20,20),mouse);
}



效果图 如下unity鼠标光标样式改变


//原理很简单,先把鼠标光标屏蔽掉
//通过GUI来给鼠标位置放置一个贴图
注意的是:
鼠标坐标系同屏幕坐标系,屏幕的左下角是(0,0)点,向右X增加,向上Y增加
GUI的Rect区域坐标系同视口坐标系,左上角是(0,0)点,向右X增加,向下Y增加

注意坐标的变换


***************************************************************************

2.以下是c#代码,注意project面板中,要新建Resource文件夹,并且路径为Resource/Texture/Cursors/Cursor,不然加载不成功

using UnityEngine;
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
public class Cursorsystem : MonoBehaviour {

private TCursorState nowCursorState = TCursorState.None ;
private List<Texture2D> CursorTex = new List<Texture2D>();
private Rect CursorRect = new Rect();
private int cursorcount =0;
private float counttime =0.0f;
// Use this for initializationS
void Start () 
{

}

// Update is called once per frame
void Update () 
{

}

void OnGUI()
{

if(GUI.Button(new Rect(100,100,125,25),"None"))
{
SetCursorStyle( TCursorState.None);
}

if(GUI.Button(new Rect(100,125,125,25),"cCatch"))
{
SetCursorStyle( TCursorState.cCatch);
}


if(!Screen.showCursor)
{
if( (Time.time - counttime) > 0.3f )
{
counttime = Time.time;
cursorcount ++;
cursorcount = cursorcount%CursorTex.Count;
}
GUI.depth = 70;
CursorRect.x = Input.mousePosition.x;
CursorRect.y = Screen.height-Input.mousePosition.y;
CursorRect.width =CursorTex[cursorcount].width;
CursorRect.height = CursorTex[cursorcount].height;
GUI.DrawTexture(CursorRect,CursorTex[cursorcount]);
GUI.depth = 0;

}


}

public void SetCursorStyle(TCursorState style )
{
if (nowCursorState == style)
{
return ;

nowCursorState = style;
if(style == TCursorState.None)
{
Screen.showCursor = true;
CursorTex.Clear();
return ;
}
else
{
Screen.showCursor = false;
cursorcount=0;
counttime =Time.time ;
}
CursorTex.Clear();
TCursorStruct cursor = (TCursorStruct)RetrieveEnum<TCursorStruct>(style);
for(int i =0; i < cursor.framecount;i++ )
{
Texture2D tex = (Texture2D)Resources.Load("Texture/Cursors/Cursor"+ cursor.ID.ToString()+"_"+i.ToString(), typeof(Texture2D));
CursorTex.Add(tex);
}

}
public  T RetrieveEnum<T>(Enum value)
    {
        Type type = value.GetType();
        FieldInfo fi = type.GetField(value.ToString());
        T[] attrs = fi.GetCustomAttributes(typeof(T), false) as T[];
        if (attrs.Length > 0)
            return attrs[0];
        return default(T);
    }


}


public class TCursorStruct :System.Attribute
{
private int   _ID;
private byte  _framecount;
public TCursorStruct(int ID,byte framecount )
{
_ID = ID;
_framecount =framecount;
}
public int ID
{
get{ return _ID;}
}
public byte framecount
{
get{ return _framecount;}
}
}


//鼠标类型//
public enum TCursorState
{
[TCursorStruct(0,0)]            None,//系统光标样式//
[TCursorStruct(1,1)] cCatch,//抓捕样式//
}

可以系统光标与抓捕样式可以相互切换

unity鼠标光标样式改变