SceneDumper.cs 2.2 KB

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