InspectorUtility.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. return RuntimeUtility.NormalizeCurve(curve, true, true);
  94. }
  95. /// <summary>
  96. /// Remove the "Cinemachine" prefix, then call the standard Unity Nicify.
  97. /// </summary>
  98. /// <param name="name">The name to nicify</param>
  99. /// <returns>The nicified name</returns>
  100. public static string NicifyClassName(string name)
  101. {
  102. if (name.StartsWith("Cinemachine"))
  103. name = name.Substring(11); // Trim the prefix
  104. return ObjectNames.NicifyVariableName(name);
  105. }
  106. /// <summary>
  107. /// Add to a list all assets of a given type found in a given location
  108. /// </summary>
  109. /// <param name="type">The asset type to look for</param>
  110. /// <param name="assets">The list to add found assets to</param>
  111. /// <param name="path">The location in which to look. Path is relative to package root.</param>
  112. public static void AddAssetsFromPackageSubDirectory(
  113. Type type, List<ScriptableObject> assets, string path)
  114. {
  115. try
  116. {
  117. path = "/" + path;
  118. var info = new DirectoryInfo(ScriptableObjectUtility.CinemachineInstallPath + path);
  119. path = ScriptableObjectUtility.kPackageRoot + path + "/";
  120. var fileInfo = info.GetFiles();
  121. foreach (var file in fileInfo)
  122. {
  123. if (file.Extension != ".asset")
  124. continue;
  125. string name = path + file.Name;
  126. ScriptableObject a = AssetDatabase.LoadAssetAtPath(name, type) as ScriptableObject;
  127. if (a != null)
  128. assets.Add(a);
  129. }
  130. }
  131. catch
  132. {
  133. }
  134. }
  135. // Temporarily here
  136. /// <summary>
  137. /// Creates a new GameObject.
  138. /// </summary>
  139. /// <param name="name">Name to give the object.</param>
  140. /// <param name="types">Optional components to add.</param>
  141. /// <returns>The GameObject that was created.</returns>
  142. [Obsolete("Use ObjectFactory.CreateGameObject(string name, params Type[] types) instead.")]
  143. public static GameObject CreateGameObject(string name, params Type[] types)
  144. {
  145. return ObjectFactory.CreateGameObject(name, types);
  146. }
  147. private static int m_lastRepaintFrame;
  148. /// <summary>
  149. /// Force a repaint of the Game View
  150. /// </summary>
  151. /// <param name="unused">Like it says</param>
  152. public static void RepaintGameView(UnityEngine.Object unused = null)
  153. {
  154. if (m_lastRepaintFrame == Time.frameCount)
  155. return;
  156. m_lastRepaintFrame = Time.frameCount;
  157. EditorApplication.QueuePlayerLoopUpdate();
  158. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  159. }
  160. /// <summary>
  161. /// Try to get the name of the owning virtual camera oibject. If none then use
  162. /// the object's name
  163. /// </summary>
  164. /// <param name="property"></param>
  165. /// <returns></returns>
  166. internal static string GetVirtualCameraObjectName(SerializedProperty property)
  167. {
  168. // A little hacky here, as we favour virtual cameras...
  169. var obj = property.serializedObject.targetObject;
  170. GameObject go = obj as GameObject;
  171. if (go == null)
  172. {
  173. var component = obj as Component;
  174. if (component != null)
  175. go = component.gameObject;
  176. }
  177. if (go != null)
  178. {
  179. var vcam = go.GetComponentInParent<CinemachineVirtualCameraBase>();
  180. if (vcam != null)
  181. return vcam.Name;
  182. return go.name;
  183. }
  184. return obj.name;
  185. }
  186. }
  187. }