DumpScenePlugin.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. namespace BepInEx.Internal
  11. {
  12. class DumpScenePlugin : IUnityPlugin
  13. {
  14. public void OnStart()
  15. {
  16. }
  17. public void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
  18. {
  19. }
  20. public void OnFixedUpdate()
  21. {
  22. }
  23. public void OnLateUpdate()
  24. {
  25. }
  26. public void OnUpdate()
  27. {
  28. if (UnityEngine.Event.current.Equals(Event.KeyboardEvent("f8")))
  29. {
  30. //DumpScene();
  31. }
  32. }
  33. static List<string> lines;
  34. public static void DumpScene()
  35. {
  36. lines = new List<string>();
  37. string filename = @"M:\unity-scene.txt";
  38. Debug.Log("Dumping scene to " + filename + " ...");
  39. using (StreamWriter writer = new StreamWriter(filename, false))
  40. {
  41. foreach (GameObject gameObject in GameObject.FindObjectsOfType<GameObject>())
  42. {
  43. if (gameObject.activeInHierarchy)
  44. DumpGameObject(gameObject, writer, "");
  45. }
  46. foreach (string line in lines)
  47. {
  48. writer.WriteLine(line);
  49. }
  50. }
  51. Debug.Log("Scene dumped to " + filename);
  52. }
  53. private static void DumpGameObject(GameObject gameObject, StreamWriter writer, string indent)
  54. {
  55. //writer.WriteLine("{0}+{1}+{2}", indent, gameObject.name, gameObject.GetType().FullName);
  56. foreach (Component component in gameObject.GetComponents<Component>())
  57. {
  58. DumpComponent(component, writer, indent + " ");
  59. }
  60. foreach (Transform child in gameObject.transform)
  61. {
  62. DumpGameObject(child.gameObject, writer, indent + " ");
  63. }
  64. }
  65. private static void DumpComponent(Component component, StreamWriter writer, string indent)
  66. {
  67. //writer.WriteLine("{0}{1}", indent, (component == null ? "(null)" : component.GetType().FullName));
  68. if (component is TextMeshProUGUI)
  69. {
  70. string text = ((TextMeshProUGUI)component).text;
  71. if (!text.IsNullOrWhiteSpace()
  72. && !text.Contains("Reset")
  73. && !Regex.Replace(text, @"[\d-]", string.Empty).IsNullOrWhiteSpace())
  74. {
  75. if (!lines.Contains(text))
  76. lines.Add(text);
  77. }
  78. }
  79. }
  80. }
  81. }