在编辑器的层次结构中,我有两个场景。在菜单场景中,我有一个名为StartGameButton的按钮
当我点击按钮时,游戏运行在编辑器中,它是工作的。我点击按钮,它打开启用,真正的player2在空间站的场景。
但是在构建游戏并运行游戏exe之后,当我单击按钮时,它什么也不做。
这是层次结构和按钮检查器中两个场景的屏幕截图:

您可以在屏幕截图中看到player2是以灰色启用的false off。在按钮中,我附加了脚本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文件之后,它就不能工作了,我看到了菜单场景和按钮,但是当我点击按钮时,它什么也不做。
我看到,在构建时,运行游戏,整个游戏看起来不一样,在编辑器。
在编辑器中,这个游戏就是这样的:

由于活动场景是空间站和player2离开游戏,从主菜单开始,而是用天窗和空间站场景的其他物体开始。
然后我点击按钮开始游戏,它带我到空间站的场景:
并删除卸载菜单场景:

但是,当我运行build文件时,它看起来如下所示:

甚至和编辑器里的样子都不太接近。
这是构建设置的截图..。窗口:我不知道为什么它在编辑器中正常工作,但在运行exe构建的文件时却不工作。
我对构建设置做错了什么吗?

发布于 2018-05-21 05:49:33
我设法解决了它,现在它工作得很好。
我创建了另一个脚本并将其附加到菜单场景中的一个空GameObject上:
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场景。它给我看了菜单(主菜单)和一些“空间站”物体和天窗。
终于来了。
https://stackoverflow.com/questions/50439530
复制相似问题