【问题标题】:Why when clicking on START GAME button when the game is running in build it's not working does nothing but it's working in the editor?为什么当游戏在构建中运行时单击“开始游戏”按钮时它不起作用,但它在编辑器中工作?
【发布时间】:2018-10-30 13:26:51
【问题描述】:

在编辑器的层次结构中,我有两个场景。 我在菜单场景中有一个按钮,名为:StartGameButton

当我在编辑器中运行游戏时单击按钮时,它正在运行。 我点击按钮,它在空间站场景中启用了真正的 player2。

但是在构建游戏并运行游戏 exe 后,当我单击按钮时它什么也没做。

这是 Hierarchy 和按钮 Inspector 中两个场景的截图:

您可以在屏幕截图中看到 player2 处于灰色启用假关闭状态。 在按钮中,我附加了脚本 LoadSceneOnClick:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadSceneOnClick : MonoBehaviour
{
    public void ActivatePlayer()
    {
        GameControl.player.SetActive(true);
        SceneManager.UnloadSceneAsync(0);
    }
}

我创建了一个静态类,以便能够在两个场景之间使用一个变量:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class GameControl
{
    public static GameObject player;
}

然后在空间站场景中,我有一个带有这个脚本的游戏对象:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    public GameObject player;

    // Use this for initialization
    void Start ()
    {
        GameControl.player = player;
    }

    // Update is called once per frame
    void Update () {

    }
}

因此,当我单击 StartGameButton 按钮时,它首先将 player2 更改为启用 true,然后卸载删除菜单场景。

GameControl.player.SetActive(true);
SceneManager.UnloadSceneAsync(0);

这在编辑器中运行良好。 但是在构建游戏并运行 exe 文件后,它在构建中不起作用我看到了菜单场景和按钮,但是当我点击按钮时它什么也没做。

而且我在构建中看到运行游戏时整个游戏看起来与编辑器中的不一样。

这是游戏在编辑器中的样子:

由于活动场景是空间站,而玩家 2 离开游戏,从主菜单开始,但使用天空盒和空间站场景的其他对象。

然后我点击按钮开始游戏: 它把我带到了空间站场景:

并移除已卸载的 Menu 场景:

但是当我运行构建 exe 文件时,它看起来像这样:

甚至与它在编辑器中的样子都不相近。

这是构建设置...窗口的屏幕截图: 我不知道为什么它在编辑器中工作正常,但在运行 exe 构建文件时却不行。

我在构建设置方面做错了吗?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我设法解决了它,现在它运行完美。

    我创建了另一个脚本并将其附加到菜单场景中的一个空游戏对象:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class LoadScenes : MonoBehaviour
    {
        // Use this for initialization
        void Start ()
        { 
            SceneManager.LoadScene(1, LoadSceneMode.Additive);
            StartCoroutine(WaitForSceneLoad(SceneManager.GetSceneByName("The Space Station")));        
        }
    
        public IEnumerator WaitForSceneLoad(Scene scene)
        {
            while (!scene.isLoaded)
            {
                yield return null;
            }
            SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(1));
        }
    }
    

    一旦加载了“空间站”场景,我就将其设置为 SetActive 场景。 它向我展示了带有一些“空间站”对象和天空盒的菜单(主菜单)。

    终于。

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多