EditorUtils.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Reflection;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProMovieCapture.Editor
  9. {
  10. /*public static class Utils
  11. {
  12. public static T GetCopyOf<T>(this Component comp, T other) where T : Component
  13. {
  14. System.Type type = comp.GetType();
  15. if (type != other.GetType())
  16. {
  17. return null; // type mis-match
  18. }
  19. BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly;
  20. PropertyInfo[] pinfos = type.GetProperties(flags);
  21. for (int i = 0; i < pinfos.Length; i++)
  22. {
  23. PropertyInfo pinfo = pinfos[i];
  24. if (pinfo.CanWrite)
  25. {
  26. try
  27. {
  28. pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
  29. }
  30. catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific.
  31. }
  32. }
  33. FieldInfo[] finfos = type.GetFields(flags);
  34. foreach (var finfo in finfos)
  35. {
  36. finfo.SetValue(comp, finfo.GetValue(other));
  37. }
  38. return comp as T;
  39. }
  40. }*/
  41. internal enum BitrateUnits
  42. {
  43. BitsPerSecond,
  44. KBitsPerSecond,
  45. MBitsPerSecond,
  46. }
  47. public static class EditorUtils
  48. {
  49. public static string[] AudioCaptureSourceNames = { "None", "Unity", "Microphone", "Manual", "Wwise", "Unity Audio Mixer" };
  50. public static string[] CommonFrameRateNames = { "1", "10", "15", "23.98", "24 - CINEMA", "25 - PAL", "29.97 - NTSC", "30 - PC", "50 - PAL", "59.94 - NTSC", "60 - PC", "75", "90", "120" };
  51. public static float[] CommonFrameRateValues = { 1f, 10f, 15f, 23.976f, 24f, 25f, 29.97f, 30f, 50f, 59.94f, 60f, 75f, 90f, 120f };
  52. public static string[] CommonAudioSampleRateNames = { "8kHz", "22.5kHz", "44.1kHz", "48kHz", "96kHz" };
  53. public static int[] CommonAudioSampleRateValues = { 8000, 22050, 44100, 48000, 96000 };
  54. internal static string[] OutputTargetNames = new string[] { "Video File", "Image Sequence", "Named Pipe" };
  55. public static string[] CommonVideoBitRateNames = { "YouTube/360p30 H.264 - 1 Mbps",
  56. "YouTube/360p60 H.264 - 1.5 Mbps",
  57. "YouTube/480p30 H.264 - 2.5 Mbps",
  58. };
  59. public static float[] CommonVideoBitRateValues = { 1f, 1.5f, 2.5f };
  60. public static SerializedProperty AssertFindProperty(this SerializedObject so, string propertyName)
  61. {
  62. SerializedProperty result = so.FindProperty(propertyName);
  63. Debug.Assert(result != null, "Missing property: " + propertyName);
  64. return result;
  65. }
  66. public static void CentreLabel(string text, GUIStyle style = null)
  67. {
  68. GUILayout.BeginHorizontal();
  69. GUILayout.FlexibleSpace();
  70. if (style == null)
  71. {
  72. GUILayout.Label(text);
  73. }
  74. else
  75. {
  76. GUILayout.Label(text, style);
  77. }
  78. GUILayout.FlexibleSpace();
  79. GUILayout.EndHorizontal();
  80. }
  81. public static void BoolAsDropdown(string name, SerializedProperty prop, string trueOption, string falseOption)
  82. {
  83. string[] popupNames = { trueOption, falseOption };
  84. int popupIndex = 0;
  85. if (!prop.boolValue)
  86. {
  87. popupIndex = 1;
  88. }
  89. popupIndex = EditorGUILayout.Popup(name, popupIndex, popupNames);
  90. prop.boolValue = (popupIndex == 0);
  91. }
  92. public static void EnumAsDropdown(string name, SerializedProperty prop, string[] options)
  93. {
  94. prop.enumValueIndex = EditorGUILayout.Popup(name, prop.enumValueIndex, options);
  95. }
  96. public static void IntAsDropdown(string name, SerializedProperty prop, string[] options, int[] values)
  97. {
  98. int index = 0;
  99. for (int i = 0; i < values.Length; i++)
  100. {
  101. if (values[i] == prop.intValue)
  102. {
  103. index = i;
  104. break;
  105. }
  106. }
  107. index = EditorGUILayout.Popup(name, index, options);
  108. prop.intValue = values[index];
  109. }
  110. public static void FloatAsDropdown(string name, SerializedProperty prop, string[] options, float[] values, bool customAtEnd)
  111. {
  112. bool isFound = false;
  113. int index = 0;
  114. for (int i = 0; i < values.Length; i++)
  115. {
  116. if (values[i] == prop.floatValue)
  117. {
  118. isFound = true;
  119. index = i;
  120. break;
  121. }
  122. }
  123. if (!isFound && customAtEnd)
  124. {
  125. index = options.Length - 1;
  126. }
  127. EditorGUI.BeginChangeCheck();
  128. if (string.IsNullOrEmpty(name))
  129. {
  130. index = EditorGUILayout.Popup(index, options);
  131. }
  132. else
  133. {
  134. index = EditorGUILayout.Popup(name, index, options);
  135. }
  136. if (EditorGUI.EndChangeCheck())
  137. {
  138. prop.floatValue = values[index];
  139. }
  140. }
  141. private struct FloatPopupData
  142. {
  143. public FloatPopupData(SerializedObject obj, SerializedProperty prop, float value, Object target)
  144. {
  145. _obj = obj;
  146. _prop = prop;
  147. _value = value;
  148. _target = target;
  149. }
  150. public void Apply()
  151. {
  152. _prop.floatValue = _value;
  153. if (_obj.ApplyModifiedProperties())
  154. {
  155. EditorUtility.SetDirty(_target);
  156. }
  157. }
  158. private Object _target;
  159. private SerializedObject _obj;
  160. private SerializedProperty _prop;
  161. private float _value;
  162. }
  163. private static void FloatAsPopupCallback_Select(object obj)
  164. {
  165. ((FloatPopupData)obj).Apply();
  166. }
  167. public static void FloatAsPopup(string buttonText, string popupText, SerializedObject obj, SerializedProperty prop, string[] options, float[] values)
  168. {
  169. if (GUILayout.Button(buttonText, GUILayout.ExpandWidth(false)))
  170. {
  171. // Remove focus to clear the selection, otherwise the property field will not update
  172. GUI.FocusControl(null);
  173. GenericMenu toolsMenu = new GenericMenu();
  174. toolsMenu.AddDisabledItem(new GUIContent(popupText));
  175. toolsMenu.AddSeparator("");
  176. for (int i = 0; i < options.Length; i++)
  177. {
  178. bool isSelected = (values[i] == prop.floatValue);
  179. toolsMenu.AddItem(new GUIContent(options[i]), isSelected, FloatAsPopupCallback_Select, new FloatPopupData(obj, prop, values[i], obj.targetObject));
  180. }
  181. toolsMenu.ShowAsContext();
  182. }
  183. }
  184. internal static BitrateUnits BitrateUnitsDisplay = BitrateUnits.MBitsPerSecond;
  185. internal static void BitrateField(string name, SerializedProperty prop)
  186. {
  187. GUILayout.BeginHorizontal();
  188. {
  189. double factor = 1.0;
  190. switch (BitrateUnitsDisplay)
  191. {
  192. case BitrateUnits.BitsPerSecond:
  193. factor = 1.0;
  194. break;
  195. case BitrateUnits.KBitsPerSecond:
  196. factor = 1000;
  197. break;
  198. case BitrateUnits.MBitsPerSecond:
  199. factor = 1000000;
  200. break;
  201. }
  202. double bitrate = (uint)prop.intValue / factor;
  203. bitrate = EditorGUILayout.DelayedDoubleField(name, bitrate);
  204. prop.intValue = (int)(bitrate * factor);
  205. }
  206. BitrateUnitsDisplay = (BitrateUnits)EditorGUILayout.Popup((int)BitrateUnitsDisplay, new string[] { "bps", "Kbps", "Mbps" }, GUILayout.Width(64f), GUILayout.MaxWidth(64f), GUILayout.ExpandWidth(false));
  207. GUILayout.EndHorizontal();
  208. }
  209. public static void DrawSection(string name, ref bool isExpanded, System.Action action)
  210. {
  211. Color boxbgColor = new Color(0.8f, 0.8f, 0.8f, 0.1f);
  212. if (EditorGUIUtility.isProSkin)
  213. {
  214. boxbgColor = Color.black;
  215. }
  216. DrawSectionColored(name, ref isExpanded, action, boxbgColor, Color.white, Color.white);
  217. }
  218. public static void DrawSectionColored(string name, ref bool isExpanded, System.Action action, Color boxbgcolor, Color bgcolor, Color color)
  219. {
  220. GUI.color = Color.white;
  221. GUI.backgroundColor = Color.clear;
  222. //GUI.backgroundColor = bgcolor;
  223. if (isExpanded)
  224. {
  225. GUI.color = Color.white;
  226. GUI.backgroundColor = boxbgcolor;
  227. }
  228. GUILayout.BeginVertical("box");
  229. GUI.color = color;
  230. GUI.backgroundColor = bgcolor;
  231. if (GUILayout.Button(name, EditorStyles.toolbarButton))
  232. {
  233. isExpanded = !isExpanded;
  234. }
  235. //GUI.backgroundColor = Color.white;
  236. //GUI.color = Color.white;
  237. if (isExpanded)
  238. {
  239. action.Invoke();
  240. }
  241. GUI.backgroundColor = Color.white;
  242. GUI.color = Color.white;
  243. GUILayout.EndVertical();
  244. }
  245. }
  246. }
  247. #endif