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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #if UNITY_2020_1_OR_NEWER
  58. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PopDispatcherContext();
  59. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  60. mPopContextMethod2020.Invoke(editorDispatcher, null);
  61. #else
  62. // m_Parent.visualTree.panel.dispatcher?.PopDispatcherContext();
  63. object dispatcher = GetDispatcher(window);
  64. if (dispatcher != null)
  65. mPopContextMethod.Invoke(dispatcher, null);
  66. #endif
  67. }
  68. static void PushDispatcherContext(EditorWindow window)
  69. {
  70. #if UNITY_2020_1_OR_NEWER
  71. //UnityEngine.UIElements.EventDispatcher.editorDispatcher.PushDispatcherContext();
  72. object editorDispatcher = mEditorDispatcherProp2020.GetValue(null);
  73. mPushContextMethod2020.Invoke(editorDispatcher, null);
  74. #else
  75. // m_Parent.visualTree.panel.dispatcher?.PushDispatcherContext();
  76. object dispatcher = GetDispatcher(window);
  77. if (dispatcher != null)
  78. mPushContextMethod.Invoke(dispatcher, null);
  79. #endif
  80. }
  81. static object GetDispatcher(EditorWindow window)
  82. {
  83. object dispatcher = null;
  84. if (MayHaveDispatcher())
  85. {
  86. var parent = mParentField.GetValue(window);
  87. if (parent != null)
  88. {
  89. var visualTree = mVisualTreeProp.GetValue(parent, null);
  90. if (visualTree != null)
  91. {
  92. var panel = mPanelProp.GetValue(visualTree, null);
  93. if (panel != null)
  94. {
  95. dispatcher = mDispatcherProp.GetValue(panel, null);
  96. }
  97. }
  98. }
  99. }
  100. return dispatcher;
  101. }
  102. static bool MayHaveDispatcher()
  103. {
  104. return mDispatcherType != null
  105. && mPushContextMethod != null
  106. && mPopContextMethod != null;
  107. }
  108. static void InitializeInfo()
  109. {
  110. var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
  111. mMakeModalMethod = BuildMakeModalMethodInfo(flags);
  112. mShowWithModeMethod = typeof(EditorWindow).GetMethod("ShowWithMode", flags);
  113. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  114. var hostViewType = mParentField.FieldType;
  115. mParentWindowProp = hostViewType.GetProperty("window", flags);
  116. var savedGUIStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.SavedGUIState");
  117. mCreateSavedGUIState = savedGUIStateType.GetMethod("Create", flags);
  118. mApplyAndForgetMethod = savedGUIStateType.GetMethod("ApplyAndForget", flags);
  119. #if UNITY_2020_1_OR_NEWER
  120. mEditorDispatcherProp2020 = typeof(UnityEngine.UIElements.EventDispatcher).GetProperty("editorDispatcher", flags);
  121. mPushContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PushDispatcherContext", flags);
  122. mPopContextMethod2020 = mEditorDispatcherProp2020.PropertyType.GetMethod("PopDispatcherContext", flags);
  123. #endif
  124. flags = BindingFlags.NonPublic
  125. | BindingFlags.Instance
  126. | BindingFlags.Public;
  127. mParentField = typeof(EditorWindow).GetField("m_Parent", flags);
  128. if (mParentField != null)
  129. hostViewType = mParentField.FieldType;
  130. if (hostViewType != null)
  131. mVisualTreeProp = hostViewType.GetProperty("visualTree");
  132. if (mVisualTreeProp != null)
  133. {
  134. var visualTreeType = mVisualTreeProp.PropertyType;
  135. if (visualTreeType != null)
  136. {
  137. mPanelProp = visualTreeType.GetProperty("panel");
  138. if (mPanelProp != null)
  139. {
  140. var panelType = mPanelProp.PropertyType;
  141. if (panelType != null)
  142. {
  143. mDispatcherProp = panelType.GetProperty("dispatcher");
  144. if (mDispatcherProp != null)
  145. {
  146. mDispatcherType = mDispatcherProp.PropertyType;
  147. if (mDispatcherType != null)
  148. {
  149. mPushContextMethod = mDispatcherType.GetMethod("PushDispatcherContext", flags);
  150. mPopContextMethod = mDispatcherType.GetMethod("PopDispatcherContext", flags);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. static MethodInfo BuildMakeModalMethodInfo(BindingFlags flags)
  159. {
  160. if (EditorVersion.IsCurrentEditorOlderThan("2019.3.10f1"))
  161. return typeof(EditorWindow).GetMethod("MakeModal", flags);
  162. return typeof(EditorWindow).GetMethod("Internal_MakeModal", flags);
  163. }
  164. static FieldInfo mParentField;
  165. static PropertyInfo mParentWindowProp;
  166. static MethodInfo mMakeModalMethod;
  167. static MethodInfo mShowWithModeMethod;
  168. static MethodInfo mCreateSavedGUIState;
  169. static MethodInfo mApplyAndForgetMethod;
  170. static PropertyInfo mVisualTreeProp;
  171. static Type mDispatcherType;
  172. static MethodInfo mPushContextMethod;
  173. static MethodInfo mPopContextMethod;
  174. static PropertyInfo mPanelProp;
  175. static PropertyInfo mDispatcherProp;
  176. #if UNITY_2020_1_OR_NEWER
  177. static PropertyInfo mEditorDispatcherProp2020;
  178. static MethodInfo mPushContextMethod2020;
  179. static MethodInfo mPopContextMethod2020;
  180. #endif
  181. // // How ContainerWindows are visualized. Used with ContainerWindow.Show
  182. // internal enum ShowMode
  183. // {
  184. // // Show as a normal window with max, min & close buttons.
  185. // NormalWindow = 0,
  186. // // Used for a popup menu. On mac this means light shadow and no titlebar.
  187. // PopupMenu = 1,
  188. // // Utility window - floats above the app. Disappears when app loses focus.
  189. // Utility = 2,
  190. // // Window has no shadow or decorations. Used internally for dragging stuff around.
  191. // NoShadow = 3,
  192. // // The Unity main window. On mac, this is the same as NormalWindow, except window doesn't have a close button.
  193. // MainWindow = 4,
  194. // // Aux windows. The ones that close the moment you move the mouse out of them.
  195. // AuxWindow = 5,
  196. // // Like PopupMenu, but without keyboard focus
  197. // Tooltip = 6
  198. // }
  199. }
  200. }