Guest User

Untitled

a guest
Jan 13th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. using UnityEditor;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Metaplay.Client.Unity;
  6. using UnityEngine;
  7.  
  8. public class ExtendedModelInspectorWindow : ModelInspectorWindow
  9. {
  10. private List<string> _lastSelectionPath = new List<string>();
  11.  
  12. [MenuItem("Metaplay/Model Inspector (Extended)")]
  13. public static void OpenModelInspector()
  14. {
  15. var window = GetWindow<ExtendedModelInspectorWindow>();
  16. window.titleContent = new GUIContent("Model Inspector (Extended)");
  17. window.minSize = new Vector2(200.0f, 200.0f);
  18. window.Show();
  19. }
  20.  
  21. protected virtual void SaveSelectionPath()
  22. {
  23. var treeViewField = typeof(ModelInspectorWindow)
  24. .GetField("_treeView", BindingFlags.NonPublic | BindingFlags.Instance);
  25. var treeView = treeViewField?.GetValue(this);
  26.  
  27. if (treeView != null)
  28. {
  29. var getSelectedItemMethod = treeView.GetType()
  30. .GetMethod("GetSelectedItem", BindingFlags.Public | BindingFlags.Instance);
  31. var selectedProxy = getSelectedItemMethod?.Invoke(treeView, null) as ValueProxyBase;
  32.  
  33. _lastSelectionPath.Clear();
  34. if (selectedProxy != null)
  35. {
  36. var current = selectedProxy;
  37. while (current != null)
  38. {
  39. string name = current.UpdateNameText().Split(':')[0].Trim();
  40. _lastSelectionPath.Insert(0, name);
  41. current = current.Parent;
  42. }
  43. }
  44. }
  45. }
  46.  
  47. protected virtual void RestoreSelection()
  48. {
  49. if (_lastSelectionPath.Count == 0) return;
  50.  
  51. var treeViewField = typeof(ModelInspectorWindow)
  52. .GetField("_treeView", BindingFlags.NonPublic | BindingFlags.Instance);
  53. var rootProxyField = typeof(ModelInspectorWindow)
  54. .GetField("_rootProxy", BindingFlags.NonPublic | BindingFlags.Instance);
  55.  
  56. var treeView = treeViewField?.GetValue(this);
  57. var rootProxy = rootProxyField?.GetValue(this) as ValueProxyBase;
  58.  
  59. if (treeView == null || rootProxy == null) return;
  60.  
  61. var current = rootProxy;
  62. bool pathIsValid = true;
  63.  
  64. foreach (var pathPart in _lastSelectionPath.Skip(1))
  65. {
  66. var child = current.Children.FirstOrDefault(c =>
  67. c.UpdateNameText().Split(':')[0].Trim() == pathPart);
  68.  
  69. if (child == null)
  70. {
  71. // Path is no longer valid, silently fail without error
  72. pathIsValid = false;
  73. break;
  74. }
  75.  
  76. current = child;
  77.  
  78. var setExpandedMethod = treeView.GetType()
  79. .GetMethod("SetExpanded",
  80. BindingFlags.Public | BindingFlags.Instance,
  81. null,
  82. new[] { typeof(int), typeof(bool) },
  83. null);
  84. setExpandedMethod?.Invoke(treeView, new object[] { current.Id, true });
  85. }
  86.  
  87. if (pathIsValid && current != null)
  88. {
  89. var setSelectionMethod = treeView.GetType()
  90. .GetMethod("SetSelection",
  91. BindingFlags.Public | BindingFlags.Instance,
  92. null,
  93. new[] { typeof(IList<int>) },
  94. null);
  95. setSelectionMethod?.Invoke(treeView, new object[] { new[] { current.Id } });
  96. }
  97.  
  98. // Clear invalid path so we don't try to restore it again
  99. if (!pathIsValid)
  100. {
  101. _lastSelectionPath.Clear();
  102. }
  103. }
  104.  
  105. public new void OnEnable()
  106. {
  107. base.OnEnable();
  108. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  109. }
  110.  
  111. public new void OnDisable()
  112. {
  113. base.OnDisable();
  114. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  115. }
  116.  
  117. private void OnPlayModeStateChanged(PlayModeStateChange state)
  118. {
  119. if (state == PlayModeStateChange.ExitingPlayMode)
  120. {
  121. SaveSelectionPath();
  122. }
  123. else if (state == PlayModeStateChange.EnteredPlayMode)
  124. {
  125. EditorApplication.delayCall += () => { EditorApplication.delayCall += RestoreSelection; };
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment