Last active
October 21, 2025 17:14
-
-
Save b3x206/d5efd6884b6fbd065bc8114a7247ac80 to your computer and use it in GitHub Desktop.
Debug any class by adding this code on the GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Reflection; | |
| // This is free and unencumbered software released into the public domain. | |
| // Anyone is free to copy, modify, publish, use, compile, sell, or | |
| // distribute this software, either in source code form or as a compiled | |
| // binary, for any purpose, commercial or non-commercial, and by any | |
| // means. | |
| // (see https://unlicense.org) | |
| public class YourTargetClass : MonoBehaviour | |
| { | |
| // Add this code snippet to get live view on the fields and properties | |
| [SerializeField] | |
| private bool displayDebug = true; | |
| private GUIStyle detailsLabelStyle; | |
| private Vector2 scrollPos; | |
| private void OnGUI() | |
| { | |
| if (!displayDebug) | |
| { | |
| return; | |
| } | |
| detailsLabelStyle ??= new GUIStyle(GUI.skin.label) | |
| { | |
| richText = true, | |
| fontSize = 20, | |
| }; | |
| Rect areaRect = new Rect(20f, 20f, 500f, Mathf.Clamp(Screen.width - 200f, 200f, float.MaxValue)); | |
| Color prevColor = GUI.color; | |
| GUI.color = new Color(0f, 0f, 0f, 0.4f); | |
| GUI.DrawTexture(new Rect(areaRect.x, areaRect.y, areaRect.width, areaRect.height), Texture2D.whiteTexture); | |
| GUI.color = prevColor; | |
| GUILayout.BeginArea(areaRect); | |
| scrollPos = GUILayout.BeginScrollView(scrollPos); | |
| foreach (PropertyInfo v in GetType().GetProperties(BindingFlags.DeclaredOnly)) | |
| { | |
| // Unsupported index parameters, can be triggered by 'this[int idx]' expressions | |
| if (v.GetIndexParameters().Length > 0) | |
| { | |
| continue; | |
| } | |
| GUILayout.Label(string.Format(" <color=#f3bd28>[ Property ]</color> <color=#2eb6ae>{0}</color> <color=#dcdcdc>{1}</color> = {2}", v.PropertyType.Name, v.Name, v.GetValue(this)), detailsLabelStyle); | |
| } | |
| foreach (FieldInfo v in GetType().GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) | |
| { | |
| // Don't draw properties twice | |
| if (v.Name.Contains("k__BackingField")) | |
| { | |
| continue; | |
| } | |
| GUILayout.Label(string.Format(" <color=#f3bd28>[ Field ]</color> <color=#2eb6ae>{0}</color> <color=#dcdcdc>{1}</color> = {2}", v.FieldType.Name, v.Name, v.GetValue(this)), detailsLabelStyle); | |
| } | |
| GUILayout.EndScrollView(); | |
| GUILayout.EndArea(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment