场景切换

场景A、B、C
C用作中转场景,A跳转到B,经过C场景
首先创建脚本GetScreenName获取到需要跳转的名字
public class GetScreenName {

public  string m_ScreenName;

}
A场景脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class AScreen : MonoBehaviour {

   /// <summary>
/// 想要跳转场景的名字
/// </summary>
public string name;
private Button m_btn;
// Use this for initialization
void Start () {
    m_btn=GetComponent<Button>();
    m_btn.onClick.AddListener(SkioScreen);
}
void SkioScreen()
{
    //跳转到中转场景
    SceneManager.LoadScene("C");
    //获取到最终需要跳转的场景名
    GetScreenName.Instance().m_ScreenName= name;//单例
}
private void OnDestroy()
{

    m_btn.onClick.RemoveListener(SkioScreen);
}

}
C场景脚本(中转场景)
在这里插入图片描述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SkipScreen : MonoBehaviour {

 private string m_ScreeName;
public Slider m_Slider;
public Text m_ShowValue;
private AsyncOperation m_Prog;
// Use this for initialization
void Start () {
    m_ScreeName = GetScreenName.Instance().m_ScreenName;//获取到的跳转场景//单例
    m_Prog = SceneManager.LoadSceneAsync(m_ScreeName);
    StartCoroutine(SkipScreenAsy());
}
IEnumerator SkipScreenAsy()
{
    m_Prog.allowSceneActivation = false;//如果加载完成,也不进入场景
    int toprogress = 0;
    int showProgress = 0;
    while(m_Prog.progress<0.9f)
    {
        toprogress = (int)(m_Prog.progress * 100);
        while(showProgress<toprogress)
        {
            showProgress++;
            GetSliderValue(showProgress);
        }
        yield return new WaitForEndOfFrame();
    }
    toprogress = 100;
    while(showProgress<toprogress)
    {
        showProgress++;
        GetSliderValue(showProgress);
        yield return new WaitForEndOfFrame();
    }
    m_Prog.allowSceneActivation = true;
}
private void GetSliderValue(int value)
{
    m_Slider.value = value;
    m_ShowValue.text = value + "%";
}
private void OnDestroy()
{
    m_Slider.value = 0;
    m_ShowValue.text = null;
}

}

发布了24 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qiao2037641855/article/details/105225964