- Home /
Swap Mecanim Layers in Unity 4.2
Unity currently does not support the ability to swap mecanim layers, but before version 4.2 there was a nice editor window script for doing it found here. In Unity 4.2 they changed the animator controller class, so that this script no longer works, and they still haven't added a swap feature.
Does anyone have a fix for this script? I would do it myself, but I cannot figure out what the equivalent of SetLayerType() is in the 4.2 and the state machine appears to be read only now.
Here's the script:
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class AnimatorSwapLayers : EditorWindow
{
private Vector2 _scrollPosition;
[MenuItem("Window/Animator Swap Layers")]
static void Init ()
{
AnimatorSwapLayers window = (AnimatorSwapLayers)GetWindow(typeof(AnimatorSwapLayers));
}
void OnSelectionChange()
{
Repaint();
}
void OnGUI()
{
if(Selection.objects.Length > 0 && Selection.objects[0] is AnimatorController)
{
AnimatorController controller = (AnimatorController)Selection.objects[0];
_scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
for (int i = 0; i < controller.GetLayerCount(); i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(controller.GetLayerName(i));
GUILayout.FlexibleSpace();
if(i>0)
{
if (GUILayout.Button("↑"))
{
Swap(controller, i, true);
}
}
if (i < controller.GetLayerCount()-1)
{
if (GUILayout.Button("↓"))
{
Swap(controller, i, false);
}
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
}
GUILayout.EndScrollView();
}
else if (Selection.objects.Length > 1)
{
GUILayout.Label("Multy edit not supported");
}
else
{
GUILayout.Label("No controller selected");
}
}
void Swap(AnimatorController controller, int pos, bool moveUp)
{
int newPos = pos + (moveUp ? -1 : 1);
AnimatorLayerBlendingMode blendingModeT = controller.GetLayerBlendingMode(newPos);
bool iKPassT = controller.GetLayerIKPass(newPos);
AvatarMask layerMaskT = controller.GetLayerMask(newPos);
string nameT = controller.GetLayerName(newPos);
StateMachine stateMachineT = controller.GetLayerStateMachine(newPos);
int syncedIndexT = controller.GetLayerSyncedIndex(newPos);
int layerTypeT = controller.GetLayerType(newPos);
controller.SetLayerBlendingMode(newPos, controller.GetLayerBlendingMode(pos));
controller.SetLayerIKPass(newPos, controller.GetLayerIKPass(pos));
controller.SetLayerMask(newPos, controller.GetLayerMask(pos));
controller.SetLayerName(newPos, controller.GetLayerName(pos));
controller.SetLayerStateMachine(newPos, controller.GetLayerStateMachine(pos));
controller.SetLayerSyncedIndex(newPos, controller.GetLayerSyncedIndex(pos));
controller.SetLayerType(newPos, controller.GetLayerType(pos));
controller.SetLayerBlendingMode(pos, blendingModeT);
controller.SetLayerIKPass(pos, iKPassT);
controller.SetLayerMask(pos, layerMaskT);
controller.SetLayerName(pos, nameT);
controller.SetLayerStateMachine(pos, stateMachineT);
controller.SetLayerSyncedIndex(pos, syncedIndexT);
controller.SetLayerType(pos, layerTypeT);
}
}
Comment