1. 과정
1. Animator.runtimeAnimatorController를 인자로 AnimatorOverrideController 객체를 생성
2. AnimatorOverrideController 객체에서 List<KeyValuePair<AnimationClip, AnimationClip>> 읽기
3. 가져온 List<KeyValuePair<AnimationClip, AnimationClip>>에서 원하는 AnimationClip 교체 후, AnimatorOverrideController 객체에 적용
4. 수정된 AnimatorOverrideController를 Animator.runtimeAnimatorController에 대입
2. Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeAnimationClipTest : MonoBehaviour
{
public AnimationClip newClip;
public string clipName;
public Animator animator;
void Start()
{
ShowClips(animator);
ChangeAnimationClip(animator, clipName, newClip);
ShowClips(animator);
}
public void ChangeAnimationClip(Animator targetAnimator, string targetClipName, AnimationClip toReplaceClip)
{
AnimatorOverrideController controller = new AnimatorOverrideController(targetAnimator.runtimeAnimatorController);
List<KeyValuePair<AnimationClip, AnimationClip>> clips = new();
controller.GetOverrides(clips);
for(int i =0; i < clips.Count; i++)
{
if(string.Equals(clips[i].Key.name, targetClipName))
{
Debug.Log($"{clips[i].Key.name} -> {toReplaceClip.name}");
clips[i] = new KeyValuePair<AnimationClip, AnimationClip>(clips[i].Key, toReplaceClip);
break;
}
}
controller.ApplyOverrides(clips);
targetAnimator.runtimeAnimatorController = controller;
}
public void ShowClips(Animator toShowAnimator)
{
AnimationClip[] clips = toShowAnimator.runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in clips)
{
Debug.Log("clip : " + clip.name);
}
}
}
- 처음 AnimatorOverrideController 객체의 KeyValuePair<AnimationClip, AnimationClip>의 Value 값은 null이고, 수정하지 않고 targetAnimator.runtimeAnimatorController에 대입하면 그대로 똑같이 동작함