RecorderBindings.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Linq;
  3. #if UNITY_EDITOR
  4. using UnityEditor.SceneManagement;
  5. #endif
  6. using UnityObject = UnityEngine.Object;
  7. namespace UnityEngine.Recorder
  8. {
  9. /// <summary>
  10. /// Helper component mainly used to save Recorder's GameObject references.
  11. /// Some recorders (such as the Animation Recorder) require a GameObject reference from a Scene to record from.
  12. /// In such a case, this component is automatically added to the Scene and binds the selected GameObject to the Recorder Settings.
  13. /// </summary>
  14. [ExecuteInEditMode]
  15. public class RecorderBindings : MonoBehaviour
  16. {
  17. [Serializable]
  18. class PropertyObjects : SerializedDictionary<string, UnityObject> { }
  19. [SerializeField] PropertyObjects m_References = new PropertyObjects();
  20. /// <summary>
  21. /// Binds a particular ID to an Object value.
  22. /// </summary>
  23. /// <param name="id">The unique ID key.</param>
  24. /// <param name="value">The value for the key.</param>
  25. public void SetBindingValue(string id, UnityObject value)
  26. {
  27. #if UNITY_EDITOR
  28. var dirty = !m_References.dictionary.ContainsKey(id) || m_References.dictionary[id] != value;
  29. #endif
  30. m_References.dictionary[id] = value;
  31. #if UNITY_EDITOR
  32. if (dirty)
  33. MarkSceneDirty();
  34. #endif
  35. }
  36. /// <summary>
  37. /// Retrieves a binding for an unique ID.
  38. /// </summary>
  39. /// <param name="id">The binding key.</param>
  40. /// <returns>The value corresponding to the key – or null if the key doesn't exist.</returns>
  41. public UnityObject GetBindingValue(string id)
  42. {
  43. UnityObject value;
  44. return m_References.dictionary.TryGetValue(id, out value) ? value : null;
  45. }
  46. /// <summary>
  47. /// Tests if a value exists for a given key.
  48. /// </summary>
  49. /// <param name="id">The key to test.</param>
  50. /// <returns>True if the key value exists, False otherwise.</returns>
  51. public bool HasBindingValue(string id)
  52. {
  53. return m_References.dictionary.ContainsKey(id);
  54. }
  55. /// <summary>
  56. /// Removes the binding for a key.
  57. /// </summary>
  58. /// <param name="id">The unique key of the binding to remove.</param>
  59. public void RemoveBinding(string id)
  60. {
  61. if (m_References.dictionary.ContainsKey(id))
  62. {
  63. m_References.dictionary.Remove(id);
  64. MarkSceneDirty();
  65. }
  66. }
  67. /// <summary>
  68. /// Tests if any bindings exist.
  69. /// </summary>
  70. /// <returns>True if there are any existing bindings, False otherwise.</returns>
  71. public bool IsEmpty()
  72. {
  73. return m_References == null || !m_References.dictionary.Keys.Any();
  74. }
  75. /// <summary>
  76. /// Duplicates the binding from an existing key to a new one (makes dst point to the same object as src).
  77. /// </summary>
  78. /// <param name="src">The key to duplicate the binding from.</param>
  79. /// <param name="dst">The new key that points to the same object.</param>
  80. public void DuplicateBinding(string src, string dst)
  81. {
  82. if (m_References.dictionary.ContainsKey(src))
  83. {
  84. m_References.dictionary[dst] = m_References.dictionary[src];
  85. MarkSceneDirty();
  86. }
  87. }
  88. void MarkSceneDirty()
  89. {
  90. #if UNITY_EDITOR
  91. if (!Application.isPlaying)
  92. EditorSceneManager.MarkSceneDirty(gameObject.scene);
  93. #endif
  94. }
  95. }
  96. }