CinemachineTriggerAction.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #if !UNITY_2019_3_OR_NEWER
  2. #define CINEMACHINE_PHYSICS
  3. #define CINEMACHINE_PHYSICS_2D
  4. #endif
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.Playables;
  10. namespace Cinemachine
  11. {
  12. #if !(CINEMACHINE_PHYSICS || CINEMACHINE_PHYSICS_2D)
  13. // Workaround for Unity scripting bug
  14. [AddComponentMenu("")] // Hide in menu
  15. public class CinemachineTriggerAction : MonoBehaviour {}
  16. #else
  17. /// <summary>
  18. /// A multi-purpose script which causes an action to occur when
  19. /// a trigger collider is entered and exited.
  20. /// </summary>
  21. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  22. [SaveDuringPlay]
  23. [HelpURL(Documentation.BaseURL + "api/Cinemachine.CinemachineTriggerAction.html")]
  24. public class CinemachineTriggerAction : MonoBehaviour
  25. {
  26. /// <summary>Only triggers generated by objects on these layers will be considered.</summary>
  27. [Header("Trigger Object Filter")]
  28. [Tooltip("Only triggers generated by objects on these layers will be considered")]
  29. public LayerMask m_LayerMask = 1;
  30. /// <summary>If set, only triggers generated by objects with this tag will be considered</summary>
  31. [TagField]
  32. [Tooltip("If set, only triggers generated by objects with this tag will be considered")]
  33. public string m_WithTag = string.Empty;
  34. /// <summary>Triggers generated by objects with this tag will be ignored</summary>
  35. [TagField]
  36. [Tooltip("Triggers generated by objects with this tag will be ignored")]
  37. public string m_WithoutTag = string.Empty;
  38. /// <summary>Skip this many trigger entries before taking action</summary>
  39. [NoSaveDuringPlay]
  40. [Tooltip("Skip this many trigger entries before taking action")]
  41. public int m_SkipFirst = 0;
  42. /// <summary>Repeat the action for all subsequent trigger entries</summary>
  43. [Tooltip("Repeat the action for all subsequent trigger entries")]
  44. public bool m_Repeating = true;
  45. /// <summary>Defines what action to take on trigger enter/exit</summary>
  46. [Serializable]
  47. public struct ActionSettings
  48. {
  49. /// <summary>What action to take</summary>
  50. public enum Mode
  51. {
  52. /// <summary>Use the event only</summary>
  53. Custom,
  54. /// <summary>Boost priority of virtual camera target</summary>
  55. PriorityBoost,
  56. /// <summary>Activate the target GameObject</summary>
  57. Activate,
  58. /// <summary>Decativate target GameObject</summary>
  59. Deactivate,
  60. /// <summary>Enable a component</summary>
  61. Enable,
  62. /// <summary>Disable a component</summary>
  63. Disable,
  64. /// <summary>Start animation on target</summary>
  65. Play,
  66. /// <summary>Stop animation on target</summary>
  67. Stop
  68. }
  69. /// <summary>Serializable parameterless game event</summary>
  70. [Serializable] public class TriggerEvent : UnityEvent {}
  71. /// <summary>What action to take</summary>
  72. [Tooltip("What action to take")]
  73. public Mode m_Action;
  74. /// <summary>The target object on which to operate. If null, then the current behaviour/GameObject will be used</summary>
  75. [Tooltip("The target object on which to operate. If null, then the current behaviour/GameObject will be used")]
  76. public UnityEngine.Object m_Target;
  77. /// <summary>If PriorityBoost, this amount will be added to the virtual camera's priority</summary>
  78. [Tooltip("If PriorityBoost, this amount will be added to the virtual camera's priority")]
  79. public int m_BoostAmount;
  80. /// <summary>If playing a timeline, start at this time</summary>
  81. [Tooltip("If playing a timeline, start at this time")]
  82. public float m_StartTime;
  83. /// <summary>How to interpret the start time</summary>
  84. public enum TimeMode { FromStart, FromEnd, BeforeNow, AfterNow };
  85. /// <summary>How to interpret the start time</summary>
  86. [Tooltip("How to interpret the start time")]
  87. public TimeMode m_Mode;
  88. /// <summary>This event will be invoked</summary>
  89. [Tooltip("This event will be invoked")]
  90. public TriggerEvent m_Event;
  91. /// <summary>Constructor</summary>
  92. public ActionSettings(Mode action)
  93. {
  94. m_Action = action;
  95. m_Target = null;
  96. m_BoostAmount = 0;
  97. m_StartTime = 0;
  98. m_Mode = TimeMode.FromStart;
  99. m_Event = new TriggerEvent();
  100. }
  101. /// <summary>Invoke the action. Depending on the mode, different action will
  102. /// be performed. The embedded event will always be invoked, in addition to the
  103. /// action specified by the Mode.</summary>
  104. public void Invoke()
  105. {
  106. UnityEngine.Object currentTarget = m_Target;
  107. if (currentTarget != null)
  108. {
  109. GameObject targetGameObject = currentTarget as GameObject;
  110. Behaviour targetBehaviour = currentTarget as Behaviour;
  111. if (targetBehaviour != null)
  112. targetGameObject = targetBehaviour.gameObject;
  113. switch (m_Action)
  114. {
  115. case Mode.Custom:
  116. break;
  117. case Mode.PriorityBoost:
  118. {
  119. CinemachineVirtualCameraBase vcam
  120. = targetGameObject.GetComponent<CinemachineVirtualCameraBase>();
  121. if (vcam != null)
  122. {
  123. vcam.Priority += m_BoostAmount;
  124. vcam.MoveToTopOfPrioritySubqueue();
  125. }
  126. break;
  127. }
  128. case Mode.Activate:
  129. if (targetGameObject != null)
  130. {
  131. targetGameObject.SetActive(true);
  132. CinemachineVirtualCameraBase vcam
  133. = targetGameObject.GetComponent<CinemachineVirtualCameraBase>();
  134. if (vcam != null)
  135. vcam.MoveToTopOfPrioritySubqueue();
  136. }
  137. break;
  138. case Mode.Deactivate:
  139. if (targetGameObject != null)
  140. targetGameObject.SetActive(false);
  141. break;
  142. case Mode.Enable:
  143. {
  144. if (targetBehaviour != null)
  145. targetBehaviour.enabled = true;
  146. break;
  147. }
  148. case Mode.Disable:
  149. {
  150. if (targetBehaviour != null)
  151. targetBehaviour.enabled = false;
  152. break;
  153. }
  154. case Mode.Play:
  155. {
  156. PlayableDirector playable
  157. = targetGameObject.GetComponent<PlayableDirector>();
  158. if (playable != null)
  159. {
  160. double startTime = 0;
  161. double duration = playable.duration;
  162. double current = playable.time;
  163. switch (m_Mode)
  164. {
  165. default:
  166. case TimeMode.FromStart:
  167. startTime += m_StartTime;
  168. break;
  169. case TimeMode.FromEnd:
  170. startTime = duration - m_StartTime;
  171. break;
  172. case TimeMode.BeforeNow:
  173. startTime = current - m_StartTime;
  174. break;
  175. case TimeMode.AfterNow:
  176. startTime = current + m_StartTime;
  177. break;
  178. }
  179. playable.time = startTime;
  180. playable.Play();
  181. }
  182. else
  183. {
  184. Animation ani = targetGameObject.GetComponent<Animation>();
  185. if (ani != null)
  186. ani.Play();
  187. }
  188. break;
  189. }
  190. case Mode.Stop:
  191. {
  192. PlayableDirector playable
  193. = targetGameObject.GetComponent<PlayableDirector>();
  194. if (playable != null)
  195. playable.Stop();
  196. else
  197. {
  198. Animation ani = targetGameObject.GetComponent<Animation>();
  199. if (ani != null)
  200. ani.Stop();
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. m_Event.Invoke();
  207. }
  208. }
  209. /// <summary>What action to take when an eligible object enters the collider or trigger zone</summary>
  210. public ActionSettings m_OnObjectEnter = new ActionSettings(ActionSettings.Mode.Custom);
  211. /// <summary>What action to take when an eligible object exits the collider or trigger zone</summary>
  212. public ActionSettings m_OnObjectExit = new ActionSettings(ActionSettings.Mode.Custom);
  213. HashSet<GameObject> m_ActiveTriggerObjects = new HashSet<GameObject>();
  214. private bool Filter(GameObject other)
  215. {
  216. if (!enabled)
  217. return false;
  218. if (((1 << other.layer) & m_LayerMask) == 0)
  219. return false;
  220. if (m_WithTag.Length != 0 && !other.CompareTag(m_WithTag))
  221. return false;
  222. if (m_WithoutTag.Length != 0 && other.CompareTag(m_WithoutTag))
  223. return false;
  224. return true;
  225. }
  226. void InternalDoTriggerEnter(GameObject other)
  227. {
  228. if (!Filter(other))
  229. return;
  230. --m_SkipFirst;
  231. if (m_SkipFirst > -1)
  232. return;
  233. if (!m_Repeating && m_SkipFirst != -1)
  234. return;
  235. m_ActiveTriggerObjects.Add(other);
  236. m_OnObjectEnter.Invoke();
  237. }
  238. void InternalDoTriggerExit(GameObject other)
  239. {
  240. if (!m_ActiveTriggerObjects.Contains(other))
  241. return;
  242. m_ActiveTriggerObjects.Remove(other);
  243. if (enabled)
  244. m_OnObjectExit.Invoke();
  245. }
  246. #if CINEMACHINE_PHYSICS
  247. void OnTriggerEnter(Collider other) { InternalDoTriggerEnter(other.gameObject); }
  248. void OnTriggerExit(Collider other) { InternalDoTriggerExit(other.gameObject); }
  249. void OnCollisionEnter(Collision other) { InternalDoTriggerEnter(other.gameObject); }
  250. void OnCollisionExit(Collision other) { InternalDoTriggerExit(other.gameObject); }
  251. #endif
  252. #if CINEMACHINE_PHYSICS_2D
  253. void OnTriggerEnter2D(Collider2D other) { InternalDoTriggerEnter(other.gameObject); }
  254. void OnTriggerExit2D(Collider2D other) { InternalDoTriggerExit(other.gameObject); }
  255. void OnCollisionEnter2D(Collision2D other) { InternalDoTriggerEnter(other.gameObject); }
  256. void OnCollisionExit2D(Collision2D other) { InternalDoTriggerExit(other.gameObject); }
  257. #endif
  258. void OnEnable() {} // For the Enabled checkbox
  259. }
  260. #endif
  261. }