using System;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif
using UnityObject = UnityEngine.Object;
namespace UnityEngine.Recorder
{
///
/// Helper component mainly used to save Recorder's GameObject references.
/// Some recorders (such as the Animation Recorder) require a GameObject reference from a Scene to record from.
/// In such a case, this component is automatically added to the Scene and binds the selected GameObject to the Recorder Settings.
///
[ExecuteInEditMode]
public class RecorderBindings : MonoBehaviour
{
[Serializable]
class PropertyObjects : SerializedDictionary { }
[SerializeField] PropertyObjects m_References = new PropertyObjects();
///
/// Binds a particular ID to an Object value.
///
/// The unique ID key.
/// The value for the key.
public void SetBindingValue(string id, UnityObject value)
{
#if UNITY_EDITOR
var dirty = !m_References.dictionary.ContainsKey(id) || m_References.dictionary[id] != value;
#endif
m_References.dictionary[id] = value;
#if UNITY_EDITOR
if (dirty)
MarkSceneDirty();
#endif
}
///
/// Retrieves a binding for an unique ID.
///
/// The binding key.
/// The value corresponding to the key – or null if the key doesn't exist.
public UnityObject GetBindingValue(string id)
{
UnityObject value;
return m_References.dictionary.TryGetValue(id, out value) ? value : null;
}
///
/// Tests if a value exists for a given key.
///
/// The key to test.
/// True if the key value exists, False otherwise.
public bool HasBindingValue(string id)
{
return m_References.dictionary.ContainsKey(id);
}
///
/// Removes the binding for a key.
///
/// The unique key of the binding to remove.
public void RemoveBinding(string id)
{
if (m_References.dictionary.ContainsKey(id))
{
m_References.dictionary.Remove(id);
MarkSceneDirty();
}
}
///
/// Tests if any bindings exist.
///
/// True if there are any existing bindings, False otherwise.
public bool IsEmpty()
{
return m_References == null || !m_References.dictionary.Keys.Any();
}
///
/// Duplicates the binding from an existing key to a new one (makes dst point to the same object as src).
///
/// The key to duplicate the binding from.
/// The new key that points to the same object.
public void DuplicateBinding(string src, string dst)
{
if (m_References.dictionary.ContainsKey(src))
{
m_References.dictionary[dst] = m_References.dictionary[src];
MarkSceneDirty();
}
}
void MarkSceneDirty()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
EditorSceneManager.MarkSceneDirty(gameObject.scene);
#endif
}
}
}