【问题标题】:Why doesnt my scene finish loading Unity?为什么我的场景没有完成加载 Unity?
【发布时间】:2019-11-05 10:44:02
【问题描述】:

您好,我目前正在 Unity 中开发游戏,但由于某种原因,我遇到了一个小问题,我正在创建的第四个场景(第 3 级)卡住并且没有完成加载,从而导致我的游戏无法从场景 2(第 2 级)过渡2) 到场景 3(Level 3)。我有一个管理这些转换的场景管理器脚本,它适用于所有其他场景转换,除了上面解释的情况。有谁知道我做错了什么?下面您将看到负责处理场景转换的代码:

这是我负责附加加载和卸载场景的场景管理器脚本:

public static class MySceneManager
{

    private static int lastLoadedScene = 0;

    public static void LoadScene(int index, MonoBehaviour caller)
    {
        ObjectPooler objP = new ObjectPooler();
        objP.ReleaseAll();
        caller.StartCoroutine(loadNextScene(index));
    }

    private static IEnumerator loadNextScene(int index)
    {
        var _async = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);

        _async.allowSceneActivation = false;
        while (_async.progress < 0.9f)
        {

            yield return null;
        }

        _async.allowSceneActivation = true;

        while (!_async.isDone)
        {
            yield return null;
        }

        var newScene = SceneManager.GetSceneByBuildIndex(index);

        if (!newScene.IsValid()) yield break;


        SceneManager.SetActiveScene(newScene);


        if (lastLoadedScene >= 0) SceneManager.UnloadSceneAsync(lastLoadedScene);


        lastLoadedScene = index;

    }
}

这是我调用场景转换的脚本:

public class HeartScript : MonoBehaviour
{
    int HeartEnter = 1;
    void Start()
    {
        DontDestroyOnLoad(this.gameObject);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Scene scene = SceneManager.GetActiveScene();

        if (other.gameObject.CompareTag("White Ball"))
        {
            if (HeartIndicator.numOfHearts < 3)
            {
                Debug.Log("Entered COLLIDER");
                HeartIndicator.numOfHearts += 1;
            }

            if(scene.name == "Level 2" && HeartEnter == 1) 
            {
                MySceneManager.LoadScene(3, this);
                HeartEnter++;
            }

            this.gameObject.SetActive(false);

        }
    }
}

【问题讨论】:

  • 可能是因为您将 gameobject.active 设置为 false,然后它会尝试在其上启动协同程序。
  • @BugFinder 谢谢他似乎成功了
  • 在添加之前检查标签描述。 game-engine 标签仅适用于询问有关编写自己的引擎的问题,而不是在使用引擎时。如果可以的话,我也会从标题中删除Unity,因为它是一个标签并且已经在标签中加了标签,但是你的标题被 Stack Overflow 认为是坏的(可能是重复的),这表明你没有看可能重复。

标签: c# unity3d game-development scene-manager


【解决方案1】:

这可能是由于:

  1. 场景 3 未添加到“构建中的场景”中。

  2. 或者游戏对象持有协程脚本没有激活。

【讨论】:

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