Unity 鼠标个性化

时间:2023-03-09 02:27:40
Unity 鼠标个性化

  最初的版本,API是可以直接设置鼠标显示与否的,新版本就改了,先上下旧店的版本的;

  1.思路:

  •   在某些游戏下,经常会隐藏鼠标,或者有绚丽的动画来代替鼠标显示。
  •   原理就是将鼠标隐藏(不显示)起来,设置一个sprite的坐标为鼠标坐标即可。当然代码要放到 Update里才行。
  •   注意事项:此脚本不能挂在Camera上,否则会出现鼠标闪烁的情况。

  顺带贴上一个之前的脚本:

  Unity 鼠标个性化

实例代码:

public dfGUICamera Camera;
public dfSlicedSprite sprite;
public float value = 0f; void Update()
{
//隐藏鼠标
Screen.showCursor = false;
sprite.transform.position = Camera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z));
}

  2.首先,用来做鼠标个性化的图片,格式要是Cursor;见下图:

  Unity 鼠标个性化

第二步:脚本挂载,写脚本;将默认的和点击时的鼠标图,拖拽进去即可;

    public Texture2D norT;
public Texture2D pressT;
private Vector2 hotspot;
void Start () {
//Screen.showCursor = false;//outofDate
//Cursor.SetCursor(norT, hotspot, CursorMode.Auto);
}
private void Update()
{
if (Input.GetMouseButton())
{
Cursor.SetCursor(pressT, Vector2.zero, CursorMode.Auto);
//Cursor.SetCursor(pressT, hospot, CursorMode.Auto);
//hotspot:The offset from the top left of the texture to use as the target point (must // be within the bounds of the cursor). 默认选择Vector2.zero
//从纹理顶部的偏移量作为目标点(必须//位于游标的范围内)
}
else
{
Cursor.SetCursor(norT, Vector2.zero, CursorMode.Auto);
}
}

  简单说下, 上面的方法里实现了鼠标左键点击就会把鼠标的图设置成对应的图片。

代码里cursorMode = CursorMode.ForceSoftware;这个模式是说当鼠标改变了以后,鼠标移到Unity窗口外(例如你打开一个别的软件)Unity里的鼠标依然不会消失而且是设定的图标。

  若cursorMode = CursorMode.Auto这种模式,鼠标样式只有一个,当你移到Unity窗口外,Unity里就没有鼠标了。