本文主要是介绍[Unity] StateMachineBehaviour简单调用MonoBehaviour的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
### 在动画状态机中,动画片段上添加脚本,并调用其他MonoBehaviour的方法
在动画片段上添加如下脚本:
using UnityEngine;public class testSMB : 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 stateoverride public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){m_MonoBehaviour.OnAction();}// 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)//}protected testMonoBehavior m_MonoBehaviour;public void Initialise(testMonoBehavior mono){m_MonoBehaviour = mono;}}
然后新建一个继承自MonoBehaviourde脚本
using UnityEngine;
using UnityEngine.Events;public class testMonoBehavior : MonoBehaviour
{private Animator anim;private testSMB smb;public UnityEvent testAction;private void Awake(){anim = GetComponent<Animator>();smb = anim.GetBehaviour<testSMB>();smb.Initialise(this);}public void OnAction(){testAction?.Invoke();}
}
这篇关于[Unity] StateMachineBehaviour简单调用MonoBehaviour的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!