Showing
7 changed files
with
213 additions
and
0 deletions
Assets/Packages/GlobaTools/DebugConsole.meta
0 → 100644
| 1 | +using UnityEngine; | ||
| 2 | +using System.Collections; | ||
| 3 | +using UnityEngine.UI; | ||
| 4 | +using System.Collections.Generic; | ||
| 5 | + | ||
| 6 | +[assembly: System.Reflection.AssemblyVersion("1.0.*")] | ||
| 7 | +public class DebugConsole : MonoBehaviour | ||
| 8 | +{ | ||
| 9 | + // Use this event to reset junk in your game to their initial states. | ||
| 10 | + public static event System.Action OnReset; | ||
| 11 | + public static int fps { get; private set; } | ||
| 12 | + | ||
| 13 | + [Header("Spawning")] | ||
| 14 | + [SerializeField] | ||
| 15 | + private CanvasGroup _container; | ||
| 16 | + [SerializeField] | ||
| 17 | + private KeyCode toggleKey = KeyCode.D; | ||
| 18 | + [SerializeField] | ||
| 19 | + private int _tapsToSpawn = 3; | ||
| 20 | + [SerializeField] | ||
| 21 | + private float _totalTapTime = 1; | ||
| 22 | + [Header("Info")] | ||
| 23 | + [SerializeField] | ||
| 24 | + private Text _appInfo; | ||
| 25 | + [SerializeField] | ||
| 26 | + private Text _fpsCounter; | ||
| 27 | + [SerializeField] | ||
| 28 | + private float _fpsUpdateFrequency = 0.5f; | ||
| 29 | + [SerializeField] | ||
| 30 | + private Text _debugTextNode; | ||
| 31 | + | ||
| 32 | + private static DebugConsole _inst = null; | ||
| 33 | + | ||
| 34 | + void Awake() | ||
| 35 | + { | ||
| 36 | + if (_inst != null) | ||
| 37 | + { | ||
| 38 | + Destroy(gameObject); | ||
| 39 | + return; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + _inst = this; | ||
| 43 | + DontDestroyOnLoad(this); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + private void Start () | ||
| 47 | + { | ||
| 48 | + if (Application.isMobilePlatform) | ||
| 49 | + GetComponent<CanvasScaler>().scaleFactor = 3; | ||
| 50 | + | ||
| 51 | + Application.logMessageReceived += OnLogMessageReceived; | ||
| 52 | + _debugTextNode.text = "Debug Console Window"; | ||
| 53 | + _debugTextNode.color = Color.cyan; | ||
| 54 | + | ||
| 55 | + Hide(); | ||
| 56 | + | ||
| 57 | + string[] appInfo = new string[] | ||
| 58 | + { | ||
| 59 | + Application.companyName, | ||
| 60 | + Application.productName, | ||
| 61 | + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() | ||
| 62 | + }; | ||
| 63 | + _appInfo.text = string.Format("{0} {1} - v{2}", appInfo); | ||
| 64 | + | ||
| 65 | + StartCoroutine(FPS()); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + private IEnumerator FPS() | ||
| 69 | + { | ||
| 70 | + while (true) | ||
| 71 | + { | ||
| 72 | + int lastFrameCount = Time.frameCount; | ||
| 73 | + float lastTime = Time.realtimeSinceStartup; | ||
| 74 | + yield return new WaitForSeconds(_fpsUpdateFrequency); | ||
| 75 | + float timeSpan = Time.realtimeSinceStartup - lastTime; | ||
| 76 | + int frameCount = Time.frameCount - lastFrameCount; | ||
| 77 | + | ||
| 78 | + fps = Mathf.RoundToInt(frameCount / timeSpan); | ||
| 79 | + _fpsCounter.text = fps.ToString() + " fps"; | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + private List<Text> _logItems = new List<Text>(); | ||
| 84 | + private const int _maxLogItems = 50; | ||
| 85 | + | ||
| 86 | + private void OnLogMessageReceived(string condition, string stackTrace, LogType type) | ||
| 87 | + { | ||
| 88 | + Text newText = Instantiate(_debugTextNode) as Text; | ||
| 89 | + newText.transform.SetParent(_debugTextNode.transform.parent); | ||
| 90 | + newText.transform.localScale = _debugTextNode.transform.localScale; | ||
| 91 | + newText.text = string.Format("{0}\n{1}", condition, stackTrace); | ||
| 92 | + | ||
| 93 | + switch (type) | ||
| 94 | + { | ||
| 95 | + case LogType.Log: | ||
| 96 | + newText.color = Color.white; | ||
| 97 | + break; | ||
| 98 | + case LogType.Warning: | ||
| 99 | + newText.color = Color.yellow; | ||
| 100 | + break; | ||
| 101 | + case LogType.Exception: | ||
| 102 | + newText.color = Color.red; | ||
| 103 | + break; | ||
| 104 | + case LogType.Error: | ||
| 105 | + newText.color = Color.red; | ||
| 106 | + break; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + _logItems.Add(newText); | ||
| 110 | + | ||
| 111 | + if (_logItems.Count > _maxLogItems) | ||
| 112 | + { | ||
| 113 | + Destroy(_logItems[0].gameObject); | ||
| 114 | + _logItems.RemoveAt(0); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + public void DebugTap() | ||
| 119 | + { | ||
| 120 | + if (_container.gameObject.activeSelf) | ||
| 121 | + Hide(); | ||
| 122 | + else | ||
| 123 | + _tapCount++; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + private int _tapCount; | ||
| 127 | + private float _currentTapTime; | ||
| 128 | + | ||
| 129 | + private void Update() | ||
| 130 | + { | ||
| 131 | + if (Input.GetKeyDown(toggleKey)) | ||
| 132 | + Toggle(); | ||
| 133 | + | ||
| 134 | + if (_tapCount == 0) return; | ||
| 135 | + | ||
| 136 | + _currentTapTime += Time.deltaTime; | ||
| 137 | + | ||
| 138 | + if (_currentTapTime >= _totalTapTime) | ||
| 139 | + { | ||
| 140 | + _currentTapTime = 0; | ||
| 141 | + _tapCount = 0; | ||
| 142 | + } | ||
| 143 | + else if (_tapCount >= _tapsToSpawn) | ||
| 144 | + { | ||
| 145 | + _tapCount = 0; | ||
| 146 | + _currentTapTime = 0; | ||
| 147 | + Toggle(); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + public void Toggle() | ||
| 152 | + { | ||
| 153 | + if (_container.gameObject.activeSelf) | ||
| 154 | + Hide(); | ||
| 155 | + else | ||
| 156 | + Show(); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + public void Hide() | ||
| 160 | + { | ||
| 161 | + _container.gameObject.SetActive(false); | ||
| 162 | + _container.interactable = false; | ||
| 163 | + _container.blocksRaycasts = false; | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + public void Show() | ||
| 167 | + { | ||
| 168 | + _container.gameObject.SetActive(true); | ||
| 169 | + _container.interactable = true; | ||
| 170 | + _container.blocksRaycasts = true; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + public void ResetApp() | ||
| 174 | + { | ||
| 175 | + if (OnReset != null) OnReset(); | ||
| 176 | + | ||
| 177 | + UnityEngine.SceneManagement.SceneManager.LoadScene(0); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + private void OnDestroy() | ||
| 181 | + { | ||
| 182 | + Application.logMessageReceived -= OnLogMessageReceived; | ||
| 183 | + } | ||
| 184 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
No preview for this file type
No preview for this file type
No preview for this file type
-
Please register or sign in to post a comment