TimelineChild.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. namespace Chronos
  3. {
  4. /// <summary>
  5. /// A component that transfers the effects of any ancestor Timeline in the hierarchy to the current GameObject.
  6. /// </summary>
  7. [AddComponentMenu("Time/Timeline Child")]
  8. [DisallowMultipleComponent]
  9. [HelpURL("http://ludiq.io/chronos/documentation#Timeline")]
  10. public class TimelineChild : TimelineEffector
  11. {
  12. /// <summary>
  13. /// The parent Timeline from which time effects are transfered.
  14. /// </summary>
  15. public Timeline parent { get; private set; }
  16. protected override Timeline timeline
  17. {
  18. get { return parent; }
  19. }
  20. protected override void Awake()
  21. {
  22. CacheParent();
  23. base.Awake();
  24. }
  25. /// <summary>
  26. /// The timeline hierarchy is cached for performance. If you change the transform hierarchy, you must call this method to update it.
  27. /// </summary>
  28. public void CacheParent()
  29. {
  30. var previousParent = parent;
  31. parent = GetComponentInParent<Timeline>();
  32. if (parent == null)
  33. {
  34. throw new ChronosException("Missing parent timeline for timeline child.");
  35. }
  36. if (previousParent != null)
  37. {
  38. previousParent.children.Remove(this);
  39. }
  40. parent.children.Add(this);
  41. }
  42. }
  43. }