TimelineWindowViewPrefs.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using UnityEngine;
  2. using UnityEngine.Timeline;
  3. using UnityObject = UnityEngine.Object;
  4. using ViewModelsMap = System.Collections.Generic.Dictionary<UnityEngine.Timeline.TimelineAsset, UnityEditor.Timeline.ScriptableObjectViewPrefs<UnityEditor.Timeline.TimelineAssetViewModel>>;
  5. using ViewModelsList = System.Collections.Generic.List<UnityEditor.Timeline.ScriptableObjectViewPrefs<UnityEditor.Timeline.TimelineAssetViewModel>>;
  6. namespace UnityEditor.Timeline
  7. {
  8. static class TimelineWindowViewPrefs
  9. {
  10. public const string FilePath = "Library/Timeline";
  11. static readonly ViewModelsMap k_ViewModelsMap = new ViewModelsMap();
  12. static readonly ViewModelsList k_UnassociatedViewModels = new ViewModelsList();
  13. public static int viewModelCount
  14. {
  15. get { return k_ViewModelsMap.Count + k_UnassociatedViewModels.Count; }
  16. }
  17. public static TimelineAssetViewModel GetOrCreateViewModel(TimelineAsset asset)
  18. {
  19. if (asset == null)
  20. return CreateUnassociatedViewModel();
  21. ScriptableObjectViewPrefs<TimelineAssetViewModel> vm;
  22. if (k_ViewModelsMap.TryGetValue(asset, out vm))
  23. return vm.viewModel;
  24. return CreateViewModel(asset).viewModel;
  25. }
  26. public static TimelineAssetViewModel CreateUnassociatedViewModel()
  27. {
  28. var vm = new ScriptableObjectViewPrefs<TimelineAssetViewModel>(null, FilePath);
  29. k_UnassociatedViewModels.Add(vm);
  30. return vm.viewModel;
  31. }
  32. static ScriptableObjectViewPrefs<TimelineAssetViewModel> CreateViewModel(TimelineAsset asset)
  33. {
  34. var vm = new ScriptableObjectViewPrefs<TimelineAssetViewModel>(asset, FilePath);
  35. k_ViewModelsMap.Add(asset, vm);
  36. return vm;
  37. }
  38. public static void SaveViewModel(TimelineAsset asset)
  39. {
  40. if (asset == null)
  41. return;
  42. ScriptableObjectViewPrefs<TimelineAssetViewModel> vm;
  43. if (!k_ViewModelsMap.TryGetValue(asset, out vm))
  44. vm = CreateViewModel(asset);
  45. vm.Save();
  46. }
  47. public static void SaveAll()
  48. {
  49. foreach (var kvp in k_ViewModelsMap)
  50. kvp.Value.Save();
  51. }
  52. public static void UnloadViewModel(TimelineAsset asset)
  53. {
  54. ScriptableObjectViewPrefs<TimelineAssetViewModel> vm;
  55. if (k_ViewModelsMap.TryGetValue(asset, out vm))
  56. {
  57. vm.Dispose();
  58. k_ViewModelsMap.Remove(asset);
  59. }
  60. }
  61. public static void UnloadAllViewModels()
  62. {
  63. foreach (var kvp in k_ViewModelsMap)
  64. kvp.Value.Dispose();
  65. foreach (var vm in k_UnassociatedViewModels)
  66. vm.Dispose();
  67. k_ViewModelsMap.Clear();
  68. k_UnassociatedViewModels.Clear();
  69. }
  70. public static TrackViewModelData GetTrackViewModelData(TrackAsset track)
  71. {
  72. if (track == null)
  73. return new TrackViewModelData();
  74. if (track.timelineAsset == null)
  75. return new TrackViewModelData();
  76. var prefs = GetOrCreateViewModel(track.timelineAsset);
  77. TrackViewModelData trackData;
  78. if (prefs.tracksViewModelData.TryGetValue(track, out trackData))
  79. {
  80. return trackData;
  81. }
  82. trackData = new TrackViewModelData();
  83. prefs.tracksViewModelData[track] = trackData;
  84. return trackData;
  85. }
  86. public static bool IsTrackCollapsed(TrackAsset track)
  87. {
  88. if (track == null)
  89. return true;
  90. return GetTrackViewModelData(track).collapsed;
  91. }
  92. public static void SetTrackCollapsed(TrackAsset track, bool collapsed)
  93. {
  94. if (track == null)
  95. return;
  96. GetTrackViewModelData(track).collapsed = collapsed;
  97. }
  98. public static bool IsShowMarkers(TrackAsset track)
  99. {
  100. if (track == null)
  101. return true;
  102. return GetTrackViewModelData(track).showMarkers;
  103. }
  104. public static void SetTrackShowMarkers(TrackAsset track, bool collapsed)
  105. {
  106. if (track == null)
  107. return;
  108. GetTrackViewModelData(track).showMarkers = collapsed;
  109. }
  110. public static bool GetShowInlineCurves(TrackAsset track)
  111. {
  112. if (track == null)
  113. return false;
  114. return GetTrackViewModelData(track).showInlineCurves;
  115. }
  116. public static void SetShowInlineCurves(TrackAsset track, bool inlineOn)
  117. {
  118. if (track == null)
  119. return;
  120. GetTrackViewModelData(track).showInlineCurves = inlineOn;
  121. }
  122. public static float GetInlineCurveHeight(TrackAsset asset)
  123. {
  124. if (asset == null)
  125. return TrackViewModelData.DefaultinlineAnimationCurveHeight;
  126. return GetTrackViewModelData(asset).inlineAnimationCurveHeight;
  127. }
  128. public static void SetInlineCurveHeight(TrackAsset asset, float height)
  129. {
  130. if (asset != null)
  131. GetTrackViewModelData(asset).inlineAnimationCurveHeight = height;
  132. }
  133. public static int GetTrackHeightExtension(TrackAsset asset)
  134. {
  135. if (asset == null)
  136. return 0;
  137. return GetTrackViewModelData(asset).trackHeightExtension;
  138. }
  139. public static void SetTrackHeightExtension(TrackAsset asset, int height)
  140. {
  141. if (asset != null)
  142. GetTrackViewModelData(asset).trackHeightExtension = height;
  143. }
  144. }
  145. }