unity下控制Animator播放指定动画

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);
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值