using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class RobSceneLoad : MonoBehaviour {
public Text text;
public Slider slider;
public Image fader;
public Animator animator;
string targetSceneName;
private void Start()
{
DontDestroyOnLoad(gameObject);
}
public string TargetSceneName
{
set
{
targetSceneName = value;
StartCoroutine(LoadScene(targetSceneName));
//暗
animator.Play("LoadAnimFadeIn");
}
}
AsyncOperation asyncOperation;
IEnumerator LoadScene(string sceneName)
{
yield return new WaitForSeconds(1.2f);
asyncOperation = SceneManager.LoadSceneAsync(sceneName);
while (!asyncOperation.isDone)
{
float p = asyncOperation.progress * 100 + 10f;
if (p > 100) p = 100;
text.text = p + "%";
slider.value = Mathf.Lerp(slider.value, asyncOperation.progress, Time.time);
yield return null;
}
while (asyncOperation.isDone)
{
slider.value= 1;
Invoke("FadeOut", 2);
Invoke("Destory", 6);
break;
}
}
void FadeOut()
{
//亮
animator.Play("LoadAnimFadeOut");
}
void Destory()
{
DestroyObject(gameObject);
}
}
//测试
using UnityEngine;
public class TestRobSceneLoad : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
GameObject go = Instantiate(Resources.Load<GameObject>("RobLoad/RobLoadCanvas"));
go.GetComponent<RobSceneLoad>().TargetSceneName = "New Scene 1";
}
}
}
按任意键跳转
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class RobSceneLoad : MonoBehaviour {
public Text text;
public Slider slider;
public Image fader;
public Animator animator;
string targetSceneName;
private void Start()
{
DontDestroyOnLoad(gameObject);
}
public string TargetSceneName
{
set
{
targetSceneName = value;
StartCoroutine(LoadScene(targetSceneName));
//暗
animator.Play("LoadAnimFadeIn");
}
}
AsyncOperation asyncOperation;
IEnumerator LoadScene(string sceneName)
{
yield return new WaitForSeconds(1.2f);
asyncOperation = SceneManager.LoadSceneAsync(sceneName);
//不让自动进入
asyncOperation.allowSceneActivation = false;
while (!asyncOperation.isDone)
{
text.text = slider.value.ToString("P1");
if (asyncOperation.progress < 0.9f)
slider.value = Mathf.Lerp(slider.value, asyncOperation.progress, Time.time);
else
{
slider.value = 1;
text.text = "输入任意键进入...";
if (Input.anyKeyDown)
{
asyncOperation.allowSceneActivation = true;
Invoke("FadeOut", 0.5f);
//Invoke("Destory", 6);
}
}
//离开携程
yield return null;
}
}
void FadeOut()
{
//亮
animator.Play("LoadAnimFadeOut");
}
void Destory()
{
DestroyObject(gameObject);
}
}
using System.Collections;using UnityEngine;using UnityEngine.UI;using UnityEngine.SceneManagement;public class RobSceneLoad : MonoBehaviour { public Text text; public Slider slider; public Image fader; public Animator animator; .