本文共 1430 字,大约阅读时间需要 4 分钟。
作用:Unity中在人物播放某个动画时往往伴随着动画音效的播放,或则是人物打击的粒子特效播放
旧方法:Animation(Ctrl+6)动画中添加Event事件,然后填写注册的方法名,Unity会在挂载该动画的物体上查找该方法,在动画播放到定义事件的那一帧时,会执行该方法。使用StateMachineBehaviours明显会方便很多
例子:在人物攻击时播放攻击特效,攻击动画结束关闭攻击特效
首先类继承StateMachineBehaviour,然后重写OnStateEnter,OnStateExit方法
override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex){ }
Animator动画参数是特定的animator ,是这个状态机行为的引用。
· AnimatorStateInfo 是状态机的行为是对state 的当前信息。相当于writing animator.GetCurrentStateInfo(layerIndex);
· LayerIndex 是状态机行为状态的layer 层。例如,0为基底图层,1用于第一个 等等。
步骤:将特效物体放在人物下成为子物体
AttackSMB.cs
using System.Collections;using System.Collections.Generic;using UnityEngine;public class AttackSMB : StateMachineBehaviour{ public int index; Transform effect; public override void OnStateEnter(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex) { //显示武器 //animator.transform.GetComponent().ShowWeapon(); //获取特效 effect= animator.transform.Find("TrailEffect/Ellen_Staff_Swish0" + index); //显示特效 effect.gameObject.SetActive(false); effect.gameObject.SetActive(true); } public override void OnStateExit(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex) { //animator.transform.GetComponent ().HideWeapon(); //隐藏特效 effect.gameObject.SetActive(false); }}
最后在对应动画上添加AttackSMB脚本,修改index值即可
转载地址:http://uex.baihongyu.com/