RecorderSelector.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.Recorder.Timeline
  6. {
  7. class RecorderSelector
  8. {
  9. string[] m_RecorderNames;
  10. List<Type> m_RecorderTypes;
  11. bool m_SettingsAreAssets;
  12. Type m_SelectedRecorder;
  13. public event Action<Type> OnSelectionChanged;
  14. public void Init(RecorderSettings settings)
  15. {
  16. if (m_RecorderTypes == null)
  17. {
  18. var recorderList = RecordersInventory.builtInRecorderInfos.ToList();
  19. if (RecorderOptions.ShowLegacyRecorders)
  20. recorderList.AddRange(RecordersInventory.legacyRecorderInfos);
  21. recorderList.AddRange(RecordersInventory.customRecorderInfos);
  22. m_RecorderTypes = recorderList.Select(x => x.settingsType).ToList();
  23. m_RecorderNames = recorderList.Select(x => x.displayName).ToArray();
  24. }
  25. SelectRecorder(settings != null ? settings.GetType() : m_RecorderTypes.First());
  26. }
  27. int GetRecorderIndex(Type settingType)
  28. {
  29. return m_RecorderTypes.IndexOf(settingType);
  30. }
  31. Type GetRecorderFromIndex(int index)
  32. {
  33. return m_RecorderTypes.ElementAt(index);
  34. }
  35. public void OnGui()
  36. {
  37. // Recorder in group selection
  38. EditorGUILayout.BeginHorizontal();
  39. var oldIndex = GetRecorderIndex(m_SelectedRecorder);
  40. var newIndex = EditorGUILayout.Popup("Selected recorder:", oldIndex, m_RecorderNames);
  41. SelectRecorder(GetRecorderFromIndex(newIndex));
  42. EditorGUILayout.EndHorizontal();
  43. }
  44. void SelectRecorder(Type newSelection)
  45. {
  46. if (m_SelectedRecorder == newSelection)
  47. return;
  48. var recorderAttribs = newSelection.GetCustomAttributes(typeof(ObsoleteAttribute), false);
  49. if (recorderAttribs.Length > 0 )
  50. Debug.LogWarning( "Recorder " + ((ObsoleteAttribute)recorderAttribs[0]).Message);
  51. m_SelectedRecorder = newSelection;
  52. if (OnSelectionChanged != null)
  53. OnSelectionChanged.Invoke(m_SelectedRecorder);
  54. }
  55. }
  56. }