Skip to content

Commit

Permalink
hide cursor in levels but keep it visible in the pause menu and the m…
Browse files Browse the repository at this point in the history
…ain menu
  • Loading branch information
user5522 committed Aug 14, 2024
1 parent ff283fd commit 38b3ca6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Assets/Scripts/PauseMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PauseMenu : MonoBehaviour
Expand All @@ -16,13 +14,14 @@ void Update()
Time.timeScale = 0;
isPaused = true;
pauseMenu.SetActive(true);
SettingsManager.SetPauseState(true);
return;
}

isPaused = false;
SettingsManager.SetPauseState(false);
pauseMenu.SetActive(false);
Time.timeScale = 1;

}
}
}
}
37 changes: 37 additions & 0 deletions Assets/Scripts/SettingsManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;

public class SettingsManager : MonoBehaviour
{
private static bool isPaused = false;

private void Awake()
{
DontDestroyOnLoad(gameObject);
Expand All @@ -10,6 +13,17 @@ private void Awake()
private void Start()
{
LoadSettings();
SceneManager.sceneLoaded += OnSceneLoaded;
}

private void Update()
{
UpdateCursorVisibility();
}

private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}

private void LoadSettings()
Expand All @@ -31,4 +45,27 @@ private void LoadSettings()
Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreenMode);
}
}

private void UpdateCursorVisibility()
{
if (SceneManager.GetActiveScene().buildIndex == 0)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
return;
}

Cursor.visible = isPaused;
Cursor.lockState = isPaused ? CursorLockMode.None : CursorLockMode.Locked;
}

private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
UpdateCursorVisibility();
}

public static void SetPauseState(bool paused)
{
isPaused = paused;
}
}

0 comments on commit 38b3ca6

Please sign in to comment.