纳金网

标题: animator动画从指定帧开始播放 [打印本页]

作者: 烟雨    时间: 2019-11-28 15:32
标题: animator动画从指定帧开始播放

来自:18803836360
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;

  4. public class playIndexFrame : MonoBehaviour {

  5.     public Animator _currentAnimator;
  6.     Clip[] _acInfo;
  7.     /// <summary>
  8.     /// 假设的已经播放的动画帧数
  9.     /// </summary>
  10.     public float _hadPlay;
  11.     void Awake()
  12.     {
  13.         _acInfo = _currentAnimator.runtimeAnimatorController.animationClips;
  14.         Debug.Log(_acInfo[0].length * _acInfo[0].frameRate);
  15.     }

  16.     public void setSolidFrame(int _index)
  17.     {
  18.         //瞬间的隐藏于显示可以重置animator的动画
  19.         _currentAnimator.gameObject.SetActive(false);
  20.         _currentAnimator.gameObject.SetActive(true);
  21.         //确认动画速度正常
  22.         _currentAnimator.speed = 1;
  23.         _hadPlay = _index;
  24.         Debug.Log("动画的总的时间:"+_acInfo[0].length);
  25.         //已播放的帧数占据总帧数的比例
  26.         float _temp = _hadPlay / (_acInfo[0].length * _acInfo[0].frameRate);
  27.         Debug.Log(_temp);
  28.         //动画已经播放过的时长
  29.         Debug.Log(_temp * _acInfo[0].length);
  30.         //更新动画到指定帧
  31.         _currentAnimator.Update(_temp * _acInfo[0].length);
  32.     }


  33.     void Update()
  34.     {
  35.         if (Input.GetKeyDown(KeyCode.A))
  36.         {
  37.             setSolidFrame(10);
  38.         }
  39.         if (Input.GetKeyDown(KeyCode.S))
  40.         {
  41.             setSolidFrame(40);
  42.         }
  43.         if (Input.GetKeyDown(KeyCode.D))
  44.         {
  45.             setSolidFrame(80);
  46.         }
  47.         if (Input.GetKeyDown(KeyCode.F))
  48.         {
  49.             setSolidFrame(120);
  50.         }
  51.     }
  52. }
复制代码





欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5