169 lines
4.2 KiB
C#
169 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
|
public class InGameManager : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
private Volume gameVolume;
|
|
[SerializeField]
|
|
private Volume pausedVolume;
|
|
|
|
[SerializeField]
|
|
private bool isPaused = false;
|
|
private bool isTransitioning = false;
|
|
|
|
// [SerializeField]
|
|
//private float pauseTransitionDuration = 1f;
|
|
|
|
[SerializeField]
|
|
private Canvas gameCanvas;
|
|
[SerializeField]
|
|
private Canvas pausedCanvas;
|
|
[SerializeField]
|
|
private List<AudioSource> sounds = new List<AudioSource>();
|
|
private List<float> initSoundVolumes = new List<float>();
|
|
|
|
public bool IsPaused { get { return this.isPaused; } }
|
|
|
|
public void TogglePause()
|
|
{
|
|
if (!isTransitioning)
|
|
{
|
|
isPaused = !isPaused;
|
|
Cursor.visible = isPaused;
|
|
if (!isPaused)
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
else
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
}
|
|
StartCoroutine(pauseTransition());
|
|
|
|
}
|
|
|
|
}
|
|
public void UnPause()
|
|
{
|
|
if (!isTransitioning)
|
|
{
|
|
isPaused = false;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = isPaused;
|
|
StartCoroutine(pauseTransition());
|
|
}
|
|
|
|
}
|
|
public void Pause()
|
|
{
|
|
if (!isTransitioning)
|
|
{
|
|
isPaused = true;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = isPaused;
|
|
StartCoroutine(pauseTransition());
|
|
}
|
|
|
|
}
|
|
|
|
IEnumerator pauseTransition()
|
|
{
|
|
if (pausedCanvas.gameObject.activeInHierarchy && !isPaused)
|
|
{
|
|
pausedCanvas.gameObject.SetActive(false);
|
|
}
|
|
if (gameCanvas.gameObject.activeInHierarchy && isPaused)
|
|
{
|
|
gameCanvas.gameObject.SetActive(false);
|
|
}
|
|
|
|
this.isTransitioning = true;
|
|
yield return new WaitForSeconds(0);
|
|
this.isTransitioning = false;
|
|
print("Unpause canvas?" + isPaused+","+pausedCanvas.gameObject.activeInHierarchy);
|
|
if (!pausedCanvas.gameObject.activeInHierarchy && isPaused)
|
|
{
|
|
pausedCanvas.gameObject.SetActive(true);
|
|
|
|
}
|
|
if (!gameCanvas.gameObject.activeInHierarchy && !isPaused)
|
|
{
|
|
gameCanvas.gameObject.SetActive(true);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public void SetVolume(float volume)
|
|
{
|
|
for (int i = 0;i<sounds.Count;i++)
|
|
{
|
|
sounds[i].volume = initSoundVolumes[i]*volume;
|
|
}
|
|
}
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
isPaused = false;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = isPaused;
|
|
gameVolume.weight = 1;
|
|
pausedVolume.weight = 0;
|
|
gameCanvas.gameObject.SetActive(true);
|
|
pausedCanvas.gameObject.SetActive(false);
|
|
|
|
foreach (AudioSource source in sounds)
|
|
{
|
|
initSoundVolumes.Add(source.volume);
|
|
}
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (Input.GetButtonDown("Pause"))
|
|
{
|
|
this.TogglePause();
|
|
|
|
}
|
|
|
|
if (isTransitioning||true)
|
|
{
|
|
if (isPaused)
|
|
{
|
|
//transition into pause
|
|
|
|
//gameVolume.weight = Mathf.Lerp(gameVolume.weight, 0, Time.deltaTime);
|
|
//pausedVolume.weight = Mathf.Lerp(pausedVolume.weight, 1, Time.deltaTime);
|
|
|
|
//gameVolume.weight = gameVolume.weight < 0.1 ? 0 : gameVolume.weight;
|
|
//pausedVolume.weight = pausedVolume.weight > 0.9 ? 1 : pausedVolume.weight;
|
|
gameVolume.weight = 0;
|
|
pausedVolume.weight = 1;
|
|
|
|
}
|
|
else
|
|
{
|
|
//transition out of pause
|
|
|
|
gameVolume.weight = 1;
|
|
pausedVolume.weight = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ExitToMenu()
|
|
{
|
|
SceneManager.LoadScene(0);
|
|
}
|
|
}
|