• [Unity动画]03.动画事件


    1.找到动画,添加动画事件

    2.在脚本中添加回调方法

    TestAnimator.cs

     1 using UnityEngine;
     2 
     3 public class TestAnimator : MonoBehaviour {
     4 
     5     //------------------------------------------外部
     6     public float Move = 0;
     7     public bool IsDying = false;
     8 
     9     //------------------------------------------内部
    10     private Animator animator;
    11 
    12     //常数
    13     private int baseLayerIndex;
    14     private int idleStateHash;
    15     private int runStateHash;
    16     private int dyingStateHash;
    17     private string movePara;
    18     private string isDyingPara;
    19 
    20     void Start ()
    21     {
    22         animator = GetComponent<Animator>();
    23 
    24         baseLayerIndex = animator.GetLayerIndex("Base Layer");
    25         idleStateHash = Animator.StringToHash("Base Layer.Idle");
    26         runStateHash = Animator.StringToHash("Base Layer.Run");
    27         dyingStateHash = Animator.StringToHash("Base Layer.Dying");
    28         movePara = "move";
    29         isDyingPara = "isDying";
    30     }
    31     
    32     void Update ()
    33     {
    34         //------------------------------------------播放动作
    35         animator.SetFloat(movePara, Move);
    36         if (IsDying)
    37         {
    38             animator.SetBool(isDyingPara, true);
    39             IsDying = false;
    40         }
    41 
    42         //------------------------------------------动作恢复
    43         AnimatorStateInfo stateInfo;
    44         int fullPathHash;
    45 
    46         //BaseLayer
    47         stateInfo = animator.GetCurrentAnimatorStateInfo(baseLayerIndex);
    48         if (!animator.IsInTransition(baseLayerIndex) && stateInfo.normalizedTime >= 1)
    49         {
    50             fullPathHash = stateInfo.fullPathHash;
    51             if (fullPathHash == dyingStateHash)
    52             {
    53                 animator.SetBool(isDyingPara, false);
    54             }
    55         }
    56     }
    57 
    58     public void SetDying(bool state)
    59     {
    60         if (animator)
    61         {
    62             animator.SetBool(isDyingPara, state);
    63         }
    64     }
    65 
    66     public void ActionCallBack(string s)
    67     {
    68         if (s == "dyingStart")
    69         {
    70             Debug.Log("111");
    71         }
    72         else if (s == "dying")
    73         {
    74             Debug.LogWarning("222");
    75         }
    76         else if (s == "dyingEnd")
    77         {
    78             Debug.LogError("333");
    79         }
    80     }
    81 }

    3.输出如下

  • 相关阅读:
    杂想
    杂题操作
    codeforces 11D(状压dp)
    2019 计蒜之道 复赛 “星云系统” (单调栈)
    SPOJ VLATTICE (莫比乌斯反演)
    2019 ICPC 陕西西安邀请赛 D. Miku and Generals
    buerdepepeqi 的模版
    HDU 2588 GCD
    二项式反演
    2014ACM/ICPC亚洲区西安站 F题 color (组合数学,容斥原理)
  • 原文地址:https://www.cnblogs.com/lyh916/p/10371947.html
Copyright © 2020-2023  润新知