1
2
|
public static EventSystem.Dispatcher Events = new EventSystem.Dispatcher();
public FiniteStateMachine FSM = new FiniteStateMachine();
|
1
2
3
4
|
FSM.Register("MainMenu", new MainMenuUI());
FSM.Register("AudioMenu", new AudioMenuUI());
FSM.Register("MainGame", new MainGame(FSM));
FSM.Register("QuitGame", new QuitGameUI());
|
1
|
FSM.EntryPoint("MainMenu");
|
1
2
3
|
FSM.State("MainMenu").On("OPEN_AUDIO").Enter("AudioMenu")
.On("PLAY_GAME").Push("MainGame")
.On("QUIT_GAME").Enter("QuitGame");
|
1
2
3
4
5
6
|
FSM.State("QuitGame").On("PROCESS_QUIT", delegate(bool sure) {
if (sure) {
gameObject.GetComponent
Camera.main.backgroundColor = Color.black;
} else { FSM.Enter("MainMenu"); }
});
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using UnityEngine;
using System.Collections;
class MainGame : MenuUI, IState {
protected FiniteStateMachine FSM;
protected float Score = 0;
public MainGame(FiniteStateMachine parentMachine) {
FSM = parentMachine;
}
public void OnEnter(string prevState) {
Score = 0;
}
public void OnExit(string nextState) {
}
public void OnUpdate() {
}
public override void DoGUI() {
if (GUILayout.Button("Quit / Back To Menu", GUILayout.Width(Screen.width))) {
FSM.Pop();
}
GUILayout.Space(25);
GUILayout.Label("The waiting game!");
GUILayout.Space(25);
GUILayout.Label("CurrentScore: " + System.Convert.ToInt32(Score));
Score += Time.deltaTime;
}
}
|
1
|
SM.State("AudioMenu").On("BACK_TO_MENU").Enter("MainMenu");
|
1
2
3
4
5
6
|
Events.On("OpenMainGame", delegate() { FSM.CurrentState.Trigger("PLAY_GAME"); });
Events.On("OpenAudioMenu", delegate() { FSM.CurrentState.Trigger("OPEN_AUDIO"); });
Events.On("QuitGame", delegate() { FSM.CurrentState.Trigger("QUIT_GAME"); });
Events.On("ConfirmQuit", delegate() { FSM.CurrentState.Trigger("PROCESS_QUIT", true); });
Events.On("CancelQuit", delegate() { FSM.CurrentState.Trigger("PROCESS_QUIT", false); });
Events.On("BackToMenu", delegate() { FSM.CurrentState.Trigger("BACK_TO_MENU", false); });
|
1
2
3
|
public void Update() {
FSM.Update();
}
|
1
2
3
4
5
6
|
void OnGUI() {
if (FSM.CurrentState == null)
return;
MenuUI ui = (MenuUI)FSM.CurrentState.StateObject;
ui.DoGUI();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
using UnityEngine;
using System.Collections;
class AudioMenuUI : MenuUI, IState {
float volume = 0.5f;
float backupVolume = 0.0f;
public void OnEnter(string prevState) {
backupVolume = volume;
}
public void OnExit(string nextState) {
}
public void OnUpdate() {
}
public override void DoGUI() {
GUILayout.Space(25.0f);
volume = GUILayout.HorizontalSlider(volume, 0.0f, 1.0f, GUILayout.Width(Screen.width));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label("Volume: " + System.Convert.ToInt32(volume * 100.0f) + " %");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Cancel", GUILayout.Height(75.0f))) {
volume = backupVolume;
TestUIState.Events.Trigger("BackToMenu");
}
if (GUILayout.Button("Confirm", GUILayout.Height(75.0f))) {
TestUIState.Events.Trigger("BackToMenu");
}
GUILayout.EndHorizontal();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using UnityEngine;
using System.Collections;
public class MainMenuUI : MenuUI, IState {
public void OnEnter(string prevState) {
}
public void OnExit(string nextState) {
}
public void OnUpdate() {
}
public override void DoGUI() {
if (GUILayout.Button("Play Game", GUILayout.Width(Screen.width), GUILayout.Height(Screen.height / 3))) {
TestUIState.Events.Trigger("OpenMainGame");
}
if (GUILayout.Button("Audio Menu", GUILayout.Width(Screen.width), GUILayout.Height(Screen.height / 3))) {
TestUIState.Events.Trigger("OpenAudioMenu");
}
if (GUILayout.Button("Quit Game", GUILayout.Width(Screen.width), GUILayout.Height(Screen.height / 3))) {
TestUIState.Events.Trigger("QuitGame");
}
}
}
|
1
2
3
4
5
6
|
using UnityEngine;
using System.Collections;
public class MenuUI {
public virtual void DoGUI() {
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using UnityEngine;
using System.Collections;
class QuitGameUI : MenuUI, IState {
public void OnEnter(string prevState) {
}
public void OnExit(string nextState) {
}
public void OnUpdate() {
}
public override void DoGUI() {
GUILayout.BeginHorizontal();
if (GUILayout.Button("Confirm", GUILayout.Width(Screen.width / 2), GUILayout.Height(Screen.height))) {
TestUIState.Events.Trigger("ConfirmQuit");
}
if (GUILayout.Button("Cancel", GUILayout.Width(Screen.width / 2), GUILayout.Height(Screen.height))) {
TestUIState.Events.Trigger("CancelQuit");
}
GUILayout.EndHorizontal();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
using UnityEngine;
using System.Collections;
public class TestUIState : MonoBehaviour {
public static EventSystem.Dispatcher Events = new EventSystem.Dispatcher();
public FiniteStateMachine FSM = new FiniteStateMachine();
public void Awake() {
FSM.Register("MainMenu", new MainMenuUI());
FSM.Register("AudioMenu", new AudioMenuUI());
FSM.Register("MainGame", new MainGame(FSM));
FSM.Register("QuitGame", new QuitGameUI());
FSM.EntryPoint("MainMenu");
FSM.State("MainMenu").On("OPEN_AUDIO").Enter("AudioMenu")
.On("PLAY_GAME").Push("MainGame")
.On("QUIT_GAME").Enter("QuitGame");
FSM.State("QuitGame").On("PROCESS_QUIT", delegate(bool sure) {
if (sure) {
gameObject.GetComponent
Camera.main.backgroundColor = Color.black;
} else { FSM.Enter("MainMenu"); }
});
FSM.State("AudioMenu").On("BACK_TO_MENU").Enter("MainMenu");
Events.On("OpenMainGame", delegate() { FSM.CurrentState.Trigger("PLAY_GAME"); });
Events.On("OpenAudioMenu", delegate() { FSM.CurrentState.Trigger("OPEN_AUDIO"); });
Events.On("QuitGame", delegate() { FSM.CurrentState.Trigger("QUIT_GAME"); });
Events.On("ConfirmQuit", delegate() { FSM.CurrentState.Trigger("PROCESS_QUIT", true); });
Events.On("CancelQuit", delegate() { FSM.CurrentState.Trigger("PROCESS_QUIT", false); });
Events.On("BackToMenu", delegate() { FSM.CurrentState.Trigger("BACK_TO_MENU", false); });
}
public void Update() {
FSM.Update();
}
void OnGUI() {
if (FSM.CurrentState == null)
return;
MenuUI ui = (MenuUI)FSM.CurrentState.StateObject;
ui.DoGUI();
}
}
|