EditorUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Reflection;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2012-2017 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. public static class EditorUtils
  42. {
  43. public static void CentreLabel(string text, GUIStyle style = null)
  44. {
  45. GUILayout.BeginHorizontal();
  46. GUILayout.FlexibleSpace();
  47. if (style == null)
  48. {
  49. GUILayout.Label(text);
  50. }
  51. else
  52. {
  53. GUILayout.Label(text, style);
  54. }
  55. GUILayout.FlexibleSpace();
  56. GUILayout.EndHorizontal();
  57. }
  58. public static void BoolAsDropdown(string name, SerializedProperty prop, string trueOption, string falseOption)
  59. {
  60. string[] popupNames = { trueOption, falseOption };
  61. int popupIndex = 0;
  62. if (!prop.boolValue)
  63. {
  64. popupIndex = 1;
  65. }
  66. popupIndex = EditorGUILayout.Popup(name, popupIndex, popupNames);
  67. prop.boolValue = (popupIndex == 0);
  68. }
  69. public static void EnumAsDropdown(string name, SerializedProperty prop, string[] options)
  70. {
  71. prop.enumValueIndex = EditorGUILayout.Popup(name, prop.enumValueIndex, options);
  72. }
  73. public static void IntAsDropdown(string name, SerializedProperty prop, string[] options, int[] values)
  74. {
  75. int index = 0;
  76. for (int i = 0; i < values.Length; i++)
  77. {
  78. if (values[i] == prop.intValue)
  79. {
  80. index = i;
  81. break;
  82. }
  83. }
  84. index = EditorGUILayout.Popup(name, index, options);
  85. prop.intValue = values[index];
  86. }
  87. public static void DrawSection(string name, ref bool isExpanded, System.Action action)
  88. {
  89. Color boxbgColor = new Color(0.8f, 0.8f, 0.8f, 0.1f);
  90. if (EditorGUIUtility.isProSkin)
  91. {
  92. boxbgColor = Color.black;
  93. }
  94. DrawSectionColored(name, ref isExpanded, action, boxbgColor, Color.white, Color.white);
  95. }
  96. public static void DrawSectionColored(string name, ref bool isExpanded, System.Action action, Color boxbgcolor, Color bgcolor, Color color)
  97. {
  98. GUI.color = Color.white;
  99. GUI.backgroundColor = Color.clear;
  100. //GUI.backgroundColor = bgcolor;
  101. if (isExpanded)
  102. {
  103. GUI.color = Color.white;
  104. GUI.backgroundColor = boxbgcolor;
  105. }
  106. GUILayout.BeginVertical("box");
  107. GUI.color = color;
  108. GUI.backgroundColor = bgcolor;
  109. if (GUILayout.Button(name, EditorStyles.toolbarButton))
  110. {
  111. isExpanded = !isExpanded;
  112. }
  113. //GUI.backgroundColor = Color.white;
  114. //GUI.color = Color.white;
  115. if (isExpanded)
  116. {
  117. action.Invoke();
  118. }
  119. GUI.backgroundColor = Color.white;
  120. GUI.color = Color.white;
  121. GUILayout.EndVertical();
  122. }
  123. }
  124. }
  125. #endif