![](/img/trans.png)
[英]How can I load a scene in unity with a spawn rate variable that increments every time the scene is loaded?
[英]How can I check if a scene is loaded don't load it again?
else 中的第 53 行:
SceneManager.LoadScene(0, LoadSceneMode.Additive);
我希望如果這個場景 0 已經加載了任何其他場景,在我的情況下只有兩個場景,但如果場景 0 主菜單已經加載,那么在單擊轉義鍵時不要再次加載它。
該腳本位於游戲場景 1
問題是,如果我在主菜單中點擊一個新游戲並且游戲場景已經加載,但在它刪除之前卸載了主菜單場景並且我點擊退出太快它會一次又一次地加載主菜單場景.
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Experimental.GlobalIllumination;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class BackToMainMenu : MonoBehaviour
{
public GameObject[] objsToDisable;
public AudioMixer audioMixer;
public static bool gameSceneLoaded;
public GameObject fadeImage;
public Light lights;
private float volumeLinearToDecibel;
private void Awake()
{
gameSceneLoaded = true;
}
// Start is called before the first frame update
void Start()
{
GetGameMusicVolume();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Time.timeScale == 0)
{
lights.enabled = true;
DisableEnableUiTexts(true);
SceneManager.UnloadSceneAsync(0);
if (fadeImage != null)
fadeImage.SetActive(true);
GetGameMusicVolume();
Cursor.visible = false;
Time.timeScale = 1;
}
else
{
Time.timeScale = 0;
lights.enabled = false;
MenuController.LoadSceneForSavedGame = false;
SceneManager.LoadScene(0, LoadSceneMode.Additive);
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
Cursor.visible = true;
}
}
}
private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
{
fadeImage = GameObject.FindWithTag("Game Scene Fader");
if (fadeImage != null)
fadeImage.SetActive(false);
audioMixer.SetFloat("gamemusicvolume", Mathf.Log(0.0001f) * 20);
DisableEnableUiTexts(false);
var pauseResumeMainMenuMode = FindInActiveObjectByName("MenuDefaultButtons_Canvas_Pause_Resume");
var newFreshGameMainMenuMode = FindInActiveObjectByName("MenuDefaultButtons_Canvas_NewFreshGame_SaveGame_Not_Exist");
newFreshGameMainMenuMode.SetActive(false);
pauseResumeMainMenuMode.SetActive(true);
SceneManager.sceneLoaded -= SceneManager_sceneLoaded;
}
private void DisableEnableUiTexts(bool enabled)
{
foreach (GameObject go in objsToDisable)
{
if (go.name == "Cameras Control")
{
foreach (Transform child in go.transform)
{
if (child.name == "Main Camera")
{
if (enabled == false)
{
child.GetComponent<Camera>().enabled = false;
}
else
{
child.GetComponent<Camera>().enabled = true;
}
}
}
}
else
{
go.SetActive(enabled);
}
}
}
private float LinearToDecibel(float linear)
{
float dB;
if (linear != 0)
dB = 20.0f * Mathf.Log10(linear);
else
dB = -144.0f;
return dB;
}
private void GetGameMusicVolume()
{
volumeLinearToDecibel = LinearToDecibel(PlayerPrefs.GetFloat("mainmenumusicvolume") / 100f);
audioMixer.SetFloat("gamemusicvolume", volumeLinearToDecibel);
}
GameObject FindInActiveObjectByName(string name)
{
Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
for (int i = 0; i < objs.Length; i++)
{
if (objs[i].hideFlags == HideFlags.None)
{
if (objs[i].name == name)
{
return objs[i].gameObject;
}
}
}
return null;
}
}
您可以檢查緩存的菜單對象是否已經存在打開菜單對象,如果沒有加載菜單場景。 就那么簡單。
更通用的方法是使用SceneManager.GetSceneByBuildIndex
如果在給定的構建索引處將場景添加到構建設置並且場景已加載,則此方法將返回有效的場景。 如果尚未加載,則 SceneManager 無法返回有效場景。
所以只需檢查IsValid
就像
if(SceneManager.GetSceneByBuildIndex(0).IsValid())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.