RecorderClipEditor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Globalization;
  3. using UnityEditor.Presets;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using UnityObject = UnityEngine.Object;
  7. namespace UnityEditor.Recorder.Timeline
  8. {
  9. [CustomEditor(typeof(RecorderClip), true)]
  10. class RecorderClipEditor : Editor
  11. {
  12. RecorderEditor m_Editor;
  13. TimelineAsset m_Timeline;
  14. RecorderSelector m_RecorderSelector;
  15. public void OnEnable()
  16. {
  17. m_RecorderSelector = null;
  18. }
  19. public override void OnInspectorGUI()
  20. {
  21. try
  22. {
  23. if (target == null)
  24. return;
  25. // Bug? work arround: on Stop play, Enable is not called.
  26. if (m_Editor != null && m_Editor.target == null)
  27. {
  28. UnityHelpers.Destroy(m_Editor);
  29. m_Editor = null;
  30. m_RecorderSelector = null;
  31. }
  32. if (m_RecorderSelector == null)
  33. {
  34. m_RecorderSelector = new RecorderSelector();
  35. m_RecorderSelector.OnSelectionChanged += OnRecorderSelected;
  36. m_RecorderSelector.Init(((RecorderClip) target).settings);
  37. }
  38. using (new EditorGUI.DisabledScope(EditorApplication.isPlaying))
  39. {
  40. var clip = (RecorderClip) target;
  41. if (m_Timeline == null)
  42. m_Timeline = clip.FindTimelineAsset();
  43. if (m_Timeline != null)
  44. {
  45. EditorGUILayout.LabelField("Frame Rate");
  46. EditorGUI.indentLevel++;
  47. EditorGUILayout.LabelField("Playback", FrameRatePlayback.Constant.ToString());
  48. EditorGUILayout.LabelField("Target (Timeline FPS)", m_Timeline.editorSettings.fps.ToString(CultureInfo.InvariantCulture));
  49. EditorGUI.indentLevel--;
  50. EditorGUILayout.Separator();
  51. }
  52. EditorGUILayout.BeginHorizontal();
  53. if (clip.needsDuplication)
  54. {
  55. if (clip.settings != null)
  56. {
  57. clip.settings = Instantiate(clip.settings);
  58. AssetDatabase.AddObjectToAsset(clip.settings, clip);
  59. }
  60. clip.needsDuplication = false;
  61. }
  62. m_RecorderSelector.OnGui();
  63. if (m_Editor != null)
  64. {
  65. if (GUILayout.Button(PresetHelper.presetIcon, PresetHelper.presetButtonStyle))
  66. {
  67. var settings = m_Editor.target as RecorderSettings;
  68. if (settings != null)
  69. {
  70. var presetReceiver = CreateInstance<PresetHelper.PresetReceiver>();
  71. presetReceiver.Init(settings, Repaint);
  72. PresetSelector.ShowSelector(settings, null, true, presetReceiver);
  73. }
  74. }
  75. }
  76. EditorGUILayout.EndHorizontal();
  77. if (m_Editor != null)
  78. {
  79. EditorGUILayout.Separator();
  80. m_Editor.OnInspectorGUI();
  81. serializedObject.Update();
  82. }
  83. }
  84. }
  85. catch (ExitGUIException)
  86. {
  87. }
  88. catch (Exception ex)
  89. {
  90. EditorGUILayout.HelpBox("An exception was raised while editing the settings. This can be indicative of corrupted settings.", MessageType.Warning);
  91. if (GUILayout.Button("Reset settings to default"))
  92. ResetSettings();
  93. Debug.LogException(ex);
  94. }
  95. }
  96. void ResetSettings()
  97. {
  98. UnityHelpers.Destroy(m_Editor);
  99. m_Editor = null;
  100. m_RecorderSelector = null;
  101. UnityHelpers.Destroy(((RecorderClip) target).settings, true);
  102. }
  103. void OnRecorderSelected(Type selectedRecorder)
  104. {
  105. var clip = (RecorderClip)target;
  106. if (m_Editor != null)
  107. {
  108. UnityHelpers.Destroy(m_Editor);
  109. m_Editor = null;
  110. }
  111. if (selectedRecorder == null)
  112. return;
  113. if (clip.settings != null && RecordersInventory.GetRecorderInfo(selectedRecorder).settingsType != clip.settings.GetType())
  114. {
  115. UnityHelpers.Destroy(clip.settings, true);
  116. clip.settings = null;
  117. }
  118. if (clip.settings == null)
  119. {
  120. clip.settings = RecordersInventory.CreateDefaultRecorderSettings(selectedRecorder);
  121. AssetDatabase.AddObjectToAsset(clip.settings, clip);
  122. }
  123. m_Editor = (RecorderEditor) CreateEditor(clip.settings);
  124. AssetDatabase.Refresh();
  125. }
  126. }
  127. }