网页设计建网站,口碑营销案例简短,网站导航设置,网站发布方式 提高一.StateMachineBehaviour概述
状态机行为是一类特殊脚本。与将常规 Unity 脚本 (MonoBehaviour) 附加到单个游戏对象类似#xff0c;您可以将 StateMachineBehaviour 脚本附加到状态机中的单个状态。因此可编写一些将在状态机进入、退出或保持在特定状态时执行的代码。这意味…一.StateMachineBehaviour概述
状态机行为是一类特殊脚本。与将常规 Unity 脚本 (MonoBehaviour) 附加到单个游戏对象类似您可以将 StateMachineBehaviour 脚本附加到状态机中的单个状态。因此可编写一些将在状态机进入、退出或保持在特定状态时执行的代码。这意味着您不必编写自己的逻辑来测试和检测状态的变化。
参考官方手册状态机行为
二.StateMachineBehaviour脚本创建 如上图所示选中状态节点点击Add Behavior按钮创建脚本后双击进入脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class attackState : StateMachineBehaviour
{// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state//override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//{// //}// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//{// //}// OnStateExit is called when a transition ends and the state machine finishes evaluating this state//override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//{// //}// OnStateMove is called right after Animator.OnAnimatorMove()//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//{// // Implement code that processes and affects root motion//}// OnStateIK is called right after Animator.OnAnimatorIK()//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//{// // Implement code that sets up animation IK (inverse kinematics)//}
}脚本继承StateMachineBehaviour并有五个虚方法比较常用的是前面3个
三.案例演示 Demo说明:模型初始为idle状态按下Q播放攻击动作进入动作动作进行中动作结束时引擎会自动调用OnStateEnterOnStateUpdateOnStateExit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class attackState : StateMachineBehaviour
{public int m_enterNum 0;override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){m_enterNum;Debug.Log(string.Format(OnStateEnter, m_enterNum {0}, m_enterNum));}override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){Debug.Log(OnStateUpdate);}override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){Debug.Log(OnStateExit);}}事件方法参数说明
animator脚本所在状态节点所在的animator组件AnimatorStateInfo以get属性为主的结构体layerIndex脚本所在状态节点的动画层索引
下面代码展示MonoBehaviour访问StateMachineBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Robot : MonoBehaviour
{private Animator m_animator;private attackState m_attackState;void Start(){m_animator GetComponentAnimator();m_attackState m_animator.GetBehaviourattackState();}void Update(){if (Input.GetKeyDown(KeyCode.Q)){m_animator.SetTrigger(attack);Debug.Log(string.Format(进入attack状态之前 m_enterNum {0}, m_attackState.m_enterNum));}}
} 文章转载自: http://www.morning.lskrg.cn.gov.cn.lskrg.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.ktnmg.cn.gov.cn.ktnmg.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.ykgp.cn.gov.cn.ykgp.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.ssmhn.cn.gov.cn.ssmhn.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn