暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TimelinePreferences.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditor.Timeline;
  5. using UnityEngine;
  6. using UnityEngine.Timeline;
  7. using UnityEngine.UIElements;
  8. #if !UNITY_2020_2_OR_NEWER
  9. using L10n = UnityEditor.Timeline.L10n;
  10. #endif
  11. /// <summary>
  12. /// Store the editor preferences for Timeline.
  13. /// </summary>
  14. [FilePath("TimelinePreferences.asset", FilePathAttribute.Location.PreferencesFolder)]
  15. public class TimelinePreferences : ScriptableSingleton<TimelinePreferences>
  16. {
  17. /// <summary>
  18. /// The time unit used by the Timeline Editor when displaying time values.
  19. /// </summary>
  20. [SerializeField]
  21. public TimeFormat timeFormat;
  22. /// <summary>
  23. /// Define the time unit for the timeline window.
  24. /// true : frame unit.
  25. /// false : timecode unit.
  26. /// </summary>
  27. [NonSerialized, Obsolete("timeUnitInFrame is deprecated. Use timeFormat instead", false)]
  28. public bool timeUnitInFrame;
  29. /// <summary>
  30. /// Draw the waveforms for all audio clips.
  31. /// </summary>
  32. [SerializeField]
  33. public bool showAudioWaveform = true;
  34. /// <summary>
  35. /// Allow the users to hear audio while scrubbing on audio clip.
  36. /// </summary>
  37. [SerializeField]
  38. bool m_AudioScrubbing;
  39. /// <summary>
  40. /// Enables audio scrubbing when moving the playhead.
  41. /// </summary>
  42. public bool audioScrubbing
  43. {
  44. get { return m_AudioScrubbing; }
  45. set
  46. {
  47. if (m_AudioScrubbing != value)
  48. {
  49. m_AudioScrubbing = value;
  50. TimelinePlayable.muteAudioScrubbing = !value;
  51. TimelineEditor.Refresh(RefreshReason.ContentsModified);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Enable Snap to Frame to manipulate clips and align them on frames.
  57. /// </summary>
  58. [SerializeField]
  59. public bool snapToFrame = true;
  60. #if TIMELINE_FRAMEACCURATE
  61. [SerializeField] bool m_PlaybackLockedToFrame;
  62. #endif
  63. /// <summary>
  64. /// Enable Timelines to be evaluated on frame during editor preview.
  65. /// </summary>
  66. public bool playbackLockedToFrame
  67. {
  68. get
  69. {
  70. #if TIMELINE_FRAMEACCURATE
  71. return m_PlaybackLockedToFrame;
  72. #else
  73. Debug.LogWarning($"PlaybackLockedToFrame is not available for this Unity version");
  74. return false;
  75. #endif
  76. }
  77. set
  78. {
  79. #if TIMELINE_FRAMEACCURATE
  80. m_PlaybackLockedToFrame = value;
  81. TimelineEditor.RefreshPreviewPlay();
  82. #else
  83. Debug.LogWarning($"PlaybackLockedToFrame is not available for this Unity version");
  84. #endif
  85. }
  86. }
  87. /// <summary>
  88. /// Enable the ability to snap clips on the edge of another clip.
  89. /// </summary>
  90. [SerializeField]
  91. public bool edgeSnap = true;
  92. /// <summary>
  93. /// Behavior of the timeline window during playback.
  94. /// </summary>
  95. [SerializeField]
  96. public PlaybackScrollMode playbackScrollMode;
  97. void OnDisable()
  98. {
  99. Save();
  100. }
  101. /// <summary>
  102. /// Save the timeline preferences settings file.
  103. /// </summary>
  104. public void Save()
  105. {
  106. Save(true);
  107. }
  108. internal SerializedObject GetSerializedObject()
  109. {
  110. return new SerializedObject(this);
  111. }
  112. }
  113. class TimelinePreferencesProvider : SettingsProvider
  114. {
  115. SerializedObject m_SerializedObject;
  116. SerializedProperty m_ShowAudioWaveform;
  117. SerializedProperty m_TimeFormat;
  118. SerializedProperty m_SnapToFrame;
  119. SerializedProperty m_EdgeSnap;
  120. SerializedProperty m_PlaybackScrollMode;
  121. SerializedProperty m_PlaybackLockedToFrame;
  122. internal class Styles
  123. {
  124. public static readonly GUIContent TimeUnitLabel = L10n.TextContent("Time Unit", "Define the time unit for the timeline window (Frames, Timecode or Seconds).");
  125. public static readonly GUIContent ShowAudioWaveformLabel = L10n.TextContent("Show Audio Waveforms", "Draw the waveforms for all audio clips.");
  126. public static readonly GUIContent AudioScrubbingLabel = L10n.TextContent("Allow Audio Scrubbing", "Allow the users to hear audio while scrubbing on audio clip.");
  127. public static readonly GUIContent SnapToFrameLabel = L10n.TextContent("Snap To Frame", "Enable Snap to Frame to manipulate clips and align them on frames.");
  128. public static readonly GUIContent EdgeSnapLabel = L10n.TextContent("Edge Snap", "Enable the ability to snap clips on the edge of another clip.");
  129. public static readonly GUIContent PlaybackScrollModeLabel = L10n.TextContent("Playback Scrolling Mode", "Define scrolling behavior during playback.");
  130. public static readonly GUIContent EditorSettingLabel = L10n.TextContent("Timeline Editor Settings", "");
  131. #if TIMELINE_FRAMEACCURATE
  132. public static readonly GUIContent PlaybackLockedToFrame = L10n.TextContent("Playback Locked To Frame", "Enable Frame Accurate Preview.");
  133. #endif
  134. }
  135. public TimelinePreferencesProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null)
  136. : base(path, scopes, keywords)
  137. {
  138. }
  139. public override void OnActivate(string searchContext, VisualElement rootElement)
  140. {
  141. TimelinePreferences.instance.Save();
  142. m_SerializedObject = TimelinePreferences.instance.GetSerializedObject();
  143. m_ShowAudioWaveform = m_SerializedObject.FindProperty("showAudioWaveform");
  144. m_TimeFormat = m_SerializedObject.FindProperty("timeFormat");
  145. m_SnapToFrame = m_SerializedObject.FindProperty("snapToFrame");
  146. m_EdgeSnap = m_SerializedObject.FindProperty("edgeSnap");
  147. m_PlaybackScrollMode = m_SerializedObject.FindProperty("playbackScrollMode");
  148. #if TIMELINE_FRAMEACCURATE
  149. m_PlaybackLockedToFrame = m_SerializedObject.FindProperty("m_PlaybackLockedToFrame");
  150. #endif
  151. }
  152. public override void OnGUI(string searchContext)
  153. {
  154. m_SerializedObject.Update();
  155. EditorGUI.BeginChangeCheck();
  156. using (new SettingsWindow.GUIScope())
  157. {
  158. EditorGUILayout.LabelField(Styles.EditorSettingLabel, EditorStyles.boldLabel);
  159. m_TimeFormat.enumValueIndex = EditorGUILayout.Popup(Styles.TimeUnitLabel, m_TimeFormat.enumValueIndex, m_TimeFormat.enumDisplayNames);
  160. m_PlaybackScrollMode.enumValueIndex = EditorGUILayout.Popup(Styles.PlaybackScrollModeLabel, m_PlaybackScrollMode.enumValueIndex, m_PlaybackScrollMode.enumNames);
  161. m_ShowAudioWaveform.boolValue = EditorGUILayout.Toggle(Styles.ShowAudioWaveformLabel, m_ShowAudioWaveform.boolValue);
  162. TimelinePreferences.instance.audioScrubbing = EditorGUILayout.Toggle(Styles.AudioScrubbingLabel, TimelinePreferences.instance.audioScrubbing);
  163. m_SnapToFrame.boolValue = EditorGUILayout.Toggle(Styles.SnapToFrameLabel, m_SnapToFrame.boolValue);
  164. m_EdgeSnap.boolValue = EditorGUILayout.Toggle(Styles.EdgeSnapLabel, m_EdgeSnap.boolValue);
  165. #if TIMELINE_FRAMEACCURATE
  166. m_PlaybackLockedToFrame.boolValue = EditorGUILayout.Toggle(Styles.PlaybackLockedToFrame, m_PlaybackLockedToFrame.boolValue);
  167. #endif
  168. }
  169. if (EditorGUI.EndChangeCheck())
  170. {
  171. m_SerializedObject.ApplyModifiedProperties();
  172. TimelinePreferences.instance.Save();
  173. TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
  174. TimelineEditor.RefreshPreviewPlay();
  175. }
  176. }
  177. [SettingsProvider]
  178. public static SettingsProvider CreateTimelineProjectSettingProvider()
  179. {
  180. var provider = new TimelinePreferencesProvider("Preferences/Timeline", SettingsScope.User, GetSearchKeywordsFromGUIContentProperties<Styles>());
  181. return provider;
  182. }
  183. }