【问题标题】:Scene is not Loading场景未加载
【发布时间】:2018-11-25 16:30:27
【问题描述】:

一共有9个场景。所有都添加在“构建设置”中。 9 个场景中有 7 个正在加载,但是当我尝试加载其他 2 个场景时,它一直在说:

无法加载场景“SceneName”,因为它尚未添加到构建设置或 AssetBundle 尚未加载。要将场景添加到构建设置,请使用菜单 File->Build Settings... UnityEngine.SceneManagement.SceneManager:LoadSceneAsync(String, LoadSceneMode) MoreMountains.CorgiEngine.c__Iterator0:MoveNext() (在 Assets/CorgiEngine/Common/Scripts/ Managers/LoadingSceneManager.cs:88) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) MoreMountains.CorgiEngine.LoadingSceneManager:Start()(在 Assets/CorgiEngine/Common/Scripts/Managers/LoadingSceneManager.cs:67)

我已经在场景管理器中添加了场景,但它一直显示这个错误。我已经尝试了很多。请帮我解决问题。这是我的“LoadingSceneManager”脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using MoreMountains.Tools;
using UnityEngine.SceneManagement;

namespace MoreMountains.CorgiEngine
{   
public class LoadingSceneManager : MonoBehaviour 
{
    [Header("Binding")]
    /// The name of the scene to load while the actual target scene is 
    loading (usually a loading screen)
    public static string LoadingScreenSceneName="LoadingScreen";

    [Header("GameObjects")]
    /// the text object where you want the loading message to be displayed
    public Text LoadingText;
    /// the canvas group containing the progress bar
    public CanvasGroup LoadingProgressBar;
    /// the canvas group containing the animation
    public CanvasGroup LoadingAnimation;
    /// the canvas group containing the animation to play when loading is complete
    public CanvasGroup LoadingCompleteAnimation;

    [Header("Time")]
    /// the duration (in seconds) of the initial fade in
    public float StartFadeDuration=0.2f;
    /// the speed of the progress bar
    public float ProgressBarSpeed=2f;
    /// the duration (in seconds) of the load complete fade out
    public float ExitFadeDuration=0.2f;
    /// the delay (in seconds) before leaving the scene when complete
    public float LoadCompleteDelay=0.5f;

    protected AsyncOperation _asyncOperation;
    protected static string _sceneToLoad = "";
    protected float _fadeDuration = 0.5f;
    protected float _fillTarget=0f;
    protected string _loadingTextValue;

    /// <summary>
    /// Call this static method to load a scene from anywhere
    /// </summary>
    /// <param name="sceneToLoad">Level name.</param>
    public static void LoadScene(string sceneToLoad) 
    {       
        _sceneToLoad = sceneToLoad;                 
        Application.backgroundLoadingPriority = ThreadPriority.High;
        if (LoadingScreenSceneName!=null)
        {
            SceneManager.LoadScene(LoadingScreenSceneName);
        }
    }

    /// <summary>
    /// On Start(), we start loading the new level asynchronously
    /// </summary>
    protected virtual void Start() 
    {
        _loadingTextValue=LoadingText.text;
        if (_sceneToLoad != "")
        {
            StartCoroutine(LoadAsynchronously());
        }
    }

    /// <summary>
    /// Every frame, we fill the bar smoothly according to loading progress
    /// </summary>
    protected virtual void Update()
    {
        LoadingProgressBar.GetComponent<Image>().fillAmount = MMMaths.Approach(LoadingProgressBar.GetComponent<Image>().fillAmount,_fillTarget,Time.deltaTime*ProgressBarSpeed);
    }

    /// <summary>
    /// Loads the scene to load asynchronously.
    /// </summary>
    protected virtual IEnumerator LoadAsynchronously() 
    {
        // we setup our various visual elements
        LoadingSetup();

        // we start loading the scene
        _asyncOperation = SceneManager.LoadSceneAsync(_sceneToLoad,LoadSceneMode.Single );
        _asyncOperation.allowSceneActivation = false;

        // while the scene loads, we assign its progress to a target that we'll use to fill the progress bar smoothly
        while (_asyncOperation.progress < 0.9f) 
        {
            _fillTarget = _asyncOperation.progress;
            yield return null;
        }
        // when the load is close to the end (it'll never reach it), we set it to 100%
        _fillTarget = 1f;

        // we wait for the bar to be visually filled to continue
        while (LoadingProgressBar.GetComponent<Image>().fillAmount != _fillTarget)
        {
            yield return null;
        }

        // the load is now complete, we replace the bar with the complete animation
        LoadingComplete();
        yield return new WaitForSeconds(LoadCompleteDelay);

        // we fade to black
        GUIManager.Instance.FaderOn(true,ExitFadeDuration);
        yield return new WaitForSeconds(ExitFadeDuration);

        // we switch to the new scene
        _asyncOperation.allowSceneActivation = true;
    }

    /// <summary>
    /// Sets up all visual elements, fades from black at the start
    /// </summary>
    protected virtual void LoadingSetup() 
    {
        GUIManager.Instance.Fader.gameObject.SetActive(true);
        GUIManager.Instance.Fader.GetComponent<Image>().color=new Color(0,0,0,1f);
        GUIManager.Instance.FaderOn(false,ExitFadeDuration);

        LoadingCompleteAnimation.alpha=0;
        LoadingProgressBar.GetComponent<Image>().fillAmount = 0f;
        LoadingText.text = _loadingTextValue;

    }

    /// <summary>
    /// Triggered when the actual loading is done, replaces the progress bar with the complete animation
    /// </summary>
    protected virtual void LoadingComplete() 
    {
        LoadingCompleteAnimation.gameObject.SetActive(true);
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingProgressBar,0.1f,0f));
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingAnimation,0.1f,0f));
        StartCoroutine(MMFade.FadeCanvasGroup(LoadingCompleteAnimation,0.1f,1f));

    }
}

}

【问题讨论】:

  • “我已经在场景管理器中添加了场景”是什么意思?您是在谈论构建设置吗? (菜单File > Build Settings)。您确定您尝试加载的场景名称正是构建设置中的场景名称吗?
  • 只是关于样式的附注,您不需要在 C# 中为私有/受保护的变量添加下划线前缀。您可以使用this.sceneToLoad = sceneToLoad 而不是下划线语法。
  • @Hellium 是的,我在谈论构建设置,我要加载的所有场景都在具有正确名称的构建设置中,但仍然显示错误
  • @Hellium 是的,它也被检查了。我已经更新了问题,还添加了构建设置屏幕截图链接。请检查一下
  • @Hellium out of 9 Scenes 它只打开了 7 个场景,当尝试打开“Level1”和“Level4”时它显示错误。

标签: c# unity3d unity5


【解决方案1】:

您是否已将尝试加载的场景添加到构建设置“构建中的场景”部分? 这是使用场景管理器切换到项目中的任何场景所必需的。

【讨论】:

  • 是的,我已经在“Scene In Build”中添加了正确名称的所有场景
【解决方案2】:

我知道这个问题已经过时了,但我的回答对于某些遇到此问题的人来说可能仍然很有价值。您添加到构建中的场景应避免使用更多外来字符,例如德语变音符号“äöÜß”或类似字符。如果你也避免空白字符会更好 - 因为它们可能包含其他一些你怀疑的符号,因此会造成麻烦。尤其是当您复制和粘贴来命名它们时。所以最好坚持使用纯 ASCII 字母,并用连字符或下划线替换所有空格。

【讨论】:

    【解决方案3】:

    GO File-> 和 Build Setting 确保你拥有所有的场景。

    【讨论】:

      【解决方案4】:

      或者你可以通过他们的索引值使用它更简单的方法,而不是使用字符串

      SceneManager.LoadScene(0);

      然后通过打开构建设置窗口添加所有这些场景

      【讨论】:

        【解决方案5】:

        看起来您尝试加载名称为“SceneName”的场景。

        1. 检查您的函数参数值是否正确。
        2. 尝试使用索引而不是名称来加载有问题的场景。

        【讨论】:

          【解决方案6】:

          请检查您的函数“LoadScene”。

          我不确定确切的问题是什么,但从您的日志来看,问题出在“LoadScene”函数中。

          【讨论】:

            【解决方案7】:

            由于某种原因,我在 Build Settings 中正确添加了所有场景,但在编辑器中运行游戏时仍然抛出错误(为 android 构建时没有问题)。

            我刚刚从构建设置中删除了所有场景并重新添加了它们。之后就没有问题了。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-06-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多