|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +public class DebugVar : MonoBehaviour |
| 6 | +{ |
| 7 | + static DebugVar instance; |
| 8 | + |
| 9 | + public static DebugVar main |
| 10 | + { |
| 11 | + get |
| 12 | + { |
| 13 | + if (instance == null) |
| 14 | + { |
| 15 | + instance = GameObject.FindObjectOfType<DebugVar>(); |
| 16 | + } |
| 17 | + |
| 18 | + if (instance == null) |
| 19 | + { |
| 20 | + instance = new GameObject("DebugVar").AddComponent<DebugVar>(); |
| 21 | + } |
| 22 | + return instance; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + [System.Serializable] |
| 27 | + public class debugObject |
| 28 | + { |
| 29 | + public Object obj; |
| 30 | + [System.Serializable] |
| 31 | + public class debugVar |
| 32 | + { |
| 33 | + public string varName; |
| 34 | + public object value; |
| 35 | + } |
| 36 | + [SerializeField] |
| 37 | + public List<debugVar> vars = new List<debugVar>(); |
| 38 | + |
| 39 | + public debugVar GetVarByName(string varName) |
| 40 | + { |
| 41 | + foreach (var v in vars) |
| 42 | + { |
| 43 | + if (v.varName == varName) |
| 44 | + { |
| 45 | + return v; |
| 46 | + } |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + public void DebugVar(string varName, object value) |
| 52 | + { |
| 53 | + if (vars == null) |
| 54 | + { |
| 55 | + vars = new List<debugVar>(); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + debugVar v = GetVarByName(varName); |
| 60 | + if (v == null) |
| 61 | + { |
| 62 | + v = new debugVar() { varName = varName, value = value }; |
| 63 | + vars.Add(v); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + v.value = value; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + [SerializeField] |
| 73 | + public List<debugObject> objects = new List<debugObject>(); |
| 74 | + |
| 75 | + [System.Serializable] |
| 76 | + public class alert |
| 77 | + { |
| 78 | + public string title; public object value; public Object target; |
| 79 | + } |
| 80 | + [SerializeField] |
| 81 | + public List<alert> alerts = new List<alert>(); |
| 82 | + |
| 83 | + public debugObject GetDebugObject(Object target) |
| 84 | + { |
| 85 | + foreach (var o in objects) |
| 86 | + { |
| 87 | + if (o.obj == target) |
| 88 | + { |
| 89 | + return o; |
| 90 | + } |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | + public static void Log(string varName, object value, Object target) |
| 95 | + { |
| 96 | + debugObject o = main.GetDebugObject(target); |
| 97 | + if (o == null) |
| 98 | + { |
| 99 | + o = new debugObject(); |
| 100 | + o.obj = target; |
| 101 | + o.DebugVar(varName, value); |
| 102 | + main.objects.Add(o); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + o.DebugVar(varName, value); |
| 107 | + } |
| 108 | + } |
| 109 | + public static void Alert(string title, object value, Object target, float time = 3f) |
| 110 | + { |
| 111 | + Alert(title, value, target, time, alertType.Info); |
| 112 | + } |
| 113 | + public enum alertType |
| 114 | + { |
| 115 | + Info, Warning, Error |
| 116 | + } |
| 117 | + public static async void Alert(string title, object value, Object target, float time = 3, alertType AlertType = alertType.Info) |
| 118 | + { |
| 119 | + string prefix = ""; |
| 120 | + string suffix = "</color>"; |
| 121 | + switch (AlertType) |
| 122 | + { |
| 123 | + case alertType.Info: |
| 124 | + Debug.Log($"<b>{title}</b>\n{value}", target); |
| 125 | + prefix = "<color=white>"; |
| 126 | + break; |
| 127 | + case alertType.Warning: |
| 128 | + Debug.LogWarning($"<b>{title}</b>\n{value}", target); |
| 129 | + prefix = "<color=orange> (!)"; |
| 130 | + break; |
| 131 | + case alertType.Error: |
| 132 | + Debug.LogError($"<b>{title}</b>\n{value}", target); |
| 133 | + prefix = "<color=red> [!]"; |
| 134 | + break; |
| 135 | + default: |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + //Remove older alertes if alert count is greater than 5 |
| 140 | + if (main.alerts.Count > 5) |
| 141 | + { |
| 142 | + main.alerts.RemoveRange(0, main.alerts.Count - 5); |
| 143 | + } |
| 144 | + |
| 145 | + //Remove other alert with same title and target |
| 146 | + foreach (var a in main.alerts) |
| 147 | + { |
| 148 | + if (a.title == title && a.target == target) |
| 149 | + { |
| 150 | + main.alerts.Remove(a); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + alert alert = new alert() { title = prefix + title + suffix, value = prefix + value + suffix, target = target }; |
| 155 | + main.alerts.Add(alert); |
| 156 | + //Remove alert |
| 157 | + await Task.Delay((int)(time * 1000)); |
| 158 | + main.alerts.Remove(alert); |
| 159 | + } |
| 160 | + |
| 161 | + private void OnGUI() |
| 162 | + { |
| 163 | + string debugText = ""; |
| 164 | + |
| 165 | + //Generate debug text |
| 166 | + foreach (var o in objects) |
| 167 | + { |
| 168 | + debugText += $"<b>[{o.obj}]</b>\n"; |
| 169 | + |
| 170 | + foreach (var v in o.vars) |
| 171 | + { |
| 172 | + debugText += $"{v.varName}: {v.value}\n"; |
| 173 | + } |
| 174 | + debugText += "\n\n"; |
| 175 | + } |
| 176 | + |
| 177 | + //Calculate text size |
| 178 | + Vector2 debugTextSize = GUI.skin.label.CalcSize(new GUIContent(debugText)); |
| 179 | + |
| 180 | + //Draw Shadow |
| 181 | + GUI.color = new Color(0, 0, 0, 1); |
| 182 | + GUI.Label(new Rect(11, 11, debugTextSize.x, debugTextSize.y), debugText); |
| 183 | + |
| 184 | + //Draw text |
| 185 | + GUI.color = new Color(1, 1, 1, 1f); |
| 186 | + GUI.Label(new Rect(10, 10, debugTextSize.x, debugTextSize.y), debugText); |
| 187 | + |
| 188 | + |
| 189 | + //Generate Alert |
| 190 | + string alertText = ""; |
| 191 | + foreach (var a in alerts) |
| 192 | + { |
| 193 | + alertText += $"<b>{a.title}</b>\n{a.value}\n<size=8>{a.target}</size>\n\n"; |
| 194 | + } |
| 195 | + |
| 196 | + //Calculate text size |
| 197 | + Vector2 alertTextSize = GUI.skin.label.CalcSize(new GUIContent(alertText)); |
| 198 | + |
| 199 | + GUIStyle alertStyle = new GUIStyle(); |
| 200 | + alertStyle.alignment = TextAnchor.UpperRight; |
| 201 | + alertStyle.normal.textColor = new Color(1, 1, 1, 1f); |
| 202 | + |
| 203 | + //Remove custom colors from text |
| 204 | + string shadowText = alertText.Replace("<color=white>", "").Replace("</color>", "").Replace("<color=orange>", "").Replace("</color>", "").Replace("<color=red>", "").Replace("</color>", ""); |
| 205 | + |
| 206 | + //Draw Shadow |
| 207 | + GUI.color = new Color(0, 0, 0, 0.5f); |
| 208 | + GUI.Label(new Rect(Screen.width - 10 - alertTextSize.x, 11, alertTextSize.x, alertTextSize.y), shadowText, alertStyle); |
| 209 | + |
| 210 | + //Draw text |
| 211 | + GUI.color = new Color(1, 1, 1, 1f); |
| 212 | + GUI.Label(new Rect(Screen.width - 10 - alertTextSize.x, 10, alertTextSize.x, alertTextSize.y), alertText, alertStyle); |
| 213 | + } |
| 214 | + |
| 215 | + private void Awake() |
| 216 | + { |
| 217 | + objects.Clear(); |
| 218 | + alerts.Clear(); |
| 219 | + } |
| 220 | +} |
0 commit comments