// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
using System;
using UnityEditor;
using Object = UnityEngine.Object;
namespace Animancer.Editor
{
/// [Editor-Only]
/// A wrapper for modifying a serialized field to allow it to be properly saved and undone.
///
///
/// Example:
///
/// using (new ModifySerializedField(target))// Undo gets captured.
/// {
/// // Modify values on the target.
/// }// Target gets flagged for saving.
///
/// https://kybernetik.com.au/animancer/api/Animancer.Editor/ModifySerializedField
public readonly struct ModifySerializedField : IDisposable
{
/************************************************************************************************************************/
/// The object being modified.
public readonly Object Target;
/// Prefab modifications are more complicated to track due to the possibility of nested prefabs.
public readonly bool MightBePrefab;
/************************************************************************************************************************/
/// Captures the state of the target as an undo step.
public ModifySerializedField(
Object target,
string name = "Inspector",
bool mightBePrefab = false)
{
Target = target;
MightBePrefab = mightBePrefab;
if (!string.IsNullOrEmpty(name))
Undo.RecordObject(Target, name);
}
/************************************************************************************************************************/
/// Flags the target as modified so that it will get saved by Unity.
public void Dispose()
{
if (MightBePrefab)
PrefabUtility.RecordPrefabInstancePropertyModifications(Target);
EditorUtility.SetDirty(Target);
AnimancerReflection.TryInvoke(Target, "OnValidate");
}
/************************************************************************************************************************/
}
}
#endif