SceneDumper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. namespace DeveloperConsole
  8. {
  9. static class SceneDumper
  10. {
  11. public static void DumpScene()
  12. {
  13. var objects = SceneManager.GetActiveScene().GetRootGameObjects();
  14. var fname = Path.GetTempFileName();
  15. using (var f = File.OpenWrite(fname))
  16. using (var sw = new StreamWriter(f, Encoding.UTF8))
  17. {
  18. foreach (var obj in objects)
  19. {
  20. PrintRecursive(sw, obj);
  21. }
  22. }
  23. Process.Start("notepad.exe", fname);
  24. }
  25. private static void PrintRecursive(StreamWriter sw, GameObject obj)
  26. {
  27. PrintRecursive(sw, obj, 0);
  28. }
  29. private static void PrintRecursive(StreamWriter sw, GameObject obj, int d)
  30. {
  31. //Console.WriteLine(obj.name);
  32. var pad1 = new string(' ', 3 * d);
  33. var pad2 = new string(' ', 3 * (d + 1));
  34. var pad3 = new string(' ', 3 * (d + 2));
  35. sw.WriteLine(pad1 + obj.name + "--" + obj.GetType().FullName);
  36. foreach (Component c in obj.GetComponents<Component>())
  37. {
  38. sw.WriteLine(pad2 + "::" + c.GetType().Name);
  39. var ct = c.GetType();
  40. var props = ct.GetProperties(System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  41. foreach (var p in props)
  42. {
  43. try
  44. {
  45. var v = p.GetValue(c, null);
  46. sw.WriteLine(pad3 + "@" + p.Name + "<" + p.PropertyType.Name + "> = " + v);
  47. }
  48. catch (Exception e)
  49. {
  50. //Console.WriteLine(e.Message);
  51. }
  52. }
  53. }
  54. foreach (Transform t in obj.transform)
  55. {
  56. PrintRecursive(sw, t.gameObject, d + 1);
  57. }
  58. }
  59. }
  60. }