Why does my Override Controller dosen't change the Animation Clip?
I'm trying to implement an Override Controller to override one Animation Clip for a cloned character at runtime. I tried a few different solution, but none of them are working. I hope somebody can help me.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StartScene_2 : MonoBehaviour
{
public GameObject charaPrefeb;
public AnimationClip newClip;
private Vector3 pos;
private Animator anim;
private AnimatorOverrideController animOverride;
private RuntimeAnimatorController animRuntim;
private AnimatorClipInfo[] m_CurrentClipInfo;
// Use this for initialization
void Start ()
{
pos = charaPrefeb.transform.position;
Debug.Log (pos.ToString());
pos.x = pos.x + 2.0f;
GameObject charaInstance;
charaInstance = Instantiate (charaPrefeb, pos, Quaternion.identity); //Cloning the charakter
anim = charaInstance.GetComponent<Animator> (); //animator of the clone
// SetCurrentAnimation(anim, newClip.name);
// CreateController(anim, newClip);
// animRuntim = anim.runtimeAnimatorController;
// animOverride = animRuntim as AnimatorOverrideController;
animOverride = new AnimatorOverrideController (); //creating override controller with the old runtime controller
animOverride.runtimeAnimatorController = anim.runtimeAnimatorController;
animOverride [newClip.name] = newClip; // setting new Animation clip
Debug.Log(newClip.name);
Debug.Log (animOverride.animationClips [0].name);
anim.runtimeAnimatorController = animOverride; //overriding old animator of clone with the new controller
anim.runtimeAnimatorController.name = "OverrideRunTimeController"; //name of the new controller
// anim.runtimeAnimatorController = Resources.Load("Assets/Mecanim") as RuntimeAnimatorController;
}
// Update is called once per frame
void Update ()
{
}
public void SetCurrentAnimation(Animator animator, string name)
{
RuntimeAnimatorController myController = animator.runtimeAnimatorController;
AnimatorOverrideController myOverrideController = myController as AnimatorOverrideController;
if (myOverrideController != null) {
myController = myOverrideController.runtimeAnimatorController;
}
AnimatorOverrideController animatorOverride = new AnimatorOverrideController ();
animatorOverride.runtimeAnimatorController = myController;
animatorOverride [name] = newClip;
animator.runtimeAnimatorController = animatorOverride;
animator.runtimeAnimatorController.name = "AnimatorOverrideController";
}
/* public void CreateController(Animator animator, AnimationClip clip)
{
RuntimeAnimatorController myOriginalController = animator.runtimeAnimatorController;
AnimatorOverrideController myCurrentOverrideController = myOriginalController as AnimatorOverrideController;
if (myCurrentOverrideController != null)
{
if (myCurrentOverrideController [clip.name] == clip)
{
Debug.Log("Current state is already " + clip.name);
return;
}
myOriginalController = myCurrentOverrideController.runtimeAnimatorController;
myCurrentOverrideController.runtimeAnimatorController = null;
}
AnimatorOverrideController myNewOverrideController = new AnimatorOverrideController ();
myNewOverrideController.runtimeAnimatorController = myOriginalController;
AnimatorStateInfo[] layerInfo = new AnimatorStateInfo[animator.layerCount];
for (int i = 0; i < animator.layerCount; i++) {
layerInfo [i] = animator.GetCurrentAnimatorStateInfo (i);
}
myNewOverrideController [clip.name] = clip;
Debug.Log (clip.name);
animator.runtimeAnimatorController = myNewOverrideController;
animator.runtimeAnimatorController.name = "AnimatorOverrideController";
animator.Update (0.0f);
for (int i = 0; i < animator.layerCount; i++) {
animator.Play (layerInfo [i].fullPathHash, i, layerInfo [i].normalizedTime);
}
m_CurrentClipInfo = animator.GetCurrentAnimatorClipInfo (0);
Debug.Log (m_CurrentClipInfo[0].clip.name);
Object.Destroy (myCurrentOverrideController);
}*/
}
Answer by Maxium · Mar 12, 2018 at 02:07 AM
I think the problem might be that the animation clip isn't overridden because the name of the clip you're intending to override doesn't match newClip.name.
animOverride [newClip.name] = newClip;
In this case, newClip.name should be either be the same as the clip or replaced with the name of the clip to override.
animOverride [clipToOverrideName] = newClip;
The following code works for me. Just change NameOfClipToOverride with the clip name to override.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartScene_2 : MonoBehaviour
{
public GameObject charaPrefeb;
public AnimationClip newClip;
private Vector3 pos;
private Animator anim;
private AnimatorOverrideController animOverride;
private RuntimeAnimatorController animRuntim;
void Start()
{
GameObject charaInstance;
charaInstance = Instantiate(charaPrefeb, pos, Quaternion.identity);
anim = charaInstance.GetComponent<Animator>();
animOverride = new AnimatorOverrideController();
animOverride.runtimeAnimatorController = anim.runtimeAnimatorController;
animOverride["NameOfClipToOverride"] = newClip;
anim.runtimeAnimatorController = animOverride;
anim.runtimeAnimatorController.name = "OverrideRunTimeController";
}
}
Your answer
Follow this Question
Related Questions
How do I add running and animations to my FPS controller script? 0 Answers
How to prevent a variable used in two functions from being overridden? [SOLVED] 1 Answer
Can I change scripting runtime version in a middle of the project? 0 Answers
is it possible to load .anim file runtime? 0 Answers
2d controller question (script) 0 Answers