public static void Apply(this AnimatorState dest, AnimatorState source) { dest.cycleOffsetParameter = source.cycleOffsetParameter; dest.cycleOffset = source.cycleOffset; dest.cycleOffsetParameterActive = source.cycleOffsetParameterActive; dest.iKOnFeet = source.iKOnFeet; dest.mirror = source.mirror; dest.mirrorParameter = source.mirrorParameter; dest.mirrorParameterActive = source.mirrorParameterActive; dest.motion = source.motion; // keep reference dest.speed = source.speed; dest.speedParameter = source.speedParameter; dest.speedParameterActive = source.speedParameterActive; dest.tag = source.tag; dest.timeParameter = source.timeParameter; dest.timeParameterActive = source.timeParameterActive; dest.writeDefaultValues = source.writeDefaultValues; dest.name = source.name; dest.hideFlags = source.hideFlags; foreach (var sourceTransition in source.transitions.Where(w => w.isExit)) { var transition = dest.AddExitTransition(false); sourceTransition.CloneTo(transition); // should always false if (InstanceCaches <AnimatorStateTransition> .Find(sourceTransition.GetInstanceID()) == null) { InstanceCaches <AnimatorStateTransition> .Register(sourceTransition.GetInstanceID(), transition); } } foreach (var sourceTransition in source.transitions.Where(w => !w.isExit)) { AnimatorStateTransition transition = null; if (sourceTransition.destinationStateMachine != null) { transition = dest.AddTransition(InstanceCaches <AnimatorStateMachine> .FindOrCreate(sourceTransition.destinationStateMachine, w => w.CloneDeep())); } if (sourceTransition.destinationState != null) { transition = dest.AddTransition(InstanceCaches <AnimatorState> .FindOrCreate(sourceTransition.destinationState, CloneDeep)); } if (transition == null) { throw new ArgumentNullException(nameof(transition)); } sourceTransition.CloneTo(transition); // should always false if (InstanceCaches <AnimatorStateTransition> .Find(sourceTransition.GetInstanceID()) == null) { InstanceCaches <AnimatorStateTransition> .Register(sourceTransition.GetInstanceID(), transition); } } foreach (var sourceBehaviour in source.behaviours) { var behaviour = dest.AddStateMachineBehaviour(sourceBehaviour.GetType()); sourceBehaviour.CloneDeepTo(behaviour); // store InstanceCaches <StateMachineBehaviour> .Register(behaviour.GetInstanceID(), behaviour); } }
public static AnimatorStateMachine CloneDeep(this AnimatorStateMachine source) { var dest = new AnimatorStateMachine { defaultState = InstanceCaches <AnimatorState> .FindOrCreate(source.defaultState, w => w.CloneDeep()), anyStatePosition = source.anyStatePosition, entryPosition = source.entryPosition, exitPosition = source.exitPosition, parentStateMachinePosition = source.parentStateMachinePosition, hideFlags = source.hideFlags, name = source.name }; foreach (var sourceState in source.states) { dest.AddState(InstanceCaches <AnimatorState> .FindOrCreate(sourceState.state, w => w.CloneDeep()), sourceState.position); } foreach (var sourceTransition in source.anyStateTransitions) { AnimatorStateTransition transition = null; if (sourceTransition.destinationStateMachine != null) { transition = dest.AddAnyStateTransition(InstanceCaches <AnimatorStateMachine> .FindOrCreate(sourceTransition.destinationStateMachine, CloneDeep)); } if (sourceTransition.destinationState != null) { transition = dest.AddAnyStateTransition(InstanceCaches <AnimatorState> .FindOrCreate(sourceTransition.destinationState, w => w.CloneDeep())); } if (transition == null) { throw new ArgumentNullException(nameof(transition)); } sourceTransition.CloneTo(transition); // should always false if (InstanceCaches <AnimatorStateTransition> .Find(sourceTransition.GetInstanceID()) == null) { InstanceCaches <AnimatorStateTransition> .Register(sourceTransition.GetInstanceID(), transition); } } foreach (var sourceTransition in source.entryTransitions) { AnimatorTransition transition = null; if (sourceTransition.destinationStateMachine != null) { transition = dest.AddEntryTransition(InstanceCaches <AnimatorStateMachine> .FindOrCreate(sourceTransition.destinationStateMachine, CloneDeep)); } if (sourceTransition.destinationState != null) { transition = dest.AddEntryTransition(InstanceCaches <AnimatorState> .FindOrCreate(sourceTransition.destinationState, w => w.CloneDeep())); } if (transition == null) { throw new ArgumentNullException(nameof(transition)); } transition.CloneTo(sourceTransition); // should always false if (InstanceCaches <AnimatorTransition> .Find(sourceTransition.GetInstanceID()) == null) { InstanceCaches <AnimatorTransition> .Register(sourceTransition.GetInstanceID(), transition); } } foreach (var sourceBehaviour in source.behaviours) { var behaviour = dest.AddStateMachineBehaviour(sourceBehaviour.GetType()); sourceBehaviour.CloneDeepTo(behaviour); // store InstanceCaches <StateMachineBehaviour> .Register(behaviour.GetInstanceID(), behaviour); } foreach (var sourceStateMachine in source.stateMachines) { dest.AddStateMachine(InstanceCaches <AnimatorStateMachine> .FindOrCreate(sourceStateMachine.stateMachine, CloneDeep), sourceStateMachine.position); } return(dest); }