淡出动画Unity加载屏幕

时间:2016-01-07 20:03:14

标签: c# unity3d

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

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

  1. 我无法让淡出工作。我尝试设置' allowSceneActivation'对于我的异步加载,为false,但这会导致加载完全不发生。删除此行会使游戏加载,但它会消失淡出。
  2. 动画很有效(我的意思是非常)不稳定。我知道游戏正在加载东西,所以我认为它很糟糕,但它每2秒就会做一次框架。我尝试使用低线程优先级(参见下面的代码),但没有运气。我发现有类似问题的人,但在使用较低线程优先级时框架变得合理。
  3. 这是我加载屏幕的代码:

    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);
            }
        }
    }
    

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

    非常感谢提前!

1 个答案:

答案 0 :(得分:1)

当场景加载完成时,你正逐渐消失。加载完成后你有什么期望? :)

size

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

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

//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检查两件事。

1-开始淡出。

2-切换场景。

再一次,为什么它应该等待它完全淡出它而得到loaded并且你正在检查loaded哪个应该在alpha >= 0.95为真时在第一帧执行,因为我相信第一帧loaded会大于alpha