InspectorUtility.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace Cinemachine.Editor
  7. {
  8. /// <summary>
  9. /// Collection of tools and helpers for drawing inspectors
  10. /// </summary>
  11. public class InspectorUtility
  12. {
  13. /// <summary>Put multiple properties on a single inspector line, with
  14. /// optional label overrides. Passing null as a label (or sublabel) override will
  15. /// cause the property's displayName to be used as a label. For no label at all,
  16. /// pass GUIContent.none.</summary>
  17. /// <param name="rect">Rect in which to draw</param>
  18. /// <param name="label">Main label</param>
  19. /// <param name="props">Properties to place on the line</param>
  20. /// <param name="subLabels">Sublabels for the properties</param>
  21. public static void MultiPropertyOnLine(
  22. Rect rect,
  23. GUIContent label,
  24. SerializedProperty[] props, GUIContent[] subLabels)
  25. {
  26. if (props == null || props.Length == 0)
  27. return;
  28. const int hSpace = 2;
  29. int indentLevel = EditorGUI.indentLevel;
  30. float labelWidth = EditorGUIUtility.labelWidth;
  31. float totalSubLabelWidth = 0;
  32. int numBoolColumns = 0;
  33. List<GUIContent> actualLabels = new List<GUIContent>();
  34. for (int i = 0; i < props.Length; ++i)
  35. {
  36. GUIContent sublabel = new GUIContent(props[i].displayName, props[i].tooltip);
  37. if (subLabels != null && subLabels.Length > i && subLabels[i] != null)
  38. sublabel = subLabels[i];
  39. actualLabels.Add(sublabel);
  40. totalSubLabelWidth += GUI.skin.label.CalcSize(sublabel).x;
  41. if (i > 0)
  42. totalSubLabelWidth += hSpace;
  43. // Special handling for toggles, or it looks stupid
  44. if (props[i].propertyType == SerializedPropertyType.Boolean)
  45. {
  46. totalSubLabelWidth += rect.height;
  47. ++numBoolColumns;
  48. }
  49. }
  50. float subFieldWidth = rect.width - labelWidth - totalSubLabelWidth;
  51. float numCols = props.Length - numBoolColumns;
  52. float colWidth = numCols == 0 ? 0 : subFieldWidth / numCols;
  53. // Main label. If no first sublabel, then main label must take on that
  54. // role, for mouse dragging value-scrolling support
  55. int subfieldStartIndex = 0;
  56. if (label == null)
  57. label = new GUIContent(props[0].displayName, props[0].tooltip);
  58. if (actualLabels[0] != GUIContent.none)
  59. rect = EditorGUI.PrefixLabel(rect, label);
  60. else
  61. {
  62. rect.width = labelWidth + colWidth;
  63. EditorGUI.PropertyField(rect, props[0], label);
  64. rect.x += rect.width + hSpace;
  65. subfieldStartIndex = 1;
  66. }
  67. for (int i = subfieldStartIndex; i < props.Length; ++i)
  68. {
  69. EditorGUI.indentLevel = 0;
  70. EditorGUIUtility.labelWidth = GUI.skin.label.CalcSize(actualLabels[i]).x;
  71. if (props[i].propertyType == SerializedPropertyType.Boolean)
  72. {
  73. rect.width = EditorGUIUtility.labelWidth + rect.height;
  74. props[i].boolValue = EditorGUI.ToggleLeft(rect, actualLabels[i], props[i].boolValue);
  75. }
  76. else
  77. {
  78. rect.width = EditorGUIUtility.labelWidth + colWidth;
  79. EditorGUI.PropertyField(rect, props[i], actualLabels[i]);
  80. }
  81. rect.x += rect.width + hSpace;
  82. }
  83. EditorGUIUtility.labelWidth = labelWidth;
  84. EditorGUI.indentLevel = indentLevel;
  85. }
  86. /// <summary>
  87. /// Normalize a curve so that each of X and Y axes ranges from 0 to 1
  88. /// </summary>
  89. /// <param name="curve">Curve to normalize</param>
  90. /// <returns>The normalized curve</returns>
  91. public static AnimationCurve NormalizeCurve(AnimationCurve curve)
  92. {
  93. Keyframe[] keys = curve.keys;
  94. if (keys.Length > 0)
  95. {
  96. float minTime = keys[0].time;
  97. float maxTime = minTime;
  98. float minVal = keys[0].value;
  99. float maxVal = minVal;
  100. for (int i = 0; i < keys.Length; ++i)
  101. {
  102. minTime = Mathf.Min(minTime, keys[i].time);
  103. maxTime = Mathf.Max(maxTime, keys[i].time);
  104. minVal = Mathf.Min(minVal, keys[i].value);
  105. maxVal = Mathf.Max(maxVal, keys[i].value);
  106. }
  107. float range = maxTime - minTime;
  108. float timeScale = range < 0.0001f ? 1 : 1 / range;
  109. range = maxVal - minVal;
  110. float valScale = range < 1 ? 1 : 1 / range;
  111. float valOffset = 0;
  112. if (range < 1)
  113. {
  114. if (minVal > 0 && minVal + range <= 1)
  115. valOffset = minVal;
  116. else
  117. valOffset = 1 - range;
  118. }
  119. for (int i = 0; i < keys.Length; ++i)
  120. {
  121. keys[i].time = (keys[i].time - minTime) * timeScale;
  122. keys[i].value = ((keys[i].value - minVal) * valScale) + valOffset;
  123. }
  124. curve.keys = keys;
  125. }
  126. return curve;
  127. }
  128. /// <summary>
  129. /// Remove the "Cinemachine" prefix, then call the standard Unity Nicify.
  130. /// </summary>
  131. /// <param name="name">The name to nicify</param>
  132. /// <returns>The nicified name</returns>
  133. public static string NicifyClassName(string name)
  134. {
  135. if (name.StartsWith("Cinemachine"))
  136. name = name.Substring(11); // Trim the prefix
  137. return ObjectNames.NicifyVariableName(name);
  138. }
  139. /// <summary>
  140. /// Add to a list all assets of a given type found in a given location
  141. /// </summary>
  142. /// <param name="type">The asset type to look for</param>
  143. /// <param name="assets">The list to add found assets to</param>
  144. /// <param name="path">The location in which to look. Path is relative to package root.</param>
  145. public static void AddAssetsFromPackageSubDirectory(
  146. Type type, List<ScriptableObject> assets, string path)
  147. {
  148. try
  149. {
  150. path = "/" + path;
  151. var info = new DirectoryInfo(ScriptableObjectUtility.CinemachineInstallPath + path);
  152. path = ScriptableObjectUtility.kPackageRoot + path + "/";
  153. var fileInfo = info.GetFiles();
  154. foreach (var file in fileInfo)
  155. {
  156. if (file.Extension != ".asset")
  157. continue;
  158. string name = path + file.Name;
  159. ScriptableObject a = AssetDatabase.LoadAssetAtPath(name, type) as ScriptableObject;
  160. if (a != null)
  161. assets.Add(a);
  162. }
  163. }
  164. catch
  165. {
  166. }
  167. }
  168. // Temporarily here
  169. /// <summary>
  170. /// Create a game object. Uses ObjectFactory if the Unity version is sufficient.
  171. /// </summary>
  172. /// <param name="name">Name to give the object</param>
  173. /// <param name="types">Optional components to add</param>
  174. /// <returns></returns>
  175. public static GameObject CreateGameObject(string name, params Type[] types)
  176. {
  177. #if UNITY_2018_3_OR_NEWER
  178. return ObjectFactory.CreateGameObject(name, types);
  179. #else
  180. return new GameObject(name, types);
  181. #endif
  182. }
  183. /// <summary>
  184. /// Force a repaint of the Game View
  185. /// </summary>
  186. /// <param name="unused">Like it says</param>
  187. public static void RepaintGameView(UnityEngine.Object unused = null)
  188. {
  189. EditorApplication.QueuePlayerLoopUpdate();
  190. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  191. }
  192. /// <summary>
  193. /// Try to get the name of the owning virtual camera oibject. If none then use
  194. /// the object's name
  195. /// </summary>
  196. /// <param name="property"></param>
  197. /// <returns></returns>
  198. internal static string GetVirtualCameraObjectName(SerializedProperty property)
  199. {
  200. // A little hacky here, as we favour virtual cameras...
  201. var obj = property.serializedObject.targetObject;
  202. GameObject go = obj as GameObject;
  203. if (go == null)
  204. {
  205. var component = obj as Component;
  206. if (component != null)
  207. go = component.gameObject;
  208. }
  209. if (go != null)
  210. {
  211. var vcam = go.GetComponentInParent<CinemachineVirtualCameraBase>();
  212. if (vcam != null)
  213. return vcam.Name;
  214. return go.name;
  215. }
  216. return obj.name;
  217. }
  218. }
  219. }