即使我在退出菜单时调用了 Cursor.Visible 函数,但在退出暂停菜单时我的光标仍然可见

My Cursor Stays Visible When Exiting my Pause Menu Even Though I Have Called the Cursor.Visible Function When Exiting the Menu

我正在我的游戏中制作一个暂停菜单。我的游戏将光标锁定在某个位置,然后隐藏光标,以免分散用户的注意力。在我暂停游戏之前,这会按预期工作。光标变得可见(因为 Cursor.Visible 在暂停功能中设置为真)并且暂停菜单正常工作。但是,当我再次按退出键退出菜单时,光标在屏幕上的位置仍然可见。

我该如何解决这个问题?

谢谢,

这是我的代码:

using UnityEngine;

public class PlayerCameraRotation : MonoBehaviour
{
    Vector2 mouseDirection;      //Initialises Mouse Direction.                                                                    
    Vector2 smoothValue;         // Initialises The Smoothing Value.                                                                           
    private float sensitivity = 1.0F;     // Sets The Sensitivity To 1.                                                             
    private float smoothing = 2.0F;           // Sets Smoothing To 2.
    private GameObject cameraDirection;       // Initialises Camera Direction.
    Vector2 mouseD;
    private bool gamePause = false;

void Start()                                                                                      // Calls Once As Soon As The Program Is Started.
{
    cameraDirection = transform.parent.gameObject;                                                 // Rotates The Camera To Face The Same Direction As The Player.
}


void LateUpdate()                                                                                 // Updates Every Frame After All Other Updates Have Been Completed.
{
    if(gamePause == false)
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    mouseD = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    mouseD = Vector2.Scale(mouseD, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
    smoothValue.x = Mathf.Lerp(smoothValue.x, mouseD.x, 1f / smoothing);
    smoothValue.y = Mathf.Lerp(smoothValue.y, mouseD.y, 1f / smoothing);
    mouseDirection += smoothValue;
    mouseDirection.y = Mathf.Clamp(mouseDirection.y, -90f, 90f);

    transform.localRotation = Quaternion.AngleAxis(-mouseDirection.y, Vector3.right);
    cameraDirection.transform.localRotation = Quaternion.AngleAxis(mouseDirection.x, cameraDirection.transform.up);

    if (Input.GetKeyDown("escape"))
    {
        if(gamePause == true)
        {
            ResumeGame();
            gamePause = false;

        }
        else
        {
            PauseGame();
            gamePause = true;
        }
    }
}

private void ResumeGame()
{
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    smoothing = 2F;
}

private void PauseGame()
{
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    smoothing = 0F;
}
}

这里的问题似乎是,当您 运行 Unity Editor 中的代码时,光标不会消失,但是当构建相同的代码然后 运行 时,光标会消失按预期消失。此问题已通过构建项目和 运行ning .exe 文件解决。