微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的游戏在转换完成之前加载 Unity 我该如何解决这个问题?

如何解决我的游戏在转换完成之前加载 Unity 我该如何解决这个问题?

VIDEO

我的代码即使我有一个等待动画完成的收益,但它根本不等待,它只会延迟动画显示和播放游戏的时间。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossplatformInput;
using UnityEngine.SceneManagement;

public class replayscript : MonoBehavIoUr
{
    GameObject replaybutton;
    public Animator transitionERR;
    public float transitiontime = 19f;
    GameObject replay;
    public float timeRR = 0f;

    // Start is called before the first frame update

    // Update is called once per frame
    void Start()
    {
        replay.SetActive(false);
    }
    void Update()
    {
        if (CrossplatformInputManager.GetButton("replaybutton")){
            Debug.Log("candice");
            StartCoroutine(LoadLevel(1));
            replay.SetActive(true);
        }
    }


    IEnumerator LoadLevel(int levelIndex)
    {
        //play animation
        transitionERR.SetTrigger("start");


            //wait for the animaton
            yield return new WaitForSeconds(1.5f);
        //load the scene

         SceneManager.LoadScene(1);

    }

    }
   
    



解决方法

您可以尝试在使用 transitionERR.SetTrigger("start"); 之前添加 StartCoroutine(LoadLevel(1));

您可以为动画结束创建一个事件,然后将加载场景代码放在那里。 类似的东西:

//
            public UnityAnimationEvent OnAnimationComplete;
            public AnimationClip clip;
//
            AnimationEvent animationEndEvent = new AnimationEvent();
            animationEndEvent.time = clip.length;
            animationEndEvent.functionName = "AnimationCompleteHandler";
            animationEndEvent.stringParameter = clip.name;
            clip.AddEvent(animationEndEvent);
//
public void AnimationCompleteHandler(string name)
    {
        OnAnimationComplete?.Invoke(ChangeSceneMethodName);
    }

此外,如果您打算使用协程来获取动画持续时间,您可以使用 WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length+anim.GetCurrentAnimatorStateInfo(0).normalizedTime);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。