Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Metaplay.Client.Unity;
- using UnityEngine;
- public class ExtendedModelInspectorWindow : ModelInspectorWindow
- {
- private List<string> _lastSelectionPath = new List<string>();
- [MenuItem("Metaplay/Model Inspector (Extended)")]
- public static void OpenModelInspector()
- {
- var window = GetWindow<ExtendedModelInspectorWindow>();
- window.titleContent = new GUIContent("Model Inspector (Extended)");
- window.minSize = new Vector2(200.0f, 200.0f);
- window.Show();
- }
- protected virtual void SaveSelectionPath()
- {
- var treeViewField = typeof(ModelInspectorWindow)
- .GetField("_treeView", BindingFlags.NonPublic | BindingFlags.Instance);
- var treeView = treeViewField?.GetValue(this);
- if (treeView != null)
- {
- var getSelectedItemMethod = treeView.GetType()
- .GetMethod("GetSelectedItem", BindingFlags.Public | BindingFlags.Instance);
- var selectedProxy = getSelectedItemMethod?.Invoke(treeView, null) as ValueProxyBase;
- _lastSelectionPath.Clear();
- if (selectedProxy != null)
- {
- var current = selectedProxy;
- while (current != null)
- {
- string name = current.UpdateNameText().Split(':')[0].Trim();
- _lastSelectionPath.Insert(0, name);
- current = current.Parent;
- }
- }
- }
- }
- protected virtual void RestoreSelection()
- {
- if (_lastSelectionPath.Count == 0) return;
- var treeViewField = typeof(ModelInspectorWindow)
- .GetField("_treeView", BindingFlags.NonPublic | BindingFlags.Instance);
- var rootProxyField = typeof(ModelInspectorWindow)
- .GetField("_rootProxy", BindingFlags.NonPublic | BindingFlags.Instance);
- var treeView = treeViewField?.GetValue(this);
- var rootProxy = rootProxyField?.GetValue(this) as ValueProxyBase;
- if (treeView == null || rootProxy == null) return;
- var current = rootProxy;
- bool pathIsValid = true;
- foreach (var pathPart in _lastSelectionPath.Skip(1))
- {
- var child = current.Children.FirstOrDefault(c =>
- c.UpdateNameText().Split(':')[0].Trim() == pathPart);
- if (child == null)
- {
- // Path is no longer valid, silently fail without error
- pathIsValid = false;
- break;
- }
- current = child;
- var setExpandedMethod = treeView.GetType()
- .GetMethod("SetExpanded",
- BindingFlags.Public | BindingFlags.Instance,
- null,
- new[] { typeof(int), typeof(bool) },
- null);
- setExpandedMethod?.Invoke(treeView, new object[] { current.Id, true });
- }
- if (pathIsValid && current != null)
- {
- var setSelectionMethod = treeView.GetType()
- .GetMethod("SetSelection",
- BindingFlags.Public | BindingFlags.Instance,
- null,
- new[] { typeof(IList<int>) },
- null);
- setSelectionMethod?.Invoke(treeView, new object[] { new[] { current.Id } });
- }
- // Clear invalid path so we don't try to restore it again
- if (!pathIsValid)
- {
- _lastSelectionPath.Clear();
- }
- }
- public new void OnEnable()
- {
- base.OnEnable();
- EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
- }
- public new void OnDisable()
- {
- base.OnDisable();
- EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
- }
- private void OnPlayModeStateChanged(PlayModeStateChange state)
- {
- if (state == PlayModeStateChange.ExitingPlayMode)
- {
- SaveSelectionPath();
- }
- else if (state == PlayModeStateChange.EnteredPlayMode)
- {
- EditorApplication.delayCall += () => { EditorApplication.delayCall += RestoreSelection; };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment