暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RunModal.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Unity.PlasticSCM.Editor.UI
  6. {
  7. internal static class RunModal
  8. {
  9. static RunModal()
  10. {
  11. InitializeInfo();
  12. }
  13. internal static bool IsAvailable()
  14. {
  15. return mShowWithModeMethod != null
  16. && mCreateSavedGUIState != null
  17. && mApplyAndForgetMethod != null
  18. && mParentField != null
  19. && mParentWindowProp != null
  20. && mMakeModalMethod != null;
  21. }
  22. internal static void Dialog(EditorWindow window)
  23. {
  24. ShowAsUtility(window);
  25. object savedGUIState = CreateSavedGUIState();
  26. PushDispatcherContext(window);
  27. MakeModal(window);
  28. PopDispatcherContext(window);
  29. ApplySavedGUIState(savedGUIState);
  30. }
  31. static void MakeModal(EditorWindow window)
  32. {
  33. // MakeModal(m_Parent.window);
  34. var hostView = mParentField.GetValue(window);
  35. var parentWindow = mParentWindowProp.GetValue(hostView, null);
  36. mMakeModalMethod.Invoke(
  37. mMakeModalMethod.IsStatic ? null : window,
  38. new object[] { parentWindow });
  39. }
  40. static void ShowAsUtility(EditorWindow window)
  41. {
  42. // ShowWithMode(ShowMode.Utility);
  43. mShowWithModeMethod.Invoke(window, new object[] { 2 });
  44. }
  45. static object CreateSavedGUIState()
  46. {
  47. // SavedGUIState guiState = SavedGUIState.Create();
  48. return mCreateSavedGUIState.Invoke(null, null);
  49. }
  50. static void ApplySavedGUIState(object savedGUIState)
  51. {
  52. // guiState.ApplyAndForget();
  53. mApplyAndForgetMethod.Invoke(savedGUIState, null);
  54. }
  55. static void PopDispatcherContext(EditorWindow window)
  56. {
  57. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PopDispatcherContext();
  58. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  59. mPopContextMethod2020.Invoke(editorDispatcher, null);
  60. }
  61. static void PushDispatcherContext(EditorWindow window)
  62. {
  63. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PushDispatcherContext();
  64. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  65. mPushContextMethod2020.Invoke(editorDispatcher, null);
  66. }
  67. static object GetDispatcher(EditorWindow window)
  68. {
  69. object dispatcher = null;
  70. if (MayHaveDispatcher())
  71. {
  72. var parent = mParentField.GetValue(window);
  73. if (parent != null)
  74. {
  75. var visualTree = mVisualTreeProp.GetValue(parent, null);
  76. if (visualTree != null)
  77. {
  78. var panel = mPanelProp.GetValue(visualTree, null);
  79. if (panel != null)
  80. {
  81. dispatcher = mDispatcherProp.GetValue(panel, null);
  82. }
  83. }
  84. }
  85. }
  86. return dispatcher;
  87. }
  88. static bool MayHaveDispatcher()
  89. {
  90. return mDispatcherType != null
  91. && mPushContextMethod != null
  92. && mPopContextMethod != null;
  93. }
  94. static void InitializeInfo()
  95. {
  96. var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
  97. mMakeModalMethod = BuildMakeModalMethodInfo(flags);
  98. mShowWithModeMethod = typeof(EditorWindow).GetMethod("ShowWithMode", flags);
  99. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  100. var hostViewType = mParentField.FieldType;
  101. mParentWindowProp = hostViewType.GetProperty("window", flags);
  102. var savedGUIStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.SavedGUIState");
  103. mCreateSavedGUIState = savedGUIStateType.GetMethod("Create", flags);
  104. mApplyAndForgetMethod = savedGUIStateType.GetMethod("ApplyAndForget", flags);
  105. mEditorDispatcherProp2020 = typeof(UnityEngine.UIElements.EventDispatcher).GetProperty("editorDispatcher", flags);
  106. mPushContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PushDispatcherContext", flags);
  107. mPopContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PopDispatcherContext", flags);
  108. flags = BindingFlags.NonPublic
  109. | BindingFlags.Instance
  110. | BindingFlags.Public;
  111. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  112. if (mParentField != null)
  113. hostViewType = mParentField.FieldType;
  114. if (hostViewType != null)
  115. mVisualTreeProp = hostViewType.GetProperty("visualTree");
  116. if (mVisualTreeProp != null)
  117. {
  118. var visualTreeType = mVisualTreeProp.PropertyType;
  119. if (visualTreeType != null)
  120. {
  121. mPanelProp = visualTreeType.GetProperty("panel");
  122. if (mPanelProp != null)
  123. {
  124. var panelType = mPanelProp.PropertyType;
  125. if (panelType != null)
  126. {
  127. mDispatcherProp = panelType.GetProperty("dispatcher");
  128. if (mDispatcherProp != null)
  129. {
  130. mDispatcherType = mDispatcherProp.PropertyType;
  131. if (mDispatcherType != null)
  132. {
  133. mPushContextMethod = mDispatcherType.GetMethod("PushDispatcherContext", flags);
  134. mPopContextMethod = mDispatcherType.GetMethod("PopDispatcherContext", flags);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. static MethodInfo BuildMakeModalMethodInfo(BindingFlags flags)
  143. {
  144. return typeof(EditorWindow).GetMethod("Internal_MakeModal", flags);
  145. }
  146. static FieldInfo mParentField;
  147. static PropertyInfo mParentWindowProp;
  148. static MethodInfo mMakeModalMethod;
  149. static MethodInfo mShowWithModeMethod;
  150. static MethodInfo mCreateSavedGUIState;
  151. static MethodInfo mApplyAndForgetMethod;
  152. static PropertyInfo mVisualTreeProp;
  153. static Type mDispatcherType;
  154. static MethodInfo mPushContextMethod;
  155. static MethodInfo mPopContextMethod;
  156. static PropertyInfo mPanelProp;
  157. static PropertyInfo mDispatcherProp;
  158. static PropertyInfo mEditorDispatcherProp2020;
  159. static MethodInfo mPushContextMethod2020;
  160. static MethodInfo mPopContextMethod2020;
  161. // // How ContainerWindows are visualized. Used with ContainerWindow.Show
  162. // internal enum ShowMode
  163. // {
  164. // // Show as a normal window with max, min & close buttons.
  165. // NormalWindow = 0,
  166. // // Used for a popup menu. On mac this means light shadow and no titlebar.
  167. // PopupMenu = 1,
  168. // // Utility window - floats above the app. Disappears when app loses focus.
  169. // Utility = 2,
  170. // // Window has no shadow or decorations. Used internally for dragging stuff around.
  171. // NoShadow = 3,
  172. // // The Unity main window. On mac, this is the same as NormalWindow, except window doesn't have a close button.
  173. // MainWindow = 4,
  174. // // Aux windows. The ones that close the moment you move the mouse out of them.
  175. // AuxWindow = 5,
  176. // // Like PopupMenu, but without keyboard focus
  177. // Tooltip = 6
  178. // }
  179. }
  180. }