Skip to content

Instantly share code, notes, and snippets.

@wotakuro
Created February 7, 2024 08:29
Show Gist options
  • Save wotakuro/51516a3ed5c3f97543d8f0dd9bc64018 to your computer and use it in GitHub Desktop.
Save wotakuro/51516a3ed5c3f97543d8f0dd9bc64018 to your computer and use it in GitHub Desktop.
SerializedObjectDebugWindow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using System.IO;
using System.Text;
public class SerializedObjectDebugWindow : EditorWindow
{
[MenuItem("Tools/SerializedObjectDebug")]
public static void Create()
{
SerializedObjectDebugWindow.GetWindow(typeof(SerializedObjectDebugWindow));
}
private ScrollView scrollView;
private void OnEnable()
{
var targetObj = new ObjectField();
targetObj.objectType = typeof(UnityEngine.Object);
targetObj.RegisterValueChangedCallback(OnValueChange);
var button = new Button();
button.text = "Debug";
button.clicked += OnExec;
this.rootVisualElement.Add(targetObj);
this.rootVisualElement.Add(button);
this.scrollView = new ScrollView();
this.rootVisualElement.Add(scrollView);
}
void OnExec()
{
DumpText();
}
void DumpText()
{
var labels = this.scrollView.Query<Label>();
var sb = new StringBuilder(1024*16);
labels.ForEach((label) =>
{
sb.AppendLine(label.text);
});
System.IO.File.WriteAllText("dump.txt", sb.ToString());
}
void OnValueChange(ChangeEvent<Object> evt)
{
this.scrollView.Clear();
if (!evt.newValue)
{
return;
}
CreateFromObj(evt.newValue);
}
void CreateFromObj(Object obj) {
var serializedObject = new SerializedObject(obj);
var iterator = serializedObject.GetIterator();
while (iterator.NextVisible(true))
{
var text = iterator.propertyPath + "::";
switch(iterator.propertyType )
{
case SerializedPropertyType.String:
text += iterator.stringValue;
break;
case SerializedPropertyType.Integer:
text += iterator.intValue;
break;
case SerializedPropertyType.Boolean:
text += iterator.boolValue;
break;
}
var label = new Label(text);
this.scrollView.Add(label);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment