任务16:登录验证后进入大厅的实现

前面已经实现了登录,注册,这节我们要做到登录验证后进入大厅。服务端这节并不需要增加什么,我们通过登录请求验证通过拿到返回后,只需要在前端做出处理,就能进入大厅了。(大厅是干什么的呢?当然是匹配其它玩家准备进入三个人斗地主的房间了,那是后面章节了。)

所以我们要知道当前的开发在哪里:登录、注册界面,大厅,房间。

首先下载从这节需要的界面资源,提供了大厅的预制体,Poker图集。

LandLobby.zip (2.36KB)

Poker.unitypackage (342.09KB)

LandHelper中的Login方法最后加上,调用初始化大厅界面的事件

\Assets\Model\Landlords\LandUI\LandHelper.cs

           //加载透明界面 退出当前界面
            Game.EventSystem.Run(UIEventType.LandLoginFinish);

            //加载大厅界面
            Game.EventSystem.Run(UIEventType.LandInitLobby);

UIEventType 中增加初始化大厅界面事件方法

\Assets\Model\Landlords\LandUI\UIEventType.cs

    //初始化大厅界面事件方法
    [Event(UIEventType.LandInitLobby)]
    public class LandInitLobby_CreateLandLobby : AEvent
    {
        public override void Run()
        {
            Game.Scene.GetComponent<UIComponent>().Create(LandUIType.LandLobby);
        }
    }

增加大厅界面的UI组件与工厂方法

添加LandLobbyComponent

\Assets\Model\Landlords\LandUI\LandLobby\LandLobbyComponent.cs

using System;
using ETModel;
using UnityEngine;
using UnityEngine.UI;

namespace ETModel
{
    [ObjectSystem]
    public class LandLobbyComponentAwakeSystem : AwakeSystem<LandLobbyComponent>
    {
        public override void Awake(LandLobbyComponent self)
        {
            self.Awake();
        }
    }

    /// <summary>
    /// 大厅界面组件
    /// </summary>
    public class LandLobbyComponent : Component
    {
        //提示文本
        public Text prompt;
        //玩家名称
        private Text name;

        public bool isMatching;

        public async void Awake()
        {
            ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();

            prompt = rc.Get<GameObject>("Prompt").GetComponent<Text>();
            name = rc.Get<GameObject>("Name").GetComponent<Text>();

            //添加进入房间匹配事件
            //...

            //添加新的匹配目标
            //...

            //获取玩家数据
            //...
            
            //显示用户名和用户等级
            name.text = GetUserInfo_Ack.NickName;
        }
        
    }
}

添加LandLobbyFactory

\Assets\Model\Landlords\LandUI\LandLobby\LandLobbyFactory.cs

using ETModel;
using System;
using UnityEngine;

namespace ETModel
{
    [UIFactory(LandUIType.LandLobby)]
    public class LandLobbyFactory : IUIFactory
    {
        public UI Create(Scene scene, string type, GameObject parent)
        {
            try
            {
                //加载AB包
                ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
                resourcesComponent.LoadBundle($"{type}.unity3d");

                //加载大厅界面预设并生成实例
                GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset($"{type}.unity3d", $"{type}");
                GameObject lobby = UnityEngine.Object.Instantiate(bundleGameObject);

                //设置UI层级,只有UI摄像机可以渲染
                lobby.layer = LayerMask.NameToLayer(LayerNames.UI);
                UI ui = ComponentFactory.Create<UI, GameObject>(lobby);

                ui.AddComponent<LandLobbyComponent>();
                return ui;
            }
            catch (Exception e)
            {
                Log.Error(e);
                return null;
            }
        }

        public void Remove(string type)
        {
            ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"{type}.unity3d");
        }
    }
}

最后我们定义一下大厅相关的LandUIType,UIEventType

\Assets\Model\Landlords\LandUI\UIEventType.cs

public static partial class LandUIType
    {
        public const string LandLogin = "LandLogin";
        public const string LandLobby = "LandLobby";
    }

    public static partial class UIEventType
    {
        //斗地主EventIdType
        public const string LandInitSceneStart = "LandInitSceneStart";
        public const string LandLoginFinish = "LandLoginFinish";
        public const string LandInitLobby = "LandInitLobby";
        
    }

运行前面的服务端项目,运行今天完成的前端项目,试一下效果吧。

posted @ 2023-02-02 10:18  Domefy  阅读(32)  评论(0)    收藏  举报