Hi, in my 2D game I have an animator controller, and an override controller, the latter being assigned to my character like so:
[AnimatorController (3 layers) ]
[AnimatorOverrideController] ( assigned to character )
- The animator controller has 3 layers.
- I intend to append animations via the ‘sync’ functionality of mechanim.
- I aim to swap the animations in these 3 layers at runtime.
I can use the AnimationOverrideController to override the ‘Base Layer’ animations like this:
//( grab reference to component )
Animator animRef= GetComponent<Animator>();
//( replace with a shiny new one so we dont adjust the prefab )
AnimatorOverrideController newOne= new AnimatorOverrideController();
newOne.runtimeAnimatorController= animRef.runtimeAnimatorController;
animRef.runtimeAnimatorController= newOne;
//( Replace the animation for 'idle' )
((AnimatorOverrideController)animRef.runtimeAnimatorController)["idle"]= replacementAnimTest;
…but I cannot find a way to access the other layers to override those as well, or to swap their animation clips.
Is there a way to do this or do I need to start from scratch and use the legacy animation scripting just so that I can set like:
Animator["idleAnimation"].layer= 1;
…or some other similar approach? If you even can do that in legacy?
Otherwise I’ll end up spawning gameobjects on the fly and trying to match them up all because I can’t access the other layers to assign clips.
Advice is appreciated!
SOLUTION
1) animation ‘layers’ in mechanim are NOT aimed at overlapping sprites, but are aimed at layering bone weights in animations, and so were not fit for my purpose to begin with.
2) When you set an override controller’s clip in the way mentioned above, you set it via it’s clip name not it’s state name. As mentioned below you could create dummy animations with funky names and replace those to potentially modify animations in other layers, though due to point 1), I won’t be doing this for my project as it won’t achieve the desired effect.
3) As a point of interest, my final solution was simply to create my own framework for overlaying gameobjects with sprite renderers and animators attached.
I created one animator to act as the base/pattern for all characters, and derived override controllers for all overlays and the parent overlay, e.g. one override for the body, one for the main hand weapons, one for offhand.
I then at runtime replace these overrides with an ‘instantiated clone’ of the override so that they may be tweaked independantly at runtime without affecting one another (by reference).
These clone overrides on child objects then act as the other overlapping ‘layers’ in a conventional, photoshop-like sense.
These layers are synchroinsed by setting the animator’s parameters to all children overrides in unison, if they have been created through the framework.
It’s worth noting that you must also set their state speeds to have the same duration overall for all clips of the same name/state upon being assigned. This seems to be because upon import, a clip’s sprites-per-second are sometimes interpreted differently based on things like the number of sprites overall.