public void OnPointerEnter(PointerEventData data)
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
我在游戏的每个UI上都设置了此方法(和退出方法)。但是,当我将其设置为null时,它将转到DEFAULT光标。现在使用Unity,我已经进入项目设置并为用户设置一个自定义光标。但是,当用户继续使用UI时,我希望光标Texture2D为“ None”,并将其设置为null只会将其设置为我开始使用的自定义光标。
我了解我可以执行“ void Start()”,只需将Cursor设置为我的自定义光标,并将默认光标设置为“ None”,但是由于此脚本正在游戏中的每个UI上运行,因此将我的默认设置设置为自定义设置,并在某些情况下使用c#将其设置为“无”更为合理。
答案 0 :(得分:0)
更新后的答案:
您无法做的事。
您将需要在“播放器设置”中将DefaultCursor设置为“无”,然后在要使用自定义光标时显式设置光标,从而允许SetCursor(null,Vector2.zero,CursorMode.Auto)引用系统指针光标。
创建CursorManager脚本并从需要更新游标的任何UI元素访问该脚本可能是值得的。在该脚本中,您可以编写如下方法:
public Texture2D GetCursor(string aCursor) {
switch (aCursor) {
case "none":
return null;
case "custompointer":
return ...
// Rest of implementation goes here
}
}
然后,为您的UI元素提供对该脚本的引用,并在需要时调用该方法。
public void OnPointerEnter(PointerEventData data)
{
Cursor.SetCursor(CursorManager.GetCursor("none"), Vector2.zero, CursorMode.Auto);
}
原始答案(无效)
下面的方法不起作用,因为尽管您可以通过代码设置PlayerSettings.defaultCursor,但不能在运行时对其进行更新或更改。它将在构建时设置defaultCursor。
在希望使用默认系统Cursor的OnPointerEnter方法上,需要在当前的SetCursor调用之前添加PlayerSettings.defaultCursor分配。我还没有测试过,但是以下应该可以工作。
PlayerSettings.defaultCursor = null;
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
然后,在OnPointerExit方法上将PlayerSettings.defaultCursor设置为自定义默认光标Texture2D。
答案 1 :(得分:0)
我同意HumanWrites。在“播放器设置”菜单中设置默认光标时,您将覆盖Unity None游标,并且在构建后实际上不存在。 如果我要使用自定义默认游标,并使用unity none游标作为UI焦点游标,则可以在MainController中设置setCursor,或者在任何UI中使用一次。像这样:
[SerializeField]
private Texture2D cursorTexture;
private static bool defaultCursorNotSet = true;
private void Awake()
{
if (defaultCursorNotSet)
{
defaultCursorNotSet = false;
SetCustomCursor();
}
}
public void OnPointerEnter(PointerEventData data)
{
SetNoneCursor();
}
public void OnPointerExit(PointerEventData data)
{
SetCustomCursor();
}
private void SetNoneCursor()
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
private void SetCustomCursor()
{
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto);
}
即使您可以通过静态方法加载customTuxture,也可以这样做:
public class CursorHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private static Texture2D cursorTexture;
static CursorHandler()
{
cursorTexture = ...
SetCustomCursor();
}
public void OnPointerEnter(PointerEventData data)
{
SetNoneCursor();
}
public void OnPointerExit(PointerEventData data)
{
SetCustomCursor();
}
private static void SetNoneCursor()
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
private static void SetCustomCursor()
{
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto);
}
}