简体   繁体   中英

Showing menu make the cursor disappear

The cursor disappears when making a single click, like so:

在此处输入图片说明

How to reproduce:

  • get the Game Jam Menu template: https://www.assetstore.unity3d.com/en/#!/content/40465
  • Add another scene with an FPS controller imported from the standard assets.
  • Tick the "change scenes" in the UI -> start options component.

  • Press escape and touch the slider, any single click will make the cursor disappear.

It looks like this: https://imgur.com/a/hefmP

If I tick off the mouse look -> lock cursor in the FPS controller then this doesn't happen, but I also see the cursor in my FPS game.

I tried to add Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; and the opposite when pausing and unpausing but it doesn't help. Probably because the MouseLook.cs is also updating them in:

在此处输入图片说明

I'd rather not couple the FPSController and the pause menu and they're also in different scenes.

So how else can I unlock the cursor when entering the pause menu? Is there an event pattern that can be used here?

It also darkens the scene, but that's for another question.

It's been modified by the MouseLook script which is used in the FirstPersonController script. One solution is to modify the MouseLook script but a simple Asset update would kill that modification.

A proper solution to this problem is to disable the FirstPersonController script when you open up any menu and then enable it back when you close that menu. Also, when you disable it, manually set Cursor.lockState to None and set the cursor to visible.

Below is a simple function that will handle that. Each of your menu open and close button should be linked to the function below. When you call it, pass false to it with the open button and true to it with the close button :

void enableFPS(bool enable)
{
    GameObject fpsObj = GameObject.Find("FPSController");
    FirstPersonController fpsScript = fpsObj.GetComponent<FirstPersonController>();

    if (enable)
    {
        //Enable FPS script
        fpsScript.enabled = true;
    }
    else
    {
        //Disable FPS script
        fpsScript.enabled = false;
        //Unlock Mouse and make it visible
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM