EditorUtilities.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Assertions;
  6. using UnityEngine.Rendering.PostProcessing;
  7. namespace UnityEditor.Rendering.PostProcessing
  8. {
  9. /// <summary>
  10. /// A set of editor utilities used in post-processing editors.
  11. /// </summary>
  12. public static class EditorUtilities
  13. {
  14. static Dictionary<string, GUIContent> s_GUIContentCache;
  15. static Dictionary<Type, AttributeDecorator> s_AttributeDecorators;
  16. static PostProcessEffectSettings s_ClipboardContent;
  17. /// <summary>
  18. /// Returns <c>true</c> if the current target is a console, <c>false</c> otherwise.
  19. /// </summary>
  20. public static bool isTargetingConsoles
  21. {
  22. get
  23. {
  24. var t = EditorUserBuildSettings.activeBuildTarget;
  25. return t == BuildTarget.PS4
  26. || t == BuildTarget.XboxOne
  27. || t == BuildTarget.Switch;
  28. }
  29. }
  30. /// <summary>
  31. /// Returns <c>true</c> if the current target is a mobile, <c>false</c> otherwise.
  32. /// </summary>
  33. public static bool isTargetingMobiles
  34. {
  35. get
  36. {
  37. var t = EditorUserBuildSettings.activeBuildTarget;
  38. return t == BuildTarget.Android
  39. || t == BuildTarget.iOS
  40. || t == BuildTarget.tvOS
  41. #if !UNITY_2018_2_OR_NEWER
  42. || t == BuildTarget.Tizen
  43. #endif
  44. #if !UNITY_2018_3_OR_NEWER
  45. || t == BuildTarget.N3DS
  46. || t == BuildTarget.PSP2
  47. #endif
  48. ;
  49. }
  50. }
  51. /// <summary>
  52. /// Returns <c>true</c> if the current target is a console or a mobile, <c>false</c>
  53. /// otherwise.
  54. /// </summary>
  55. public static bool isTargetingConsolesOrMobiles
  56. {
  57. get { return isTargetingConsoles || isTargetingMobiles; }
  58. }
  59. static EditorUtilities()
  60. {
  61. s_GUIContentCache = new Dictionary<string, GUIContent>();
  62. s_AttributeDecorators = new Dictionary<Type, AttributeDecorator>();
  63. ReloadDecoratorTypes();
  64. }
  65. [Callbacks.DidReloadScripts]
  66. static void OnEditorReload()
  67. {
  68. ReloadDecoratorTypes();
  69. }
  70. static void ReloadDecoratorTypes()
  71. {
  72. s_AttributeDecorators.Clear();
  73. // Look for all the valid attribute decorators
  74. var types = RuntimeUtilities.GetAllAssemblyTypes()
  75. .Where(
  76. t => t.IsSubclassOf(typeof(AttributeDecorator))
  77. && t.IsDefined(typeof(DecoratorAttribute), false)
  78. && !t.IsAbstract
  79. );
  80. // Store them
  81. foreach (var type in types)
  82. {
  83. var attr = type.GetAttribute<DecoratorAttribute>();
  84. var decorator = (AttributeDecorator)Activator.CreateInstance(type);
  85. s_AttributeDecorators.Add(attr.attributeType, decorator);
  86. }
  87. }
  88. internal static AttributeDecorator GetDecorator(Type attributeType)
  89. {
  90. AttributeDecorator decorator;
  91. return !s_AttributeDecorators.TryGetValue(attributeType, out decorator)
  92. ? null
  93. : decorator;
  94. }
  95. /// <summary>
  96. /// Gets a <see cref="GUIContent"/> for the given label and tooltip. These are recycled
  97. /// internally and help reduce the garbage collector pressure in the editor.
  98. /// </summary>
  99. /// <param name="textAndTooltip">The label and tooltip separated by a <c>|</c>
  100. /// character</param>
  101. /// <returns>A recycled <see cref="GUIContent"/></returns>
  102. public static GUIContent GetContent(string textAndTooltip)
  103. {
  104. if (string.IsNullOrEmpty(textAndTooltip))
  105. return GUIContent.none;
  106. GUIContent content;
  107. if (!s_GUIContentCache.TryGetValue(textAndTooltip, out content))
  108. {
  109. var s = textAndTooltip.Split('|');
  110. content = new GUIContent(s[0]);
  111. if (s.Length > 1 && !string.IsNullOrEmpty(s[1]))
  112. content.tooltip = s[1];
  113. s_GUIContentCache.Add(textAndTooltip, content);
  114. }
  115. return content;
  116. }
  117. /// <summary>
  118. /// Draws a UI box with a description and a "Fix Me" button next to it.
  119. /// </summary>
  120. /// <param name="text">The description</param>
  121. /// <param name="action">The action to execute when the button is clicked</param>
  122. public static void DrawFixMeBox(string text, Action action)
  123. {
  124. Assert.IsNotNull(action);
  125. EditorGUILayout.HelpBox(text, MessageType.Warning);
  126. GUILayout.Space(-32);
  127. using (new EditorGUILayout.HorizontalScope())
  128. {
  129. GUILayout.FlexibleSpace();
  130. if (GUILayout.Button("Fix", GUILayout.Width(60)))
  131. action();
  132. GUILayout.Space(8);
  133. }
  134. GUILayout.Space(11);
  135. }
  136. /// <summary>
  137. /// Draws a horizontal split line.
  138. /// </summary>
  139. public static void DrawSplitter()
  140. {
  141. var rect = GUILayoutUtility.GetRect(1f, 1f);
  142. // Splitter rect should be full-width
  143. rect.xMin = 0f;
  144. rect.width += 4f;
  145. if (Event.current.type != EventType.Repaint)
  146. return;
  147. EditorGUI.DrawRect(rect, Styling.splitter);
  148. }
  149. /// <summary>
  150. /// Draws a toggle using the "override checkbox" style.
  151. /// </summary>
  152. /// <param name="rect">The position and size of the toggle</param>
  153. /// <param name="property">The override state property for the toggle</param>
  154. public static void DrawOverrideCheckbox(Rect rect, SerializedProperty property)
  155. {
  156. property.boolValue = GUI.Toggle(rect, property.boolValue, GetContent("|Override this setting for this volume."), Styling.smallTickbox);
  157. }
  158. /// <summary>
  159. /// Draws a header label.
  160. /// </summary>
  161. /// <param name="title">The label to display as a header</param>
  162. public static void DrawHeaderLabel(string title)
  163. {
  164. EditorGUILayout.LabelField(title, Styling.headerLabel);
  165. }
  166. internal static bool DrawHeader(string title, bool state)
  167. {
  168. var backgroundRect = GUILayoutUtility.GetRect(1f, 17f);
  169. var labelRect = backgroundRect;
  170. labelRect.xMin += 16f;
  171. labelRect.xMax -= 20f;
  172. var foldoutRect = backgroundRect;
  173. foldoutRect.y += 1f;
  174. foldoutRect.width = 13f;
  175. foldoutRect.height = 13f;
  176. // Background rect should be full-width
  177. backgroundRect.xMin = 0f;
  178. backgroundRect.width += 4f;
  179. // Background
  180. EditorGUI.DrawRect(backgroundRect, Styling.headerBackground);
  181. // Title
  182. EditorGUI.LabelField(labelRect, GetContent(title), EditorStyles.boldLabel);
  183. // Foldout
  184. state = GUI.Toggle(foldoutRect, state, GUIContent.none, EditorStyles.foldout);
  185. var e = Event.current;
  186. if (e.type == EventType.MouseDown && backgroundRect.Contains(e.mousePosition) && e.button == 0)
  187. {
  188. state = !state;
  189. e.Use();
  190. }
  191. return state;
  192. }
  193. internal static bool DrawHeader(string title, SerializedProperty group, SerializedProperty activeField, PostProcessEffectSettings target, Action resetAction, Action removeAction)
  194. {
  195. Assert.IsNotNull(group);
  196. Assert.IsNotNull(activeField);
  197. Assert.IsNotNull(target);
  198. var backgroundRect = GUILayoutUtility.GetRect(1f, 17f);
  199. var labelRect = backgroundRect;
  200. labelRect.xMin += 32f;
  201. labelRect.xMax -= 20f;
  202. var foldoutRect = backgroundRect;
  203. foldoutRect.y += 1f;
  204. foldoutRect.width = 13f;
  205. foldoutRect.height = 13f;
  206. var toggleRect = backgroundRect;
  207. toggleRect.x += 16f;
  208. toggleRect.y += 2f;
  209. toggleRect.width = 13f;
  210. toggleRect.height = 13f;
  211. var menuIcon = Styling.paneOptionsIcon;
  212. var menuRect = new Rect(labelRect.xMax + 4f, labelRect.y + 4f, menuIcon.width, menuIcon.height);
  213. // Background rect should be full-width
  214. backgroundRect.xMin = 0f;
  215. backgroundRect.width += 4f;
  216. // Background
  217. EditorGUI.DrawRect(backgroundRect, Styling.headerBackground);
  218. // Title
  219. using (new EditorGUI.DisabledScope(!activeField.boolValue))
  220. EditorGUI.LabelField(labelRect, GetContent(title), EditorStyles.boldLabel);
  221. // foldout
  222. group.serializedObject.Update();
  223. group.isExpanded = GUI.Toggle(foldoutRect, group.isExpanded, GUIContent.none, EditorStyles.foldout);
  224. group.serializedObject.ApplyModifiedProperties();
  225. // Active checkbox
  226. activeField.serializedObject.Update();
  227. activeField.boolValue = GUI.Toggle(toggleRect, activeField.boolValue, GUIContent.none, Styling.smallTickbox);
  228. activeField.serializedObject.ApplyModifiedProperties();
  229. // Dropdown menu icon
  230. GUI.DrawTexture(menuRect, menuIcon);
  231. // Handle events
  232. var e = Event.current;
  233. if (e.type == EventType.MouseDown)
  234. {
  235. if (menuRect.Contains(e.mousePosition))
  236. {
  237. ShowHeaderContextMenu(new Vector2(menuRect.x, menuRect.yMax), target, resetAction, removeAction);
  238. e.Use();
  239. }
  240. else if (labelRect.Contains(e.mousePosition))
  241. {
  242. if (e.button == 0)
  243. group.isExpanded = !group.isExpanded;
  244. else
  245. ShowHeaderContextMenu(e.mousePosition, target, resetAction, removeAction);
  246. e.Use();
  247. }
  248. }
  249. return group.isExpanded;
  250. }
  251. static void ShowHeaderContextMenu(Vector2 position, PostProcessEffectSettings target, Action resetAction, Action removeAction)
  252. {
  253. Assert.IsNotNull(resetAction);
  254. Assert.IsNotNull(removeAction);
  255. var menu = new GenericMenu();
  256. menu.AddItem(GetContent("Reset"), false, () => resetAction());
  257. menu.AddItem(GetContent("Remove"), false, () => removeAction());
  258. menu.AddSeparator(string.Empty);
  259. menu.AddItem(GetContent("Copy Settings"), false, () => CopySettings(target));
  260. if (CanPaste(target))
  261. menu.AddItem(GetContent("Paste Settings"), false, () => PasteSettings(target));
  262. else
  263. menu.AddDisabledItem(GetContent("Paste Settings"));
  264. menu.DropDown(new Rect(position, Vector2.zero));
  265. }
  266. static void CopySettings(PostProcessEffectSettings target)
  267. {
  268. Assert.IsNotNull(target);
  269. if (s_ClipboardContent != null)
  270. {
  271. RuntimeUtilities.Destroy(s_ClipboardContent);
  272. s_ClipboardContent = null;
  273. }
  274. s_ClipboardContent = (PostProcessEffectSettings)ScriptableObject.CreateInstance(target.GetType());
  275. EditorUtility.CopySerializedIfDifferent(target, s_ClipboardContent);
  276. }
  277. static void PasteSettings(PostProcessEffectSettings target)
  278. {
  279. Assert.IsNotNull(target);
  280. Assert.IsNotNull(s_ClipboardContent);
  281. Assert.AreEqual(s_ClipboardContent.GetType(), target.GetType());
  282. Undo.RecordObject(target, "Paste Settings");
  283. EditorUtility.CopySerializedIfDifferent(s_ClipboardContent, target);
  284. }
  285. static bool CanPaste(PostProcessEffectSettings target)
  286. {
  287. return s_ClipboardContent != null
  288. && s_ClipboardContent.GetType() == target.GetType();
  289. }
  290. }
  291. }