【问题标题】:Unity calculate total animator to complete then change sceneUnity计算总动画师完成然后改变场景
【发布时间】:2018-04-10 04:01:17
【问题描述】:

我试图从场景中显示退出动画,同时使用 LoadSceneAsync 加载新场景,将场景保留在内存中直到动画结束,然后切换到新场景,如 this question already solved

问题是我有几个动画在层上运行,它们都由同一个触发器触发,现在我所有的动画都是 1 seg 长度,但有些从 0 开始,有些从 0.5 seg 开始

我如何计算完成动画所需的总时间,异步加载新场景,然后等待动画时间完成或新场景的加载完成(需要更长的时间),最后换个场景?

我正在考虑使用 2 个同时运行的协程并等待最长的完成,但我不确定这是否可以完成(我对 C# 编程很陌生),有什么想法吗?

现在我正在等待 1.6 Seg 的固定时间,因为我的动画在时间上是相等的,但这可能会改变,这是我能想到的唯一方法......

public void GotoScene(string scene)
{
    // Start exit animations and wait
    CanvasAnimation.SetBool("hide", true);
    StartCoroutine(ChangeScene(1.6f, scene));
}

IEnumerator ChangeScene(float time, string goToScene)
{
    //Set the current Scene to be able to unload it later
    Scene currentScene = SceneManager.GetActiveScene();

    // Wait for exit animation to finish
    yield return new WaitForSeconds(time);

    // The Application loads the Scene in the background at the same time as the current Scene.
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(goToScene, LoadSceneMode.Additive);

    //Wait until the last operation fully loads to return anything
    while (!asyncLoad.isDone)
    {
        yield return null;
    }

    //Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
    SceneManager.MoveGameObjectToScene(WireRoomObj, SceneManager.GetSceneByName(goToScene));

    //Unload the previous Scene
    SceneManager.UnloadSceneAsync(currentScene);
}

【问题讨论】:

    标签: c# animation unity3d async-await


    【解决方案1】:

    您可能会从AnimatorStateInfo.normalizedTime 检查动画状态,然后在当前动画的 normalizedTime 低于 1 时在协程中等待。例如:

    using System;
    using System.Linq;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class WaitUntilComplete : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            StartCoroutine(WaitUntilAllAnimationsCompleted());
        }
    
        IEnumerator WaitUntilAllAnimationsCompleted() {
            print("waiting started " + Time.time);
            yield return new WaitWhile(() => GetAllAnimationStates().Any(state => state.normalizedTime < 1f));
            print("waiting ended " + Time.time);
        }
    
        List<AnimatorStateInfo> GetAllAnimationStates() {
            List<AnimatorStateInfo> states = new List<AnimatorStateInfo>();
            var animatorList = GetComponentsInChildren<Animator>();
            foreach (var anim in animatorList) {
                for (int i = 0; i < anim.layerCount; ++i) {
                    states.Add(anim.GetCurrentAnimatorStateInfo(i));
                }
            }
            return states;
        }
    
    }
    

    【讨论】:

    • mmm 不起作用......也许现在我会等待一个固定的时间,直到我找到更好的答案,无论如何感谢您的帮助
    猜你喜欢
    • 2022-01-28
    • 1970-01-01
    • 2023-01-31
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多