PauseMenu.cs 2.5 KB
Newer Older
Isa's avatar
Isa committed
1
2
3
4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
Isa's avatar
Isa committed
5
using UnityEngine.SceneManagement;
Isa's avatar
Isa committed
6
7
8
9
10
11
12
13
using UnityEngine.UI;

public class PauseMenu : MonoBehaviour
{
	public GameObject pauseMenu, optionsMenu;
	private EventSystem _myEventSystem;
	public Button firstSelect;
	private GameObject _backScreen, _currentScreen;
Isa's avatar
Isa committed
14
15
	private bool isPaused = false;
	private GameObject _old, _new;
Isa's avatar
Isa committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    // Start is called before the first frame update
    void Start()
    {
	    _myEventSystem = EventSystem.current;
    }

    IEnumerator highlightBtn()
    {
	    _myEventSystem.SetSelectedGameObject(null);
	    yield return null;
	    _myEventSystem.firstSelectedGameObject = firstSelect.gameObject;
	    _myEventSystem.SetSelectedGameObject(_myEventSystem.firstSelectedGameObject);
    }

    // Update is called once per frame
    void Update()
    {
Isa's avatar
Isa committed
33
34
35
36
37
	    _new = _myEventSystem.currentSelectedGameObject;
	    if (_new != null)
	    {
		    _old = _new;
	    }
Isa's avatar
Isa committed
38
39
40
41
42
43
44
45
46
47
48
        if(Input.GetButtonDown ("Cancel") && isPaused == true && _backScreen == null)
        {
	        Resume();
        } else if (Input.GetButtonDown ("Cancel") && isPaused == false && _backScreen == null)
        {
	        Pause();
        }
        else if (Input.GetButtonDown ("Cancel") && isPaused == true && _backScreen != null)
        {
	        _backScreen.SetActive(true);
	        _currentScreen.SetActive(false);
Isa's avatar
Isa committed
49
	        //firstSelect.Select();
Isa's avatar
Isa committed
50
51
	        _backScreen = null;
        }
Isa's avatar
Isa committed
52
53
54
55
56

        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
	        _myEventSystem.SetSelectedGameObject(_old);
        }
Isa's avatar
Isa committed
57
58
59
60
61
62
63
    }

    public void SetBackScreen(GameObject bScreen)
    {
	    _backScreen = bScreen;
    }

Isa's avatar
Isa committed
64
65
66
67
68
69
70
71
72
73
    public void ResetBackScreen()
    {
	    _backScreen = null;
    }

    public void selectBtn(Button optionBtn)
    {
	    optionBtn.Select();
    }

Isa's avatar
Isa committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    public void SetCurrentScreen(GameObject cScreen)
    {
	    _currentScreen = cScreen;
    }

    public void Resume()
    {
	    isPaused = false;
	    pauseMenu.SetActive(false);
	    optionsMenu.SetActive(false);
	    Time.timeScale = 1f;
    }

    public void Pause()
    {
	    isPaused = true;
	    pauseMenu.SetActive(true);
	    Time.timeScale = 0f;
	    StartCoroutine("highlightBtn");
    }
Isa's avatar
Isa committed
94
95
96
97
98
99
100
101
102
103

    public void SwitchToMouse()
    {
    	_myEventSystem.GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
    }

    public void LoadMenu()
    {
	    SceneManager.LoadScene("MainMenu");
    }
Isa's avatar
Isa committed
104
}