【问题标题】:Fading out animated Unity loading screen淡出动画 Unity 加载屏幕
【发布时间】:2016-01-08 04:51:52
【问题描述】:

我试图在我的菜单场景和游戏场景之间设置一个简单的动画加载屏幕。我试图通过在我的加载场景中异步加载游戏场景来做到这一点。我还希望加载屏幕淡入淡出。

我让淡入工作。但是,我有两个问题,我已经工作了几个小时,但没有任何成功。这些问题是:

  1. 我无法让淡出工作。我尝试将“allowSceneActivation”设置为 false 以进行异步加载,但这会导致加载根本不发生。移除这条线会使游戏加载,但它不会淡出。
  2. 动画效果非常(我的意思是非常)波涛汹涌。我知道游戏正在加载东西,所以我认为它会很糟糕,但它几乎每 2 秒就会做一帧。我尝试使用低线程优先级(见下面的代码),但没有运气。我发现有人有类似的问题,但是当使用较低的线程优先级时,帧结果是合理的。

这是我的加载屏幕代码:

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

public class LoadIntro : MonoBehaviour {

    private bool loaded;
    private bool fadingOut;
    private bool loading;
    AsyncOperation async;

    void Start(){
        loaded = false;
        fadingOut = false;
        loading = false;
        Application.backgroundLoadingPriority = ThreadPriority.Low;
    }

    // Use this for initialization
    void Update() {
        //wait for loading screen to fade in, then execute once
        if (!GameObject.Find ("SceneFader").GetComponent<Image> ().enabled && !loaded && !loading) {
            loading = true;
            async = Application.LoadLevelAsync(mainMenuButtons.leveltoload);
            async.allowSceneActivation = false;
            StartCoroutine (LoadLevel (async));
        }

        //if next scene is loaded, start fading out loading screen
        if (loaded) {
            GameObject.Find ("SceneFader").GetComponent<SceneFadeInOut> ().FadeToBlack();
            fadingOut = true;
        }

        //when faded out, switch to new scene
        if (GameObject.Find ("SceneFader").GetComponent<Image> ().color.a >= 0.95f && loaded) {
            async.allowSceneActivation = true;
        }
    }

    IEnumerator LoadLevel(AsyncOperation async){
        yield return async;
        Debug.Log("Loading complete");
        loaded = true;
    }
}

我有一段单独的代码用于实际褪色,上面的代码调用它:

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

public class SceneFadeInOut : MonoBehaviour
{
    public float fadeSpeed = 1.5f;          // Speed that the screen fades to and from black.

    private bool sceneStarting = true;      // Whether or not the scene is still fading in.

    public bool sceneEnding = false;

    public string scene;

    private Image fadeTexture;


    void Awake ()
    {
        fadeTexture = GetComponent<Image>();
    }


    void Update ()
    {
        // If the scene is starting...
        if(sceneStarting)
            // ... call the StartScene function.
            StartScene();

        if (sceneEnding)
            EndScene();
    }


    void FadeToClear ()
    {
        // Lerp the colour of the texture between itself and transparent.
        fadeTexture.color = Color.Lerp(fadeTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
    }


    public void FadeToBlack ()
    {
        // Lerp the colour of the texture between itself and black.
        fadeTexture.color = Color.Lerp(fadeTexture.color, Color.black, fadeSpeed * Time.deltaTime);
    }


    void StartScene ()
    {
        // Fade the texture to clear.
        FadeToClear();

        // If the texture is almost clear...
        if(fadeTexture.color.a <= 0.05f)
        {
            // ... set the colour to clear and disable the GUITexture.
            fadeTexture.color = Color.clear;
            fadeTexture.enabled = false;

            // The scene is no longer starting.
            sceneStarting = false;
        }
    }


    public void EndScene ()
    {
        // Make sure the texture is enabled.
        fadeTexture.enabled = true;

        // Start fading towards black.
        FadeToBlack();

        // If the screen is almost black...
        if (fadeTexture.color.a >= 0.95f) {
            // ... reload the level.
            if (scene == "") Application.Quit();
            else Application.LoadLevel (scene);
        }
    }
}

有人知道如何解决上述问题吗?我已经尝试了我能找到的每一个主题,但它们似乎都不起作用。构建我的游戏也没有解决问题。

非常感谢!

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    当场景加载完成时,你会淡出。加载完成后你期待什么? :)

    //if next scene is loaded, start fading out loading screen
            if (async.isDone) {
                GameObject.Find ("SceneFader").GetComponent<SceneFadeInOut> ().FadeToBlack();
                fadingOut = true;
            }
    

    显然它会改变场景并且你的代码没有足够的时间来执行淡出操作。 :)

    例如,如果考虑您的观点。你写的,

    //if next scene is loaded, start fading out loading screen
            if (loaded) {
                GameObject.Find ("SceneFader").GetComponent<SceneFadeInOut> ().FadeToBlack();
                fadingOut = true;
            }
    
            //when faded out, switch to new scene
            if (GameObject.Find ("SceneFader").GetComponent<Image> ().color.a >= 0.95f && loaded) {
                async.allowSceneActivation = true;
            }
    

    Update。在这里你的loaded 检查做两件事。

    1- 开始淡出。

    2- 切换场景。

    再次,为什么它应该等待它在获得loaded 时完全消失,而您正在检查alpha &gt;= 0.95,当您将loaded 设置为真时,它应该在第一帧执行,因为我相信在第一帧@ 987654329@ 将大于 0.95

    【讨论】:

    • 感谢您的快速回复!这确实没有多大意义,我已经在我的代码中更改了它。但是,这并不能解决问题, async.allowSceneActivation = false 仍然以某种方式阻止加载不发生..
    • 再次感谢您的帮助!在淡出期间(在加载结束时),alpha 实际上正在上升,因为我使用的是黑色叠加层(SceneFader 的图像组件),它正在慢慢覆盖加载屏幕。因此,一旦淡出(或:覆盖层的淡入)完成,将执行第三个 if 语句。
    • 我还没有找到解决方案,尤其是第一个问题。我可以忍受我猜想的波涛汹涌。但我真的很想在游戏加载后修复淡出。有什么想法吗?
    • 你的ImagefadeTexture的初始配置是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多