我正在编写一个管理器脚本,该脚本在List中存储来自Object名称数组的字符串集合。这些对象实际上是我在团结编辑器中拖入公共数组的游戏场景。从那里我在GetRandomScene()中选择其中一个字符串,然后通过LoadScene()将字符串名称传递给SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
。
此脚本附加到空白场景(构建设置中的第一个场景),目的是加载我拖动到Object [] forestScenes数组中的一个场景。这在编辑器中工作正常,但是当我构建为独立应用程序时它不起作用。为了正常运行,我需要修改什么?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class ForestGameManager : MonoBehaviour
{
public static ForestGameManager fgm = null; // create singleton
[Header("Network Prefab")]
public GameObject serverNetworkManagerPrefab;
[Header("Forest Scenes")]
public Object[] forestScenes;
private List<string> forestSceneNames;
private int randomSceneIndex;
private string randomSceneName;
[Header("Forest Animal Management")]
public int maxAnimalCount = 4;
public List<ForestAnimal> forestAnimals;
private void Awake()
{
StartCoroutine(InitForestGameManager());
StartCoroutine(InitServerNetworking());
}
private void Start()
{
forestAnimals = new List<ForestAnimal>();
forestSceneNames = new List<string>();
StartCoroutine(InitSceneNameList());
StartCoroutine(GetRandomScene());
}
private void LateUpdate()
{
// Destroy oldest animal gameobject based on maxAnimalCount
if (forestAnimals.Count >= maxAnimalCount)
{
GameObject toDestroy = forestAnimals[0].forestAnimalGO;
forestAnimals.RemoveAt(0);
Destroy(toDestroy);
}
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// If all forest scenes have been used, re-initialize scene name list and pick the next random scene
if (forestSceneNames != null)
{
if (forestSceneNames.Count == 0)
{
StartCoroutine(InitSceneNameList());
StartCoroutine(GetRandomScene());
}
}
// Start scene change countdown timer
}
private IEnumerator InitSceneNameList()
{
// Initialize scene name list
for (int i = 0; i < forestScenes.Length; i++)
{
forestSceneNames.Add(forestScenes[i].name);
// Debug.Log("Scene " + forestSceneNames[i].ToString() + " added to string list.");
}
yield return new WaitForEndOfFrame();
}
private IEnumerator InitForestGameManager()
{
if (fgm == null)
fgm = this;
else if (fgm != null)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
yield return new WaitForEndOfFrame();
}
private IEnumerator InitServerNetworking()
{
Instantiate(serverNetworkManagerPrefab);
yield return new WaitForEndOfFrame();
}
private IEnumerator GetRandomScene()
{
// Get scene name from random pick
randomSceneName = forestSceneNames[Random.Range(0, forestSceneNames.Count)];
// Remove selected scene from list
forestSceneNames.Remove(randomSceneName);
yield return new WaitForEndOfFrame();
// Load selected scene
StartCoroutine(LoadScene(randomSceneName, 1.0f));
//Debug.Log("randomSceneName is " + randomSceneName);
for (int i = 0; i < forestSceneNames.Count; i++)
{
//Debug.Log("Scene " + forestSceneNames[i].ToString() + " is still left in the list");
}
}
private IEnumerator LoadScene(string sceneName, float loadDelay)
{
yield return new WaitForSeconds(loadDelay);
SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
}
}