-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoEventOnInput.cs
More file actions
139 lines (125 loc) · 3.5 KB
/
DoEventOnInput.cs
File metadata and controls
139 lines (125 loc) · 3.5 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
using UnityEngine;
public class DoEventOnInput : MonoBehaviour
{
[Header("Input Settings")]
[SerializeField] private KeyCode _InputKey;
public enum InputOptions { GetKeyDown, GetKeyUp, GetKey }
[SerializeField] private InputOptions _InputOption = InputOptions.GetKeyDown;
[Header("Event")]
[SerializeField] private UnityEvent _Event = null;
[Header("Other Options")]
[SerializeField] private bool _OnStart = false;
[SerializeField] private bool _OnUpdate = false;
[SerializeField] private bool _OnAnyKey = false;
private bool _AsyncLoading = false;
void Start()
{
if (_OnStart)
DoEvents();
}
void Update()
{
if (_OnUpdate)
DoEvents();
if (_OnAnyKey)
if (Input.anyKey)
DoEvents();
switch(_InputOption)
{
case InputOptions.GetKeyDown:
if (Input.GetKeyDown(_InputKey))
_Event.Invoke();
break;
case InputOptions.GetKeyUp:
if (Input.GetKeyUp(_InputKey))
_Event.Invoke();
break;
case InputOptions.GetKey:
if (Input.GetKey(_InputKey))
_Event.Invoke();
break;
}
}
private void DoEvents()
{
_Event.Invoke();
}
//Set Object true/false
public void SetGameobject_InActive(GameObject targetobject)
{
targetobject.SetActive(false);
}
public void SetGameobject_Active(GameObject targetobject)
{
targetobject.SetActive(true);
}
public void SetGameObject_Negative(GameObject targetobject)
{
if (targetobject.activeSelf)
targetobject.SetActive(false);
else
targetobject.SetActive(true);
}
//Load/Reload Scenes
public void LoadScene(int sceneid)
{
SceneManager.LoadScene(sceneid);
}
public void LoadScene(string scenename)
{
SceneManager.LoadScene(scenename);
}
public void ReloadScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void AsyncReloadScene()
{
if (!_AsyncLoading)
{
_AsyncLoading = true;
StartCoroutine(LoadSceneAsync(SceneManager.GetActiveScene().buildIndex));
}
}
public void AsyncLoadScene(int sceneid)
{
if (!_AsyncLoading)
{
_AsyncLoading = true;
StartCoroutine(LoadSceneAsync(sceneid));
}
}
public void AsyncLoadScene(string scenename)
{
if (!_AsyncLoading)
{
_AsyncLoading = true;
StartCoroutine(LoadSceneAsync(scenename));
}
}
private IEnumerator LoadSceneAsync(string scenename)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(scenename);
while (!asyncLoad.isDone)
{
yield return null;
}
}
private IEnumerator LoadSceneAsync(int sceneid)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneid);
while (!asyncLoad.isDone)
{
yield return null;
}
}
//Quit
public void Quit()
{
Application.Quit();
}
}