InputSettingsSelector.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace UnityEditor.Recorder
  7. {
  8. /// <summary>
  9. /// Use this class to specify a particular input type (it allows for each recorder to support only a subset of the input settings).
  10. /// </summary>
  11. [Serializable]
  12. public abstract class InputSettingsSelector
  13. {
  14. [SerializeField] string m_Selected;
  15. readonly Dictionary<string, RecorderInputSettings> m_RecorderInputSettings = new Dictionary<string, RecorderInputSettings>();
  16. /// <summary>
  17. /// The currently selected RecorderInputSettings.
  18. /// </summary>
  19. public RecorderInputSettings Selected
  20. {
  21. get
  22. {
  23. if (string.IsNullOrEmpty(m_Selected) || !m_RecorderInputSettings.ContainsKey(m_Selected))
  24. m_Selected = m_RecorderInputSettings.Keys.First();
  25. return m_RecorderInputSettings[m_Selected];
  26. }
  27. protected set
  28. {
  29. foreach (var field in InputSettingFields())
  30. {
  31. var input = (RecorderInputSettings)field.GetValue(this);
  32. if (input.GetType() == value.GetType())
  33. {
  34. field.SetValue(this, value);
  35. m_Selected = field.Name;
  36. m_RecorderInputSettings[m_Selected] = value;
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. internal IEnumerable<FieldInfo> InputSettingFields()
  43. {
  44. return GetInputFields(GetType()).Where(f => typeof(RecorderInputSettings).IsAssignableFrom(f.FieldType));
  45. }
  46. internal static IEnumerable<FieldInfo> GetInputFields(Type type)
  47. {
  48. return type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  49. }
  50. /// <summary>
  51. /// The default constructor.
  52. /// </summary>
  53. protected internal InputSettingsSelector()
  54. {
  55. foreach (var field in InputSettingFields())
  56. {
  57. var input = (RecorderInputSettings)field.GetValue(this);
  58. m_RecorderInputSettings.Add(field.Name, input);
  59. }
  60. }
  61. }
  62. }