当鼠标悬停在按钮上时更改光标图标

Change icon of cursor when mouse hover at button

我正在开发一个游戏,当光标指向一个按钮时,我需要改变光标的图标,当不指向按钮时,保持回到正常状态。到目前为止,我已经这样做了,但没有什么好处,有什么建议我哪里出错了吗? 我将此脚本附加到按钮...

using UnityEngine;
using System.Collections;

public class cursor : MonoBehaviour {

  Vector2 mouse;
  int w =32;
  int h=32;
  public Texture2D cursor1;
  // Use this for initialization
  void Start () {
    Cursor.visible = true;
  }

  // Update is called once per frame
  void Update () {
    mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
  }
  void OnMouseOver()
  {
    Cursor.SetCursor (cursor1, Vector2.zero, CursorMode.Auto);
  }
  void OnMouseExit()
  {
    Cursor.SetCursor (null,Vector2.zero,CursorMode.Auto);
  }
}

你在 Unity Doc 上检查过这个 link 了吗:

https://docs.unity3d.com/ScriptReference/Cursor.SetCursor.html

您是否根据需要在检查器中配置了纹理?

The texture to use for the cursor or null to set the default cursor. Note that a texture needs to be imported with "Read/Write enabled" in the texture importer (or using the "Cursor" defaults), in order to be used as a cursor.

您需要使用 UI 系统的事件触发器来更改系统图标或任何基于输入的交互(这很容易实现)。

首先将鼠标悬停和鼠标退出功能更改为 public。 将事件系统添加到您希望光标在悬停时更改的按钮 添加组件->事件触发器->添加新事件类型->

Here there are multiple events you can choose from but you need only on Pointer Enter and Pointer Exit

所以select你的指针进入 & 指针退出 -> 单击“+”符号 -> 将光标脚本拖放到这两个位置,并从脚本中 select 各自的函数并执行程序。

另一种方法是在您的光标脚本中实现事件系统功能并将其放置在按钮对象上,但我认为第一种方法干净且易于实现。 Event Trigger Scripted Implementation 1

Note: Your old mouse over will work on objects on 3D world which has colliders and not as UI so that is why your cursor script is not working for buttons as buttons are in UI space. Though you can make them work in UI also using 3D/2D colliders but you will have to go with raycasters which is a bit long method to implement. I would rather go with simple ones unless required

Event Trigger Info

希望这能解决您的问题。