Unity에서 장면 미리로드
저는 메뉴와 메인 게임의 두 가지 장면이있는 게임이 있습니다. googleplay에 게임을 업로드했습니다. 하지만 게임을 열고 메뉴 씬에서 플레이 버튼을 클릭하면 다음 씬 (메인 게임)을 불러오는 데 약 4 ~ 5 초가 걸립니다. 로딩 지연이 없도록 메뉴와 함께 메인 게임을로드 할 수있는 방법이 있습니까?
편집하다
게임이 시작될 때 LoadSceneAsync 함수가 호출되고 allowSceneActivation이 false로 설정되도록 MainMenu.cs 파일에 코드를 추가했습니다. "재생"버튼을 누르면 활성화가 true로 설정됩니다. 게임이 처음로드 될 때 여전히 약간의 지연이 있지만 플레이어가 죽고 메인 메뉴로 이동하여 플레이를 다시 누르면 지연이 없습니다.
AsyncOperation async;
void Start()
{
async = SceneManager.LoadSceneAsync(1);
async.allowSceneActivation = false;
}
public void PlayBtnPressed()
{
async.allowSceneActivation = true;
AudioManager.audiomanager.Play("PlayButton");
}
답변
장면 로딩
기본
새 프로젝트에서 이것을 테스트했습니다. 아이디어는 Return버튼을 눌렀을 때 씬 프리로드를 시작 AsyncOperation하고 그 작업을 저장 하고 씬 활성화를 허용하지 않고 준비가되는 즉시 씬을로드해야 할 때 허용하는 것입니다.
메모리에로드 될 것으로 예상합니다. RAM이 제한된 경우에는 두 장면이 모두 언로드 될 때까지 메모리 공간을 차지하므로주의해야합니다.
낳다:
- 새 프로젝트를 만듭니다.
- 2 개의 장면 만들기 :
- "메뉴"
- "메인 게임"
- 두 장면을
Build Settings. - "메뉴"장면을 엽니 다.
- 만들기
GameObject및 추가PreloadSceneInUnity그것을 구성 요소입니다. - 플레이 모드로 들어갑니다.
Return계층 구조 창에서 "메인 게임 (로드 중)"을 누르고 보십시오.- 눌러
Space- "메인 게임"장면이 열립니다.
암호
Git : Unity에서 장면 미리로드
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PreloadSceneInUnity : MonoBehaviour
{
[SerializeField] private string _sceneName = "maingame";
public string _SceneName => this._sceneName;
private AsyncOperation _asyncOperation;
private IEnumerator LoadSceneAsyncProcess(string sceneName)
{
// Begin to load the Scene you have specified.
this._asyncOperation = SceneManager.LoadSceneAsync(sceneName);
// Don't let the Scene activate until you allow it to.
this._asyncOperation.allowSceneActivation = false;
while (!this._asyncOperation.isDone)
{
Debug.Log($"[scene]:{sceneName} [load progress]: {this._asyncOperation.progress}");
yield return null;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Return) && this._asyncOperation == null)
{
Debug.Log("Started Scene Preloading");
// Start scene preloading.
this.StartCoroutine(this.LoadSceneAsyncProcess(sceneName: this._sceneName));
}
// Press the space key to activate the Scene.
if (Input.GetKeyDown(KeyCode.Space) && this._asyncOperation != null)
{
Debug.Log("Allowed Scene Activation");
this._asyncOperation.allowSceneActivation = true;
}
}
}
많은
씬 로딩이 작동하는 방식이 만족스럽지 않다면 Unity가 제공하는 패키지를 UnityEngine.SceneManagement살펴 보는 것이 좋습니다. Addressables씬 로딩도 지원하지만 들어가는 것이 훨씬 더 복잡하고 학습이 있습니다. 씬 로딩 전에 몇 가지 더 많은 것을 배워야하는 곡선.
코 루틴을 사용하여 SceneManager.LoadSceneAsync ()를 사용하여 비동기식으로 다음 장면을로드 할 수 있습니다. 메뉴를 표시하기 전에로드되는 것을보고 싶다면 스플래시 /로드 화면에서이 전략을 사용할 수 있습니다.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Example : MonoBehaviour
{
void Update()
{
// Press the space key to start coroutine
if (Input.GetKeyDown(KeyCode.Space))
{
// Use a coroutine to load the Scene in the background
StartCoroutine(LoadYourAsyncScene());
}
}
IEnumerator LoadYourAsyncScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Scene2");
// yield to other processes until the scene is loaded
while (!asyncLoad.isDone)
{
yield return null;
}
// Do something here like enabling the play button or closing the splash/loading screen
}
}
코드 예제 : https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
편집 : 주석에 표시된대로 장면이로드되는시기를 제어하려면 위의 AsyncOperation 개체에서 allowSceneActivation 플래그를 사용할 수 있습니다. asyncLoad.allowSceneActivation = false; 다음 장면을로드하려는 경우 : asyncLoad.allowSceneActivation = true;