Last active
September 11, 2018 22:20
-
-
Save GhatSmith/b0cd86bb49a8ed74236b7ce0c5bbb370 to your computer and use it in GitHub Desktop.
CopyPasteStateMachineBehaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Animations; | |
namespace OddTales.TLN.Animations | |
{ | |
/// <summary> Add Copy/Paste context menu item for StateMachineBehaviour </summary> | |
public static class CopyPasteStateMachineBehaviour | |
{ | |
private static StateMachineBehaviour copiedBehaviour = null; | |
// Priority 1001 : to set after Move Down menu item | |
[MenuItem("CONTEXT/StateMachineBehaviour/Copy Behaviour", false, 1001)] | |
static void CopyBehaviour(MenuCommand command) | |
{ | |
copiedBehaviour = command.context as StateMachineBehaviour; | |
} | |
[MenuItem("CONTEXT/StateMachineBehaviour/Paste Behaviour Values", false, 1002)] | |
static void PasteBehaviourValues(MenuCommand command) | |
{ | |
EditorUtility.CopySerializedIfDifferent(copiedBehaviour, command.context); | |
} | |
/// <summary> Can only paste values into a behaviour of the same type </summary> | |
[MenuItem("CONTEXT/StateMachineBehaviour/Paste Behaviour Values", true)] | |
static bool ValidatePasteBehaviourValues(MenuCommand command) | |
{ | |
return copiedBehaviour != null && command.context.GetType() == copiedBehaviour.GetType(); | |
} | |
[MenuItem("CONTEXT/AnimatorState/Paste Behaviour As New", false, 1002)] | |
static void PasteBehaviourAsNewOnState(MenuCommand command) | |
{ | |
StateMachineBehaviour addedBehaviour = ((AnimatorState)command.context).AddStateMachineBehaviour(copiedBehaviour.GetType()); | |
EditorUtility.CopySerializedIfDifferent(copiedBehaviour, addedBehaviour); | |
} | |
[MenuItem("CONTEXT/AnimatorState/Paste Behaviour As New", true)] | |
static bool ValidatePasteBehaviourAsNewOnState(MenuCommand command) | |
{ | |
return copiedBehaviour != null; | |
} | |
[MenuItem("CONTEXT/StateMachineBehaviour/Paste Behaviour As New", false, 1003)] | |
static void PasteBehaviourAsNew(MenuCommand command) | |
{ | |
AnimatorState animatorState = GetAnimatorState(command.context as StateMachineBehaviour); | |
StateMachineBehaviour addedBehaviour = animatorState.AddStateMachineBehaviour(copiedBehaviour.GetType()); | |
EditorUtility.CopySerializedIfDifferent(copiedBehaviour, addedBehaviour); | |
} | |
[MenuItem("CONTEXT/StateMachineBehaviour/Paste Behaviour As New", true)] | |
static bool ValidatePasteBehaviourAsNew(MenuCommand command) | |
{ | |
return copiedBehaviour != null; | |
} | |
private static AnimatorState GetAnimatorState(StateMachineBehaviour stateMachineBehaviour) | |
{ | |
StateMachineBehaviourContext[] context = AnimatorController.FindStateMachineBehaviourContext(stateMachineBehaviour); | |
return (context != null && context.Length > 0) ? context[0].animatorObject as AnimatorState : null; // animatorObject can be an AnimatorState or AnimatorStateMachine | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment