2017-01-01 79 views
0

我想在我的统一WebGL游戏中添加一个事件监听器来更改Cursor.lockState。如何为Cursor.lockstate创建事件监听器?

如果光标被锁定,我想取消暂停游戏。如果我检测到游标已被解锁,我想暂停。

这里是我迄今为止

using UnityEngine; 
using System.Collections; 

public class Pause : MonoBehaviour { 

public RotateCamera rotateCamera; 
public GameObject pauseMenu; 
private bool paused; 

void Update() 
{ 
    if (paused) 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      UnpauseGame(); 
      return; 
     } 
    } 
    if (Cursor.lockState == CursorLockMode.Locked) 
    { 
     Debug.Log("CursorLockMode = Locked"); 
     UnpauseGame(); 
     return; 
    } 
    if (Cursor.lockState == CursorLockMode.Confined) 
    { 
     Debug.Log("CursorLockMode = Confined"); 
     UnpauseGame(); 
     return; 
    } 

    if (Cursor.lockState == CursorLockMode.None && paused == false) 
    { 
     Debug.Log("CursorLockMode = None"); 
     PauseGame(); 
    } 
} 

public void PauseGame() 
{ 
    Cursor.lockState = CursorLockMode.None; 
    Cursor.visible = true; 
    paused = true; 
    pauseMenu.SetActive(true); 
} 

public void UnpauseGame() 
{ 
    Cursor.lockState = CursorLockMode.Locked; 
    Cursor.visible = false; 
    paused = false; 
    pauseMenu.SetActive(false); 
} 
} 

问题是

  • 当试图取消暂停与鼠标按钮的比赛下来的游戏瞬间暂停本身。我相信该脚本检测到cursor.lockstate已解锁并立即重新暂停游戏。

为了解决这个问题,我想我需要创建一个事件来监听锁状态的变化并调用该方法。而不是每帧调用的方法。

回答

0

我想我需要创建一个事件来监听锁 状态的变化并调用该方法。而不是每个 帧被调用的方法。

这很简单。请勿直接使用Cursor API。围绕它包装另一个类,然后使用属性getter和setter来实现一个简单的事件系统。查看Unity的活动和代表教程here

的包装AdvanceCursor.cs

public class AdvanceCursor 
{ 
    //Not needed but included just because Cursor has a pulbic constructor 
    private Cursor cursor; 

    public AdvanceCursor() 
    { 
     cursor = new Cursor(); 
    } 

    public static CursorLockMode lockState 
    { 
     set 
     { 
      Cursor.lockState = value; 

      //Notify Event 
      if (OnLockStateChanged != null) 
      { 
       OnLockStateChanged(value); 
      } 
     } 

     get { return Cursor.lockState; } 
    } 

    public static bool visible 
    { 
     set 
     { 
      Cursor.visible = value; 

      //Notify Event 
      if (OnVisibleChanged != null) 
      { 
       OnVisibleChanged(value); 
      } 
     } 

     get { return Cursor.visible; } 
    } 

    public static void SetCursor(Texture2D texture, Vector2 hotspot, CursorMode cursorMode) 
    { 
     Cursor.SetCursor(texture, hotspot, cursorMode); 

     //Notify Event 
     if (OnTextureChanged != null) 
     { 
      OnTextureChanged(texture, hotspot, cursorMode); 
     } 
    } 

    /////////////////////////////////////////////EVENTS////////////////////////////////// 

    //Event For LockState 
    public delegate void cursorLockStateAction(CursorLockMode lockMode); 
    public static event cursorLockStateAction OnLockStateChanged; 

    //Event For visible 
    public delegate void cursorVisibleAction(bool visible); 
    public static event cursorVisibleAction OnVisibleChanged; 

    //Event For Texture Change 
    public delegate void cursorTextureAction(Texture2D texture, Vector2 hotspot, CursorMode cursorMode); 
    public static event cursorTextureAction OnTextureChanged; 
} 

用法

只需订阅事件。

public class CursorTest: MonoBehaviour 
{ 
    void Start() 
    { 
     AdvanceCursor.lockState = CursorLockMode.Locked; 
     AdvanceCursor.visible = true; 
     //AdvanceCursor.SetCursor(...); 
    } 

    //Subscribe to Events 
    void OnEnable() 
    { 
     AdvanceCursor.OnLockStateChanged += OnCursorLockStateChanged; 
     AdvanceCursor.OnVisibleChanged += OnCursorVisibleChanged; 
     AdvanceCursor.OnTextureChanged += OnCursorTextureChange; 
    } 

    //Un-Subscribe to Events 
    void OnDisable() 
    { 
     AdvanceCursor.OnLockStateChanged -= OnCursorLockStateChanged; 
     AdvanceCursor.OnVisibleChanged -= OnCursorVisibleChanged; 
     AdvanceCursor.OnTextureChanged -= OnCursorTextureChange; 
    } 

    void OnCursorLockStateChanged(CursorLockMode lockMode) 
    { 
     Debug.Log("Cursor State changed to: " + lockMode.ToString()); 
    } 

    void OnCursorVisibleChanged(bool visible) 
    { 
     Debug.Log("Cursor Visibility is now: " + visible); 
    } 

    void OnCursorTextureChange(Texture2D texture, Vector2 hotspot, CursorMode cursorMode) 
    { 
     Debug.Log("Cursor Texture Changed!"); 
    } 
} 
+0

嗨,谢谢你的回应。我需要直接监视Cursor.lockState。原因是Cursor.lockState正在被浏览器/ Unity webGL修改,我不能依靠它来使用你建议的'SetCursor'属性。我需要知道是否有方法可以将事件/委托添加到Cursor.lockstate。 – Jim

+0

“我不能依靠它来使用你建议的'SetCursor'属性。”我不明白你的评论。问题是关于lockState。我添加了可见和SetCursor作为奖励。这三个功能应该引发一个事件。你对这个解决方案有什么问题? – Programmer