SequencePath.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.Timeline;
  7. namespace UnityEditor.Timeline
  8. {
  9. [Serializable]
  10. class SequencePath
  11. {
  12. [SerializeField] int m_SelectionRoot;
  13. public int selectionRoot
  14. {
  15. get { return m_SelectionRoot; }
  16. }
  17. [SerializeField] List<SequencePathSubElement> m_SubElements;
  18. public List<SequencePathSubElement> subElements
  19. {
  20. get { return m_SubElements ?? (m_SubElements = new List<SequencePathSubElement>()); }
  21. }
  22. public void SetSelectionRoot(int instanceID)
  23. {
  24. m_SelectionRoot = instanceID;
  25. subElements.Clear();
  26. }
  27. public void AddSubSequence(ISequenceState state, IExposedPropertyTable resolver)
  28. {
  29. subElements.Add(SequencePathSubElement.Create(state, resolver));
  30. }
  31. public void Clear()
  32. {
  33. m_SelectionRoot = 0;
  34. subElements.Clear();
  35. }
  36. public static bool AreEqual(SequencePath lhs, SequencePath rhs)
  37. {
  38. if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null)) return true;
  39. if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) return false;
  40. if (ReferenceEquals(lhs, rhs)) return true;
  41. var result = lhs.selectionRoot == rhs.selectionRoot &&
  42. lhs.subElements.Count == rhs.subElements.Count;
  43. if (!result)
  44. return false;
  45. for (int i = 0, n = lhs.subElements.Count; i < n; ++i)
  46. result = result && SequencePathSubElement.AreEqual(lhs.subElements[i], rhs.subElements[i]);
  47. return result;
  48. }
  49. public override string ToString()
  50. {
  51. var sb = new StringBuilder();
  52. sb.AppendFormat("[{0}]", m_SelectionRoot.ToString());
  53. if (m_SubElements != null && m_SubElements.Count > 0)
  54. {
  55. foreach (var element in m_SubElements)
  56. {
  57. sb.Append(" > ");
  58. sb.Append(element.ToString());
  59. }
  60. }
  61. return sb.ToString();
  62. }
  63. }
  64. [Serializable]
  65. class SequencePathSubElement
  66. {
  67. public int trackInstanceID;
  68. public int trackHash;
  69. public int clipIndex;
  70. public int clipHash;
  71. public int subDirectorIndex;
  72. public static SequencePathSubElement Create(ISequenceState state, IExposedPropertyTable resolver)
  73. {
  74. var clip = state.hostClip;
  75. Debug.Assert(clip != null);
  76. var track = clip.parentTrack;
  77. Debug.Assert(track != null);
  78. var asset = track.timelineAsset;
  79. Debug.Assert(asset != null);
  80. var directors = TimelineUtility.GetSubTimelines(clip, resolver as PlayableDirector);
  81. return new SequencePathSubElement
  82. {
  83. trackInstanceID = track.GetInstanceID(),
  84. trackHash = track.Hash(),
  85. clipIndex = Array.IndexOf(track.clips, clip),
  86. clipHash = clip.Hash(),
  87. subDirectorIndex = directors.IndexOf(state.director)
  88. };
  89. }
  90. public static bool AreEqual(SequencePathSubElement lhs, SequencePathSubElement rhs)
  91. {
  92. if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null)) return true;
  93. if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) return false;
  94. if (ReferenceEquals(lhs, rhs)) return true;
  95. return lhs.trackInstanceID == rhs.trackInstanceID &&
  96. lhs.trackHash == rhs.trackHash &&
  97. lhs.clipIndex == rhs.clipIndex &&
  98. lhs.clipHash == rhs.clipHash &&
  99. lhs.subDirectorIndex == rhs.subDirectorIndex;
  100. }
  101. public override string ToString()
  102. {
  103. return string.Format(
  104. "[track[{0}] ({1}) > clip[{2}] ({3})]",
  105. trackInstanceID.ToString(), trackHash.ToString(),
  106. clipIndex.ToString(), clipHash.ToString());
  107. }
  108. }
  109. }