#if UNITY_EDITOR using System.Linq; using MonKey.Extensions; using UnityEditor; using UnityEngine; using Object = UnityEngine.Object; namespace MonKey { public abstract class AbstractUndo where T : Object { /// /// Registers objects /// before a long series of operation, /// so that the undo can be registered /// until the state it was in before the operations /// /// public abstract void Register(params T[] valuesToRegister); /// /// Creates an undo when all the operations /// are done to make sure all the undo is properly collapse /// You must create an Undo id /// and collapse it after calling this method though. /// public abstract void RecordUndo(); } /// /// Registers transforms before a series of movement /// public class TransformUndo : AbstractUndo { private Vector3[] positions; private Quaternion[] rotations; private Vector3[] localScales; private Transform[] currentValues; /// /// /// /// public override void Register(params Transform[] valuesToRegister) { positions = valuesToRegister.Convert(_ => _.position).ToArray(); rotations = valuesToRegister.Convert(_ => _.rotation).ToArray(); localScales = valuesToRegister.Convert(_ => _.localScale).ToArray(); currentValues = valuesToRegister; } /// /// /// public override void RecordUndo() { Vector3[] newPositions = currentValues.Convert(_ => _.position).ToArray(); Quaternion[] newRotations = currentValues.Convert(_ => _.rotation).ToArray(); Vector3[] newLocalScales = currentValues.Convert(_ => _.localScale).ToArray(); for (int i = 0; i < currentValues.Length; i++) { Transform currentValue = currentValues[i]; currentValue.position = positions[i]; currentValue.rotation = rotations[i]; currentValue.localScale = localScales[i]; } for (int i = 0; i < currentValues.Length; i++) { Transform currentValue = currentValues[i]; Undo.RecordObject(currentValue,"transform"); currentValue.position = newPositions[i]; currentValue.rotation = newRotations[i]; currentValue.localScale = newLocalScales[i]; } } } } #endif