简体   繁体   English

Unity 3D 试图引用动画师中附加到空状态的脚本。 获取组件不工作

[英]Unity 3D Trying to reference script that is attached to empty state in the animator. GetComponent Not Working

Is there a way to reference the script that is attached to the empty state in the animator?有没有办法引用动画师中附加到空状态的脚本? I have a script called ResetAnimatorBool and I want to reference it from another script but GetComponent<ResetAnimatorBool> does not work我有一个名为ResetAnimatorBool的脚本,我想从另一个脚本中引用它,但GetComponent<ResetAnimatorBool>不起作用

The idea is to see if the bool is false(if the animation is finished and back to the empty state) and if it isn't and the animation is still playing, then disable controls until the animation is finished.我们的想法是查看 bool 是否为 false(如果动画完成并返回空状态),如果不是并且动画仍在播放,则禁用控件直到动画完成。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
    
public class ResetAnimatorBool : StateMachineBehaviour
{
    [Header("Disable Root Motion")]
    public string disableRootMotion = "disableRootMotion";
    public bool disableRootMotionStatus = false;
    
    [Header("Is Performing Action Bool")]
    public string isPerformingAction = "isPerformingAction";
    public bool isPerformingActionStatus = false;
    
    [Header("Is Preforming Quick Turn")]
    public string isPerformingQuickTurn = "isPerformingQuickTurn";
    public bool isPerformingQuickTurnStatus = false;
    
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool(disableRootMotion, disableRootMotionStatus);
        animator.SetBool(isPerformingAction, isPerformingActionStatus);
        animator.SetBool(isPerformingQuickTurn, isPerformingQuickTurnStatus);
    }
}

First refer to the animator you want and then use Animator.GetBehaviour<>() :首先参考您想要的动画师,然后使用Animator.GetBehaviour<>()

public Animator playerAnimator;
public void Start()
{
    playerAnimator = GetComponent<Animator>();

    var disableRootMotion = playerAnimator.GetBehaviour<ResetAnimatorBool>().disableRootMotion;
    
    Debug.Log(disableRootMotion); // disableRootMotion
}

Also do the following to get all the behaviors.还可以执行以下操作以获取所有行为。

var behaviours = playerAnimator.GetBehaviours<StateMachineBehaviour>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM