No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindEditorWindow.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace Unity.PlasticSCM.Editor.UI
  8. {
  9. internal static class FindEditorWindow
  10. {
  11. internal static EditorWindow ProjectWindow()
  12. {
  13. Type projectBrowserType = typeof(EditorWindow).Assembly.GetType(
  14. "UnityEditor.ProjectBrowser");
  15. UnityEngine.Object[] windows = Resources.FindObjectsOfTypeAll(
  16. projectBrowserType);
  17. if (windows.Length == 0)
  18. return null;
  19. return windows[0] as EditorWindow;
  20. }
  21. internal static EditorWindow ToDock<T>()
  22. {
  23. List<EditorWindow> windows = GetAvailableWindows();
  24. IEnumerable<EditorWindow> candidateWindows = windows
  25. .Where(w => !(w is T))
  26. .Where(w => w.position.width > 400 && w.position.height > 300)
  27. .OrderByDescending(w => w.position.width * w.position.height);
  28. return candidateWindows.FirstOrDefault();
  29. }
  30. static List<EditorWindow> GetAvailableWindows()
  31. {
  32. List<EditorWindow> result = new List<EditorWindow>();
  33. var hostViewField = typeof(EditorWindow).GetField(
  34. "m_Parent", BindingFlags.Instance | BindingFlags.NonPublic);
  35. if (hostViewField == null)
  36. return null;
  37. var hostViewType = hostViewField.FieldType;
  38. var actualViewField = hostViewType.GetField(
  39. "m_ActualView", BindingFlags.Instance | BindingFlags.NonPublic);
  40. if (actualViewField == null)
  41. return null;
  42. foreach (var window in Resources.FindObjectsOfTypeAll<EditorWindow>())
  43. {
  44. var hostView = hostViewField.GetValue(window);
  45. if (hostView == null)
  46. continue;
  47. EditorWindow actualDrawnWindow = actualViewField
  48. .GetValue(hostView) as EditorWindow;
  49. if (actualDrawnWindow == null)
  50. continue;
  51. if (result.Contains(actualDrawnWindow))
  52. continue;
  53. result.Add(actualDrawnWindow);
  54. }
  55. return result;
  56. }
  57. }
  58. }