1.6.1 GUI动画相应延迟

问题描述:在GUI插入动画时,当点击BUTTON希望动画播放。 思路为点击button 调用编辑好的animator,向animator传入指定值 完成动画播放。 但是值传入animator之后会出现 动画播放延迟问题。

更新:后来发现 只要修改Trasition中的has exit time。将其取消就没有延迟了!!!


下述方法使用了旧版本的animation。对比后发现,旧版本的优势在于所有代码都是自己管理,新版本利用animator托管这些代码。

解决方法 :弃用 animator 使用 animation ,将动画片段clip 加入animation中。 再利用button 直接调用 animation.play();

遇到的问题:执行 animation1.Play("UIAnimation") 是会提示无法找到对应动画片段的错误。对于以上问题是因为使用animation.Play()时clip只能是legacy模式。具体修改方法为找到对应动画片段后

点击

选择debug

在debug中 将legacy打勾


不足:未解决animator为什么会存在延迟现象。


注释 : 下述代码中//animator.SetBool("isShowMsg", true);为第一次使用animator传值方法出现延迟的代码。

 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class btnShowMsg : MonoBehaviour {


    public Animation animation1; //获取动画 针对animator响应延迟
    public Animator animator;  //获取动画控制器 (有响应延迟现象)
    public Text text;  //获取本btn显示文本


    public void Click()
    {
        if(text.text == "ShowMsg")
        {
            //animator.SetBool("isShowMsg", true);
            text.text = "HideMsg";
            animation1["UIAnimation"].speed = 1;
            if (!animation1.isPlaying) animation1.Play("UIAnimation"); ;//if 防止正在播放的情况下调用Play








        }
        else
        {
            text.text = "ShowMsg";
            //animator.SetBool("isShowMsg", false);
            //下面两行代码实现动画倒放
            animation1["UIAnimation"].time = animation1["UIAnimation"].clip.length; //起始时间点设置为终点
            animation1["UIAnimation"].speed = -1; //播放速度为-1
            if (!animation1.isPlaying) animation1.Play("UIAnimation"); ;//if 防止正在播放的情况下调用Play


        }
    }
}


猜你喜欢

转载自blog.csdn.net/mindcastle/article/details/80629728