unity 利用协程加载场景

今天回头看之前的内容,LoadScene的时候都是直接跳转,看上去很呆,想加上一个通用一点的跳转加载页面。

 

利用协程解决,直接贴代码。

 

这个代码附带了创建角色的判断,懒得去删了,有用的只有协程和update的部分,因为加载太快,读条闪一下就没了,所以加上了

        loadingImage.fillAmount += 0.05f;
        if (loadingImage.fillAmount > 0.98f && operation != null)
            operation.allowSceneActivation = true;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Team7.Network;
using Team7;

public class LoadingPage : MonoBehaviour
{
    public GameObject White;
    public GameObject Black;

    public Text PlayerName;
    public GameObject RegisterPanel;
    public GameObject ErrorPanel;
    
    public Image loadingImage;
    private AsyncOperation operation=null;

    private NetworkManager networkMgr = NetworkManager.GetInstance();

    // Use this for initialization
    void Start()
    {
        SendMessageGetPlayerInfo();
    }

    // Update is called once per frame
    void Update()
    {
        loadingImage.fillAmount += 0.05f;
        if (loadingImage.fillAmount > 0.98f && operation != null)
            operation.allowSceneActivation = true;
    }

    IEnumerator processLoading()
    {
        operation = SceneManager.LoadSceneAsync(ScenePath.S_HomeScreen);

        operation.allowSceneActivation = false;

        yield return operation;
    }

    void SendMessageGetPlayerInfo()
    {
        var msg = new mmopb.m_role_read_s();
        msg.u_id = networkMgr.user.ID;
        msg.u_name = networkMgr.user.Account;

        networkMgr.m_CActor._messageContributionMgr.AddOncePlayerListener(nameof(mmopb.m_role_read_c), SendMessageGetPalyerInfoCb);
        networkMgr.m_CActor.tcpConnection.Send(ProtoBuf.ProtoHelper.EncodeWithName(msg));

    }

    void SendMessageGetPalyerInfoCb(object msg)
    {
        if (msg is mmopb.m_role_read_c)
        {
            var o = msg as mmopb.m_role_read_c;
            
            if (o.succ)
            {
                Debug.Log(o.role.id+o.role.name);
                networkMgr.player = new Team7.Data.Player(o.role.id, o.role.name, o.role.honorTitle);
                //有角色数据
                StartCoroutine(processLoading());
            }
            else
            {
                //无角色数据
                RegisterPanel.SetActive(true);
            }
        }
    }

    //确认创建
    public void OnComfirmButtonClicked()
    {
        var msg = new mmopb.m_role_create_s();
        msg.role = new mmopb.m_player();
        var playerName = "";
        var playerType = "";
        if (White.GetComponent().isOn == true)
        {
            playerType = "white";
        }
        else
        {
            playerType = "black";
        }
        playerName = PlayerName.text;

        Debug.Log(playerName + "  " + playerType);
        msg.role.id = networkMgr.user.ID;
        msg.role.name = playerName;
        msg.role.headshot = playerType;
        networkMgr.Send(msg);
        networkMgr.m_CActor._messageContributionMgr.AddOnceUserListener(nameof(mmopb.m_role_create_c), OnClickComfirmCb);

    }
    //创建监听
    private void OnClickComfirmCb(object msg)
    {
        Debug.Log("OnClick");
        if (msg is mmopb.m_role_create_c)
        {
            var o = msg as mmopb.m_role_create_c;
            if (o.succ)
            {
                StartCoroutine(processLoading());
            }
            else
            {
                var err_Panel = Instantiate(Resources.Load(ResourcesPath.errorPanel));
                var err_Display = err_Panel.GetComponent();
                StartCoroutine(err_Display.Initialize(o.succ.ToString(), o.error));
            }
        }
    }

    public void OnErrorComfirmClicked()
    {
        ErrorPanel.transform.SetAsFirstSibling();
    }
}

 

你可能感兴趣的:(unity 利用协程加载场景)