我知道以前here曾问过这个问题,但这是行不通的。它将GameObject成功移动到另一个场景,但没有关闭前一个场景。这是screenshot。
这是项目Expanded Screen Shot的扩展屏幕截图
这是脚本,但是在脚本中您还会找到“幻灯片显示”菜单的代码,该菜单会将我的模型显示为菜单
在功能enableScene()中,在第1行中,我试图关闭上一个场景,但是它不起作用
public class PizzaScript : MonoBehaviour {
public Text header;
public List<GameObject> createObjects = new List<GameObject>();
private static int slideIndex = 1;
private GameObject instance;
private Vector3 temp = new Vector3(0.34f, 0.074f, 0);
private AsyncOperation sceneAsync;
public GameObject Pizza;
public GameObject Cube;
public GameObject Sphere;
public GameObject CocaCola;
// Use this for initialization
void Start()
{
object[] subListObjects = { Pizza, Cube, Sphere, CocaCola };
foreach(GameObject list in subListObjects )
{
GameObject lo = (GameObject)list;
createObjects.Add(lo);
}
showPrefabs(slideIndex);
StartCoroutine(loadScene(2));
}
IEnumerator loadScene(int index)
{
AsyncOperation scene = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
scene.allowSceneActivation = false;
sceneAsync = scene;
//Wait until we are done loading the scene
while (scene.progress < 0.9f)
{
Debug.Log("Loading scene " + " [][] Progress: " + scene.progress);
yield return null;
}
Debug.Log("Progress Completed.............................");
}
public void OnFinishedLoadingAllScene()
{
Debug.Log("Done Loading Scene");
enableScene(2);
Debug.Log("Scene Activated!");
}
private void enableScene(int index)
{
SceneManager.UnloadSceneAsync(1);
//Activate the Scene
sceneAsync.allowSceneActivation = true;
Scene sceneToLoad = SceneManager.GetSceneByBuildIndex(index);
if (sceneToLoad.IsValid())
{
Debug.Log("Scene is Valid");
SceneManager.MoveGameObjectToScene(instance, sceneToLoad);
SceneManager.SetActiveScene(sceneToLoad);
}
}
// Update is called once per fram
public void OnClickRightArrow()
{
plusPrefabs(1);
}
public void OnClickLeftArrow()
{
plusPrefabs(-1);
}
public void plusPrefabs(int n)
{
if(n == 1 || n == -1)
{
Destroy(instance,0.01f);
}
showPrefabs(slideIndex += n);
}
public void showPrefabs(int n)
{
var x = createObjects.Count;
if (n > createObjects.Count)
{
slideIndex = 1;
}
if (n < 1)
{
slideIndex = createObjects.Count;
}
if (slideIndex == 1)
{
header.text = slideIndex.ToString();
instance = Instantiate(createObjects[0], temp, Quaternion.Euler(-90,0,0));
instance.transform.localScale = new Vector3(20, 20, 20);
}
else if(slideIndex == 2)
{
header.text = slideIndex.ToString();
instance = Instantiate(createObjects[1], temp, transform.rotation);
instance.transform.localScale = new Vector3(10, 10, 10);
}
else if (slideIndex == 3)
{
header.text = slideIndex.ToString();
instance = Instantiate(createObjects[2], temp, transform.rotation);
instance.transform.localScale = new Vector3(10, 10, 10);
}
else if (slideIndex == 4)
{
header.text = slideIndex.ToString();
instance = Instantiate(createObjects[3], temp, Quaternion.Euler(-90,0,0));
instance.transform.localScale = new Vector3(120, 120, 120);
}
}
}
答案 0 :(得分:-1)
在加载时将一个游戏对象持久保存到另一个场景中非常容易,只需将脚本附加到所需的游戏对象上即可。
$url2
DontDestroyOnLoad的工作方式如下:
使对象目标在加载时不会自动销毁 新场景。
加载新关卡时,场景中的所有对象均被破坏,然后 新级别的对象已加载。为了保存一个 关卡加载期间,对象调用DontDestroyOnLoad。如果 对象是组件或游戏对象,然后是其整个变换 层次结构也不会被破坏。