ControlPlayableInspector.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. [CustomEditor(typeof(ControlPlayableAsset)), CanEditMultipleObjects]
  9. class ControlPlayableInspector : Editor
  10. {
  11. static class Styles
  12. {
  13. static string s_DisabledBecauseOfSelfControlTooltip = "Must be disabled when the Source Game Object references the same PlayableDirector component that is being controlled";
  14. public static readonly GUIContent activationContent = EditorGUIUtility.TrTextContent("Control Activation", "When checked the clip will control the active state of the source game object");
  15. public static readonly GUIContent activationDisabledContent = EditorGUIUtility.TextContent("Control Activation|" + s_DisabledBecauseOfSelfControlTooltip);
  16. public static readonly GUIContent prefabContent = EditorGUIUtility.TrTextContent("Prefab", "A prefab to instantiate as a child object of the source game object");
  17. public static readonly GUIContent advancedContent = EditorGUIUtility.TrTextContent("Advanced");
  18. public static readonly GUIContent updateParticleSystemsContent = EditorGUIUtility.TrTextContent("Control Particle Systems", "Synchronize the time between the clip and any particle systems on the game object");
  19. public static readonly GUIContent updatePlayableDirectorContent = EditorGUIUtility.TrTextContent("Control Playable Directors", "Synchronize the time between the clip and any playable directors on the game object");
  20. public static readonly GUIContent updatePlayableDirectorDisabledContent = EditorGUIUtility.TextContent("Control Playable Directors|" + s_DisabledBecauseOfSelfControlTooltip);
  21. public static readonly GUIContent updateITimeControlContent = EditorGUIUtility.TrTextContent("Control ITimeControl", "Synchronize the time between the clip and any Script that implements the ITimeControl interface on the game object");
  22. public static readonly GUIContent updateHierarchy = EditorGUIUtility.TrTextContent("Control Children", "Search child game objects for particle systems and playable directors");
  23. public static readonly GUIContent randomSeedContent = EditorGUIUtility.TrTextContent("Random Seed", "A random seed to provide the particle systems for consistent previews. This will only be used on particle systems where AutoRandomSeed is on.");
  24. public static readonly GUIContent postPlayableContent = EditorGUIUtility.TrTextContent("Post Playback", "The active state to the leave the game object when the timeline is finished. \n\nRevert will leave the game object in the state it was prior to the timeline being run");
  25. }
  26. SerializedProperty m_SourceObject;
  27. SerializedProperty m_PrefabObject;
  28. SerializedProperty m_UpdateParticle;
  29. SerializedProperty m_UpdateDirector;
  30. SerializedProperty m_UpdateITimeControl;
  31. SerializedProperty m_SearchHierarchy;
  32. SerializedProperty m_UseActivation;
  33. SerializedProperty m_PostPlayback;
  34. SerializedProperty m_RandomSeed;
  35. bool m_CycleReference;
  36. GUIContent m_SourceObjectLabel = new GUIContent();
  37. // the director that the selection was made with. Normally this matches the active director in timeline,
  38. // but persists if the active timeline changes (case 962516)
  39. private PlayableDirector contextDirector
  40. {
  41. get
  42. {
  43. if (serializedObject == null)
  44. return null;
  45. return serializedObject.context as PlayableDirector;
  46. }
  47. }
  48. public void OnEnable()
  49. {
  50. if (target == null) // case 946080
  51. return;
  52. m_SourceObject = serializedObject.FindProperty("sourceGameObject");
  53. m_PrefabObject = serializedObject.FindProperty("prefabGameObject");
  54. m_UpdateParticle = serializedObject.FindProperty("updateParticle");
  55. m_UpdateDirector = serializedObject.FindProperty("updateDirector");
  56. m_UpdateITimeControl = serializedObject.FindProperty("updateITimeControl");
  57. m_SearchHierarchy = serializedObject.FindProperty("searchHierarchy");
  58. m_UseActivation = serializedObject.FindProperty("active");
  59. m_PostPlayback = serializedObject.FindProperty("postPlayback");
  60. m_RandomSeed = serializedObject.FindProperty("particleRandomSeed");
  61. CheckForCyclicReference();
  62. }
  63. public override void OnInspectorGUI()
  64. {
  65. if (target == null)
  66. return;
  67. serializedObject.Update();
  68. m_SourceObjectLabel.text = m_SourceObject.displayName;
  69. if (m_PrefabObject.objectReferenceValue != null)
  70. m_SourceObjectLabel.text = "Parent Object";
  71. bool selfControlled = false;
  72. EditorGUI.BeginChangeCheck();
  73. using (new GUIMixedValueScope(m_SourceObject.hasMultipleDifferentValues))
  74. EditorGUILayout.PropertyField(m_SourceObject, m_SourceObjectLabel);
  75. var sourceGameObject = m_SourceObject.exposedReferenceValue as GameObject;
  76. selfControlled = m_PrefabObject.objectReferenceValue == null && TimelineWindow.instance != null && TimelineWindow.instance.state != null &&
  77. contextDirector != null && sourceGameObject == contextDirector.gameObject;
  78. if (EditorGUI.EndChangeCheck())
  79. {
  80. CheckForCyclicReference();
  81. if (!selfControlled)
  82. DisablePlayOnAwake(sourceGameObject);
  83. }
  84. if (selfControlled)
  85. {
  86. EditorGUILayout.HelpBox("The assigned GameObject references the same PlayableDirector component being controlled.", MessageType.Warning);
  87. }
  88. else if (m_CycleReference)
  89. {
  90. EditorGUILayout.HelpBox("The assigned GameObject contains a PlayableDirector component that results in a circular reference.", MessageType.Warning);
  91. }
  92. EditorGUI.indentLevel++;
  93. EditorGUILayout.PropertyField(m_PrefabObject, Styles.prefabContent);
  94. EditorGUI.indentLevel--;
  95. using (new EditorGUI.DisabledScope(selfControlled))
  96. {
  97. EditorGUILayout.PropertyField(m_UseActivation, selfControlled ? Styles.activationDisabledContent : Styles.activationContent);
  98. if (m_UseActivation.boolValue)
  99. {
  100. EditorGUI.indentLevel++;
  101. EditorGUILayout.PropertyField(m_PostPlayback, Styles.postPlayableContent);
  102. EditorGUI.indentLevel--;
  103. }
  104. }
  105. m_SourceObject.isExpanded = EditorGUILayout.Foldout(m_SourceObject.isExpanded, Styles.advancedContent);
  106. if (m_SourceObject.isExpanded)
  107. {
  108. EditorGUI.indentLevel++;
  109. using (new EditorGUI.DisabledScope(selfControlled && !m_SearchHierarchy.boolValue))
  110. {
  111. EditorGUI.BeginChangeCheck();
  112. EditorGUILayout.PropertyField(m_UpdateDirector, selfControlled ? Styles.updatePlayableDirectorDisabledContent : Styles.updatePlayableDirectorContent);
  113. if (EditorGUI.EndChangeCheck())
  114. {
  115. CheckForCyclicReference();
  116. }
  117. }
  118. EditorGUILayout.PropertyField(m_UpdateParticle, Styles.updateParticleSystemsContent);
  119. if (m_UpdateParticle.boolValue)
  120. {
  121. EditorGUI.indentLevel++;
  122. EditorGUILayout.PropertyField(m_RandomSeed, Styles.randomSeedContent);
  123. EditorGUI.indentLevel--;
  124. }
  125. EditorGUILayout.PropertyField(m_UpdateITimeControl, Styles.updateITimeControlContent);
  126. EditorGUILayout.PropertyField(m_SearchHierarchy, Styles.updateHierarchy);
  127. EditorGUI.indentLevel--;
  128. }
  129. serializedObject.ApplyModifiedProperties();
  130. }
  131. //
  132. // Fix for a workflow issue where scene objects with directors have play on awake by default enabled.
  133. // This causes confusion when the director is played within another director, so we disable it on assignment
  134. // to avoid the issue, but not force the issue on the user
  135. public void DisablePlayOnAwake(GameObject sourceObject)
  136. {
  137. if (sourceObject != null && m_UpdateDirector.boolValue)
  138. {
  139. if (m_SearchHierarchy.boolValue)
  140. {
  141. var directors = sourceObject.GetComponentsInChildren<PlayableDirector>();
  142. foreach (var d in directors)
  143. {
  144. DisablePlayOnAwake(d);
  145. }
  146. }
  147. else
  148. {
  149. DisablePlayOnAwake(sourceObject.GetComponent<PlayableDirector>());
  150. }
  151. }
  152. }
  153. public void DisablePlayOnAwake(PlayableDirector director)
  154. {
  155. if (director == null)
  156. return;
  157. var obj = new SerializedObject(director);
  158. var prop = obj.FindProperty("m_InitialState");
  159. prop.enumValueIndex = (int)PlayState.Paused;
  160. obj.ApplyModifiedProperties();
  161. }
  162. void CheckForCyclicReference()
  163. {
  164. serializedObject.ApplyModifiedProperties();
  165. m_CycleReference = false;
  166. PlayableDirector director = contextDirector;
  167. if (contextDirector == null)
  168. return;
  169. foreach (var asset in targets.OfType<ControlPlayableAsset>())
  170. {
  171. if (ControlPlayableUtility.DetectCycle(asset, director))
  172. {
  173. m_CycleReference = true;
  174. return;
  175. }
  176. }
  177. }
  178. }
  179. }