• [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.输出如下

  • 相关阅读:
    未来行业
    百度网盘搜索方法
    JavaScript继承详解
    Win 7下破解Loadrunner 11(带中文版下载地址)
    NET下RabbitMQ实践[WCF发布篇]
    NET下RabbitMQ实践[示例篇]
    NET下RabbitMQ实践[配置篇]
    8个超炫的 Web 效果
    Windows下安装zip包解压版mysql
    键盘上相当于鼠标右键的快捷键和电脑快捷键大全
  • 原文地址:https://www.cnblogs.com/lyh916/p/10371947.html
Copyright © 2020-2023  润新知