录制场景信息:RecordSceneInfo
挂在Home1、Home2场景
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RecordSceneInfo : MonoBehaviour
{
public static RecordSceneInfo Instance { get; private set; }
public string sceneName;
private void Awake()
{
if (null == Instance)
{
Instance = this;
}
}
public void JumpScene(string sceneName, string targetScene = "loading")
{
this.sceneName = sceneName;
SceneManager.LoadScene(targetScene);
}
}
加载:Loading
挂在Loading场景
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Loading : MonoBehaviour
{
//public Text SliderText;
//public Slider LoadingSlider;
//异步对象
private AsyncOperation m_Async;
void Start()
{
Invoke("StartLoad", 1);
}
private void StartLoad()
{
StartCoroutine("LoadScene");
}
private IEnumerator LoadScene()
{
//异步读取场景。
m_Async = SceneManager.LoadSceneAsync(RecordSceneInfo.Instance.sceneName);
m_Async.allowSceneActivation = false;
//读取完毕后返回, 系统会自动进入C场景
yield return m_Async;
}
private void Update()
{
if (m_Async == null)
return;
//LoadingSlider.value = m_Async.progress;
if (m_Async.progress >= 0.88f)
{
m_Async.allowSceneActivation = true;
}
//SliderText.text = ((int)(LoadingSlider.value * 100)).ToString() + "%";
}
}
定义:Define
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Define
{
public enum SceneName
{
Home1 = 0,
Home2 = 1,
}
}
调用方法
public void UserOnClick()
{
RecordSceneInfo.Instance.sceneName = Define.SceneName.Home1.ToString();
SceneManager.LoadScene("loading");
}