TimelinePreferences.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEditor.Timeline;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using UnityEngine.UIElements;
  7. /// <summary>
  8. /// Store the editor preferences for Timeline.
  9. /// </summary>
  10. [FilePath("TimelinePreferences.asset", FilePathAttribute.Location.PreferencesFolder)]
  11. public class TimelinePreferences : ScriptableSingleton<TimelinePreferences>
  12. {
  13. /// <summary>
  14. /// Define the time unit for the timeline window.
  15. /// true : frame unit.
  16. /// false : seconds unit.
  17. /// </summary>
  18. [SerializeField]
  19. public bool timeUnitInFrame = true;
  20. /// <summary>
  21. /// Draw the waveforms for all audio clips.
  22. /// </summary>
  23. [SerializeField]
  24. public bool showAudioWaveform = true;
  25. /// <summary>
  26. /// Allow the users to hear audio while scrubbing on audio clip.
  27. /// </summary>
  28. [SerializeField]
  29. bool m_AudioScrubbing;
  30. /// <summary>
  31. /// Enables audio scrubbing when moving the playhead.
  32. /// </summary>
  33. public bool audioScrubbing
  34. {
  35. get { return m_AudioScrubbing; }
  36. set
  37. {
  38. if (m_AudioScrubbing != value)
  39. {
  40. m_AudioScrubbing = value;
  41. TimelinePlayable.muteAudioScrubbing = !value;
  42. TimelineEditor.Refresh(RefreshReason.ContentsModified);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// Enable Snap to Frame to manipulate clips and align them on frames.
  48. /// </summary>
  49. [SerializeField]
  50. public bool snapToFrame = true;
  51. /// <summary>
  52. /// Enable the ability to snap clips on the edge of another clip.
  53. /// </summary>
  54. [SerializeField]
  55. public bool edgeSnap = true;
  56. /// <summary>
  57. /// Behavior of the timeline window during playback.
  58. /// </summary>
  59. [SerializeField]
  60. public PlaybackScrollMode playbackScrollMode = PlaybackScrollMode.None;
  61. void OnDisable()
  62. {
  63. Save();
  64. }
  65. /// <summary>
  66. /// Save the timeline preferences settings file.
  67. /// </summary>
  68. public void Save()
  69. {
  70. Save(true);
  71. }
  72. internal SerializedObject GetSerializedObject()
  73. {
  74. return new SerializedObject(this);
  75. }
  76. }
  77. class TimelinePreferencesProvider : SettingsProvider
  78. {
  79. SerializedObject m_SerializedObject;
  80. SerializedProperty m_ShowAudioWaveform;
  81. SerializedProperty m_TimeUnitInFrame;
  82. SerializedProperty m_SnapToFrame;
  83. SerializedProperty m_EdgeSnap;
  84. SerializedProperty m_PlaybackScrollMode;
  85. static string[] timeUnitsList = new string[]
  86. {
  87. "Seconds",
  88. "Frames"
  89. };
  90. private class Styles
  91. {
  92. public static readonly GUIContent TimeUnitLabel = EditorGUIUtility.TrTextContent("Time Unit", "Define the time unit for the timeline window (Frames or Seconds).");
  93. public static readonly GUIContent ShowAudioWaveformLabel = EditorGUIUtility.TrTextContent("Show Audio Waveforms", "Draw the waveforms for all audio clips.");
  94. public static readonly GUIContent AudioScrubbingLabel = EditorGUIUtility.TrTextContent("Allow Audio Scrubbing", "Allow the users to hear audio while scrubbing on audio clip.");
  95. public static readonly GUIContent SnapToFrameLabel = EditorGUIUtility.TrTextContent("Snap To Frame", "Enable Snap to Frame to manipulate clips and align them on frames.");
  96. public static readonly GUIContent EdgeSnapLabel = EditorGUIUtility.TrTextContent("Edge Snap", "Enable the ability to snap clips on the edge of another clip.");
  97. public static readonly GUIContent PlaybackScrollModeLabel = EditorGUIUtility.TrTextContent("Playback Scrolling Mode", "Define scrolling behavior during playback.");
  98. public static readonly GUIContent EditorSettingLabel = EditorGUIUtility.TrTextContent("Timeline Editor Settings", "");
  99. }
  100. public TimelinePreferencesProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null)
  101. : base(path, scopes, keywords)
  102. {
  103. }
  104. public override void OnActivate(string searchContext, VisualElement rootElement)
  105. {
  106. TimelinePreferences.instance.Save();
  107. m_SerializedObject = TimelinePreferences.instance.GetSerializedObject();
  108. m_ShowAudioWaveform = m_SerializedObject.FindProperty("showAudioWaveform");
  109. m_TimeUnitInFrame = m_SerializedObject.FindProperty("timeUnitInFrame");
  110. m_SnapToFrame = m_SerializedObject.FindProperty("snapToFrame");
  111. m_EdgeSnap = m_SerializedObject.FindProperty("edgeSnap");
  112. m_PlaybackScrollMode = m_SerializedObject.FindProperty("playbackScrollMode");
  113. }
  114. public override void OnGUI(string searchContext)
  115. {
  116. m_SerializedObject.Update();
  117. EditorGUI.BeginChangeCheck();
  118. using (new SettingsWindow.GUIScope())
  119. {
  120. EditorGUILayout.LabelField(Styles.EditorSettingLabel, EditorStyles.boldLabel);
  121. int timeUnitIdx = EditorGUILayout.Popup(Styles.TimeUnitLabel, m_TimeUnitInFrame.boolValue ? 1 : 0, timeUnitsList);
  122. m_TimeUnitInFrame.boolValue = timeUnitIdx == 1;
  123. m_PlaybackScrollMode.enumValueIndex = EditorGUILayout.Popup(Styles.PlaybackScrollModeLabel, m_PlaybackScrollMode.enumValueIndex, m_PlaybackScrollMode.enumNames);
  124. m_ShowAudioWaveform.boolValue = EditorGUILayout.Toggle(Styles.ShowAudioWaveformLabel, m_ShowAudioWaveform.boolValue);
  125. TimelinePreferences.instance.audioScrubbing = EditorGUILayout.Toggle(Styles.AudioScrubbingLabel, TimelinePreferences.instance.audioScrubbing);
  126. m_SnapToFrame.boolValue = EditorGUILayout.Toggle(Styles.SnapToFrameLabel, m_SnapToFrame.boolValue);
  127. m_EdgeSnap.boolValue = EditorGUILayout.Toggle(Styles.EdgeSnapLabel, m_EdgeSnap.boolValue);
  128. }
  129. if (EditorGUI.EndChangeCheck())
  130. {
  131. m_SerializedObject.ApplyModifiedProperties();
  132. TimelinePreferences.instance.Save();
  133. TimelineEditor.Refresh(RefreshReason.WindowNeedsRedraw);
  134. }
  135. }
  136. [SettingsProvider]
  137. public static SettingsProvider CreateTimelineProjectSettingProvider()
  138. {
  139. var provider = new TimelinePreferencesProvider("Preferences/Timeline", SettingsScope.User, GetSearchKeywordsFromGUIContentProperties<Styles>());
  140. return provider;
  141. }
  142. }