Page 1 of 3

[SOLVED] Show/Hide mouse on ESC for menu

Posted: Mon Mar 14, 2016 5:44 am
by supadupa64
I want to hide mouse after menu closes. I don't use the mouse during game play and I have a script to show/hide for dialogue, but when I push ESC the mouse shows which is great, but then it stays.

The problem is I use ESC to exit edit play mode so I can stop the play or pause or whatever outside the game screen. Unless of course I used ESC to toggle mouse hide...

How can I set this up?

Re: Show/Hide mouse on ESC for menu

Posted: Mon Mar 14, 2016 9:41 am
by Tony Li
The example menu provided by the FeatureDemo.cs script is really just meant to drive the example scenes, although some developers have used it as a starting point for their own menu scripts. You could edit the script and hide the cursor in the SetMenuStatus method.

However, instead of that, I suggest using the Dialogue System Menu Template Prefab available on the Dialogue System Extras page (under the Extras section). It's designed for use in real games, includes an options menu and menus for slot-based saving and loading, and doesn't require any scripting.

Re: Show/Hide mouse on ESC for menu

Posted: Mon Mar 14, 2016 11:01 am
by supadupa64
I'll check it out. Thanks

Re: Show/Hide mouse on ESC for menu

Posted: Mon Apr 25, 2016 10:42 pm
by supadupa64
Ok, so my mouse cursor doesn't show during gameplay, which is great. It shows during conversations and when I hit ESC. That's all great, the only thing is that if I don't select "resume" or "close" or something to get back to the game and use "ESC" instead to close out of that stuff the mouse cursor stays on indefinitely. I want it to go away when I hit ESC, like an on/off switch. Is that something that is already a built in feature?

Re: Show/Hide mouse on ESC for menu

Posted: Tue Apr 26, 2016 9:28 am
by Tony Li
Yes. I'll post a screenshot later today to show how to check/set this up.

Re: Show/Hide mouse on ESC for menu

Posted: Tue Apr 26, 2016 9:08 pm
by Tony Li
In your Start scene, inspect the UI GameObject. It should have a Pause component and a More Pause component, among other components.

Image

Make sure the "On Do Pause" event calls MorePause.ShowCursor and the "On Un Pause" event calls MorePause.RestorePreviousCursorState. (In the screenshot above, which is from a different example project, I've also added a couple more event handlers that hide the Dialogue System's Selector and pause UFPS. You don't need to add these to your project, of course.)

As long as MorePause.ShowCursor is called when the pause menu opens, and MorePause.RestorePreviousCursorState is called when the pause menu closes, the cursor should behave like you want. It shouldn't matter how the pause menu closes -- by clicking a UI button or pressing Escape.

If it still doesn't work, maybe the Escape key is tied to something else that's showing the cursor. Here are two options in that case:

1. Instead of restoring the previous cursor state, just hide it. Add this code beneath the ShowCursor() method in MorePause.cs:

Code: Select all

        public void HideCursor()
        {
            Tools.ShowCursor(false);
            Tools.LockCursor(true);
        } 
Then in the "On Un Pause" event change MorePause.RestorePreviousCursorState to HideCursor. This will hide the cursor when the pause menu closes, regardless of whether it was hidden before the pause menu or not.


2. Or, temporarily change the "Cancel" button to a key other than Escape. Select Edit > Project Settings > Input. Then find the Cancel entry that has "escape" as the positive key. Change this to something else, like F1. When you press F1 during play, it should open and close the menu. Try this out and see if it makes a difference. If so, you'll have to figure out what other script is messing with the cursor and causing a conflict with MorePause.

Re: Show/Hide mouse on ESC for menu

Posted: Wed Apr 27, 2016 2:19 am
by supadupa64
I tried all those ways just now. I set my cancel button to P and the pause menu worked great. Cursor showed and went away on P toggle. If I hit ESC it shows and stays on indefinitely. I'll look around.

Actually something interesting I did find out. If I check the box "Deactivate during conversation" (disables the TPC controls) on the script "Dialogue System Third Person Controller Bridge," the cursor stays on indefinitely after I start a conversation with an NPC. I think since Dialogues systems think the conversation is ongoing (since it's a quest) the controls for TPC cursor are disabled (I use TPC to disable cursor during game play)? TPC uses a unity input script to disable the cursor. I'm just guessing, but it might have something to do with this?

Re: Show/Hide mouse on ESC for menu

Posted: Wed Apr 27, 2016 9:59 am
by Tony Li
That's good info! Let's try to isolate the issue so we can get it resolved. Since the pause menu works correctly with the P key, do you think the pause menu has anything to do with the cursor issue? When you press Escape, it shouldn't do anything with the pause menu now, correct?

Does anything else happen when you press Escape, or does just the cursor appear? Does the same issue also happen in a build (versus playing the editor)?

Let's first see if we can get the cursor working exactly the way you want without involving conversations. We can tackle conversations and cursors later.

Re: Show/Hide mouse on ESC for menu

Posted: Thu Apr 28, 2016 2:50 am
by supadupa64
I've tried a bunch of stuff and made a bunch of builds to try and get the cursor to go away when I hit ESC. Seems like an easy thing to solve, but apparently not.

Right now for some reason when I change the input cancel key to "p" or something else it doesn't work. Not sure what that's all about. :shock:

I don't know if this matters, but the pause menu never appears in the editor when I go into play mode. I have to make a build every time i want to see if the menu actually works. If I hit play while in editor mode, once I push ESC, the cursor stays on indefinitely. Although I do have a random hide/show cursor script that can hide/show cursor during editor play which works, but then gets all messed up when I start using the mouse since there are hide/show cursor commands coming from dialogue and the pause menu.

Here is that hide/show mouse script I have: (I was testing this script to see if it solved the problem)

Code: Select all

    using UnityEngine;
    using System.Collections;
     
    public class HideLockMouse : MonoBehaviour {
     
        public bool lockCursor = true;
     
        void Update ()
        {
     
            // pressing esc toggles between hide/show
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                lockCursor = !lockCursor;
            }
     
            Cursor.lockState = lockCursor?CursorLockMode.Locked:CursorLockMode.None;
            Cursor.visible = !lockCursor;
        }
    }
I actually is the closest thing to hide/show cursor when I hit ESC. It works great, but the second I go into a dialogue, the dialogue makes the cursor appear and then reverses the script cursor to go away when I hit ESC. It reverses it which makes it useless. I don't know if any of that is going to help this.

As a side note, I don't know if this has anything to do with this, but I have my main scene where the whole game takes place and I use another separate scene for my menu. So when I build my game, it's:

Scene1: splash screen
Scene2: main menu which also holds pause menu
Scene3: game

The current situation is everything works unless the ESC it pushed a second time (I think in any situation), which make the cursor stay on indefinitely.

Re: Show/Hide mouse on ESC for menu

Posted: Thu Apr 28, 2016 9:34 am
by supadupa64
Hey, so I was trying out a couple of the dialogue system demos.

Here's what I found: The cursor always shows. Is that supposed to happen? The menu comes up great during editor play mode. I can't get to happen when I use editor play. I can only get the ui to pop up if I build the game and play it. Anyway, that's not a huge deal, but maybe one of those things is something?