Description: a script which controls AC's cursor lock state depending on what UI menus are currently visible. To use it put it on a gameobject in your hierarchy (or if possible, add it to the game engine prefab). It uses an AC global variable to remember the last state (and so the state will be remembered in a saved game).
using UnityEngine; using System.Collections.Generic; using AC; //using UnityEngine.SceneManagement; //put this in the a gameobject in the hiearchy. //set the AC GvarID which will hold the LockState //Set the list of menu names which will not block functionality while open. public class CursorLockManager : MonoBehaviour { [Header("List of overlooked AC menus")] public List<string> MenuTitles = new List<string> {"Hotspot","Subtitles", "InGame"}; [Space] [Header("Use debug mode?")] public bool DebugMode; private AC.PlayerInput _myInput; //private bool _wasInitialized; //private AC.PlayerMenus _playerMenus; private List<Menu> _myMenus; // Use this for initialization void Start () { //get the Player input component _myInput = AC.KickStarter.playerInput; if (DebugMode) Debug.Log("Script initialization. Setting Cursor State... "); //set initial cursor lock state _myInput.cursorIsLocked = true; _myMenus = AC.PlayerMenus.GetMenus(); //close unwanted menus. Useful when loading new scenes. CloseMenus(); //_wasInitialized = true; } void CloseMenus() { foreach (var _menu in _myMenus) { if (!MenuTitles.Contains(_menu.title)) { _menu.TurnOff(); } } } private void OnEnable() { //subscribe events EventManager.OnMenuTurnOn += OnMenuTurnOn; EventManager.OnMenuTurnOff += OnMenuTurnOff; //UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded; } private void OnDisable() { //Unsubscribe events EventManager.OnMenuTurnOn -= OnMenuTurnOn; EventManager.OnMenuTurnOff -= OnMenuTurnOff; //UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded; } private void OnMenuTurnOn(AC.Menu _menu, bool isInstant) { foreach (var menuName in MenuTitles) { //if the menu that was turned on is one in the exception //list then stop. if (menuName == _menu.title) { if (DebugMode) Debug.Log("Opened menu: " + _menu.title + " is in the cursorlock exception list. Ignoring..."); return; } } if (DebugMode) Debug.Log("Menu " + _menu.title + " opened. Unlocking cursor."); //make sure the cursor is unlocked _myInput.cursorIsLocked = false; } private void OnMenuTurnOff(AC.Menu _menu, bool isInstant) { foreach (var menuName in MenuTitles) { //if the menu that was turned off is one in the exception //list then stop. if (menuName == _menu.title) { if (DebugMode) Debug.Log("Closed menu: " + _menu.title + " is in the cursorlock exception list. Ignoring..."); return; } } if (DebugMode) Debug.Log("Menu " + _menu.title + " closed. Locking cursor."); //make sure the cursor is locked _myInput.cursorIsLocked = true; //switch the Gvar to work as the script's memory } }