切换场景与进度条加载的实现

首先,先写一个场景跳转管理器来管理跳转到哪个场景去

public class SceneCtrl : Singleton<SceneCtrl>
{
        //记录当前场景
	public SceneType sceneType
    {
        get;
        private set;
    }

    //加载到登录场景
    public void LoadToLogScene()
    {
        Application.LoadLevel("GameLoading");
        sceneType = SceneType.ToLogScene;
    }
    //加载到选择角色场景
    public void LoadToSelectScene()
    {
        Application.LoadLevel("GameLoading");
        sceneType = SceneType.ToSelectRole;
    }
    //加载到主城场景
    public void LoadToCityScene()
    {
        Application.LoadLevel("GameLoading");
        sceneType = SceneType.ToCityScene;
    }
    //加载到游戏场景
    public void LoadToGameScene()
    {
        Application.LoadLevel("GameLoading");
        sceneType = SceneType.ToGameScene;
    }
}

加载场景控制器通过协程切换场景

public class UILoadOnCtrl : MonoBehaviour
{
    //场景控制器
    [SerializeField]
    private UILoadOnView m_Loading;
    private AsyncOperation m_Async;
    //当前进度
    private float m_CruuentProgess;
    private void Start()
    {
        StartCoroutine(Loading());
        if (m_Loading==null)
        {
            m_Loading = GetComponent<UILoadOnView>();
        }
    }

    IEnumerator Loading()
    {
        string str = string.Empty;
        switch (SceneCtrl.Instance.sceneType)
        {
            case SceneType.ToLogScene:
                str = "GameLog";
                break;
            case SceneType.ToCityScene:
                str = "GameCity";
                break;
            case SceneType.ToGameScene:
                str = "GameScene_ShanGu";
                break;
            case SceneType.ToSelectRole:
                str = "Scene_SelectRole";
                break;
            default:
                break;
        }
        m_Async = Application.LoadLevelAsync(str);
        m_Async.allowSceneActivation = false;
        yield return m_Async;
    }

    private void Update()
    {
        int CurrentPro = 0;
        if (m_Async.progress<0.9f)
        {
            CurrentPro = Mathf.Clamp((int)m_Async.progress*100,1,100);
        }
        else
        {
            CurrentPro = 100;
        }
        if (m_CruuentProgess<CurrentPro)
        {
            m_CruuentProgess++;
        }
        else
        {
            m_Async.allowSceneActivation = true;
        }
        m_Loading.SetPregess(m_CruuentProgess*0.01f);
    }
}
加载场景进度条设置
public class UILoadOnView : UISceneBaseView
{
    //进度条
    [SerializeField]
    private Slider m_Slider;
    //进度数字
    [SerializeField]
    private Text m_Text;

    //设置进度条的值
    public void SetPregess(float value)
    {
        m_Slider.value = value;
        m_Text.text = string.Format("{0}%",(int)(value*100));
    }
}

猜你喜欢

转载自blog.csdn.net/kelly59/article/details/80200420