using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class AnimMgr : MonoBehaviour
{
public int id;
RuntimeAnimatorController rac;
// Start is called before the first frame update
void Start()
{
//加载动画控制器
rac = Resources.Load<RuntimeAnimatorController>("Anim/Anim");
//设置默认动画控制器
GetComponent<Animator>().runtimeAnimatorController = rac;
//监听动画
MessageCenter.Ins.Add(MsgID.SKILL, OnSkill);
}
//技能动画
public void OnSkill(object obj)
{
object[] arr = obj as object[];
int skillid = (int)arr[0];
GameObject player = arr[1] as GameObject;
if (player == transform.parent.gameObject)
{
//获取技能对应动画地址
string path = ReadTable.skillDic[skillid].animpath;
//设置动画
SetAnim(path);
}
}
public AnimationClip SetAnim(string path)
{
//根据路径加载动画
AnimationClip clip = ABManager.Ins.Load<AnimationClip>("AnimMgr", "Anim", path);
//重写控制器
AnimatorOverrideController aoc = new AnimatorOverrideController();
//设置为默认控制器
aoc.runtimeAnimatorController = rac;
//把默认动画改为加载的动画
aoc["Default"] = clip;
//清空
GetComponent<Animator>().runtimeAnimatorController = null;
//赋值
GetComponent<Animator>().runtimeAnimatorController = aoc;
//播放指定动画
GetComponent<Animator>().Play("Default");
GetComponent<Animator>().applyRootMotion = false;
if (transform.CompareTag("Player"))
{
float time = clip.length;
Invoke("Zero", time);
}
return clip;
}
public void Zero()
{
transform.parent.DOMove(transform.position, 0.5f);
transform.parent.DORotate(transform.eulerAngles, 0.5f);
transform.DOLocalRotate(Vector3.zero, 0.5f);
transform.DOLocalMove(Vector3.zero, 0.5f);
}
}
unity下控制Animator播放指定动画
最新推荐文章于 2024-07-21 19:42:40 发布