- Home /
Question by
Bisorinho-Fonseca · Nov 24, 2020 at 10:41 PM ·
animationcontrollerassetbundlehow-tooverride
What I'm doing wrong here? (assetBundles and AnimatorOverrideController)
Howdy companions!
Resuming... I want to import a asset bundle which contain various animations to override a template (via script), the process of loading the assets is working fine, but I guess I'm missing something in override.
In this moment, I am testing only with one animation clip, and overriding it to two animations of the template
Here the scripts and its functions:
First Script - Animations mounter
This is the main script to override the anims it is initialized after the awake of the main script of player
public class AnimationAtlas : MonoBehaviour
{
public PlayerMov player;
public CharacterAll characterAll; // script which load the animation clips and the override controller
public AnimationClip[] animationsAtlas;
public Animator animator; // will be the animator attribute of the player
public Animation animationWindow; // only a test
public AnimatorOverrideController animatorOverrideController; // will receive the runtimeAnimatorController
public List<KeyValuePair<AnimationClip, AnimationClip>> overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>(); // parameter to GetOverrides()
public void InstantiateObjects()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMov>(); // PlayerMov is the main script of player
characterAll = GetComponent<CharacterAll>();
animator = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>();
animationsAtlas = characterAll.LoadAssetBundleAnimationsClips();
animatorOverrideController = characterAll.LoadAssetBundleController();
CreateAnimations();
}
public void CreateAnimations()
{
RuntimeAnimatorController myController = animator.runtimeAnimatorController; // process I found in Unity documentation ...
animatorOverrideController.runtimeAnimatorController = myController; // ... and another answers
animatorOverrideController.GetOverrides(overrides);
for (int i = 0; i < overrides.Count; i++) // for each possible override ...
{
animatorOverrideController[i.ToString()] = animationsAtlas[i]; // ... assign the animation clip of index "i"
}
}
Second Script - Player script
This is the player start method inside his script (and some global variables)
public class PlayerMov : MonoBehaviour
{
public Vector3 latePosition;
public Animator animator;
public AnimationAtlas animationAtlas;
private GameObject p1, p2;
public void Start()
{
animationAtlas = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AnimationAtlas>(); // the main anim script is on camera
animationAtlas.InstantiateObjects(); // calls the script above
p1 = GameObject.FindGameObjectWithTag("Player1");
p2 = GameObject.FindGameObjectWithTag("Player2");
animator = GetComponent<Animator>();
}
Third Script - Assets loader
This is the scripts thats load all assets, its divided in 2 methods thats:
- Load the AnimationOverrideController and returns it
- Load the AnimationClips and returns it
public class CharacterAll : MonoBehaviour
{
public AnimationClip[] animationClips;
public AnimatorOverrideController LoadAssetBundleController()
{
var myLoadedAssetBundle = AssetBundle.LoadFromFile(StaticComponents.buildsOutput + "/animation/controller");
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
return null;
}
return myLoadedAssetBundle.LoadAsset<AnimatorOverrideController>("Player1");
}
public AnimationClip[] LoadAssetBundleAnimationsClips()
{
var myLoadedAssetBundle = AssetBundle.LoadFromFile(StaticComponents.buildsOutput + "/animation/" + StaticComponents.choose);
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
return null;
}
animationClips = myLoadedAssetBundle.LoadAllAssets<AnimationClip>();
return animationClips;
}
Comment