ET框架——demo與自定義登錄

目錄

demo

自定義登錄之客戶端

1.自定義協議

2.修改進入函數

3.創建UI面板

5.向服務器發送登錄消息

自定義登錄之服務器


demo

框架下載5.0:https://github.com/egametang/ET

下載完成後,打開 ET\Server\server.sln

打開後按照下面的鏈接修改文件,運行的服務器即可打印日誌

關於unity ET框架Nlog的控制檯Log不出來的問題

選擇Server.App,然後【開始執行(不調試)】

打開 ET\Unity\Assets\Scenes\Init.unity

直接運行即可進入demo

 

自定義登錄之客戶端

1.自定義協議

每一個命令都有2個協議,IRequest向目標發送請求,IResponse請求完成後的迴應

例如,向服務器發送登錄信息,那麼服務器就應該告訴客戶端登錄是否成功

打開 ET\Proto\HotfixMessage.proto

可以看到裏面有很多協議,具體語法自己去官網看,我們添加2個協議

message CS_Login //IRequest
{
	int32 RpcId = 90;
	string User = 1;
	string Password = 2;
}

message SC_Login // IResponse
{
	int32 RpcId = 90;
	int32 Error = 91;
	string Message = 92;
	string Address = 1;
	int64 Key = 2;
}

然後打開Unity,執行菜單 Tools/Proto2CS

就會自動生成C#協議代碼,觀察下面2個文件

Assets\Hotfix\Module\Message\HotfixMessage.cs

Assets\Hotfix\Module\Message\HotfixOpcode.cs

服務器直接引用這些文件,不需要複製到服務器

 

2.修改進入函數

打開 Assets\Hotfix\Base\Event\EventIdType.cs 添加變量

public const string InitGameScene = "InitGameScene";

打開 Assets\Hotfix\Init.cs 修改爲

//Game.EventSystem.Run(EventIdType.InitSceneStart);
Game.EventSystem.Run(EventIdType.InitGameScene);

創建 Assets\Hotfix\Module\Demo2\InitGameScene.cs

namespace ETHotfix
{
    [Event(EventIdType.InitGameScene)]
    public class InitGameScene : AEvent
    {
        public override void Run()
        {
            //創建UI面板
            UI ui = LoginFactory.Create();
            //把UI面板添加到UI組件
            Game.Scene.GetComponent<UIComponent>().Add(ui);
        }
    }
}

即可從InitGameScene的Run進入

 

3.創建UI面板

直接使用 Assets\Bundles\UI\UILogin.prefab 登錄面板

創建 Assets\Hotfix\Module\Demo2\LoginFactory.cs

namespace ETHotfix
{
    public class LoginFactory
    {
        public static UI Create()
        {
            try
            {
                //獲取資源管理組件
                //裏面維護2個字典,一個緩存字典,一個包信息字典
                ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
                //加載ab包
                resourcesComponent.LoadBundle(UIType.UILogin.StringToAB());
                //讀取ab包的資源
                GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
                //創建對象
                GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject);
                //創建UI組件,把上面創建的對象的層級改爲UI層級
                UI ui = ComponentFactory.Create<UI, string, GameObject>(UIType.UILogin, gameObject, false);
                //添加自定義登錄組件
                ui.AddComponent<LoginComponent>();
                return ui;
            }
            catch(Exception e)
            {
                Log.Error(e);
                return null;
            }
           
        }
    }
}

4.創建登錄組件

創建 Assets\Hotfix\Module\Demo2\LoginComponent.cs

namespace ETHotfix
{
    //添加特性後會反射調用該類
    [ObjectSystem]
    public class LoginComponentSystem : AwakeSystem<LoginComponent>
    {
        public override void Awake(LoginComponent self)
        {
            //調用登錄組件的Awake函數
            self.Awake();
        }
    }

    public class LoginComponent : Component
    {
        private InputField user;
        private InputField password;
        private GameObject loginButton;

        public void Awake()
        {
            //獲取面板上的引用組件
            ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();
             loginButton = rc.Get<GameObject>("LoginBtn");
            loginButton.GetComponent<Button>().onClick.Add(OnLogin);
            this.user = rc.Get<GameObject>("Account").GetComponent<InputField>();
            this.password = rc.Get<GameObject>("Password").GetComponent<InputField>();
        }

        void OnLogin()
        {
            //向服務器發送登錄消息
            MyLoginHelper.OnLoginAsync(user.text,password.text).Coroutine();
        }
    }
}

5.向服務器發送登錄消息

創建 Assets\Hotfix\Module\Demo2\MyLoginHelper.cs

namespace ETHotfix
{
    public class MyLoginHelper
    {
        public static async ETVoid OnLoginAsync(string user,string password)
        {
            //從網絡監聽組件取出一個 session
            ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
            //創建 session,並調用它的Awake
            Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
            //向服務器發送CS_Login協議消息,並對返回的SC_Login協議消息進行處理
            SC_Login r2CLogin = (SC_Login)await realmSession.Call(new CS_Login() { User = user, Password = password });
            //釋放 session
            realmSession.Dispose();
            Log.Debug(r2CLogin.Key.ToString());
        }
    }
}

 

自定義登錄之服務器

創建 Sever.Hotfix/Module/Demo/TestLoginHandle.cs

using System;
using System.Net;
using ETModel;

namespace ETHotfix
{
    //處理realm類型的消息
    [MessageHandler(AppType.Realm)]
    //帶有返回的RPC調用
    class TestLoginHandler : AMRpcHandler<CS_Login, SC_Login>
    {
        protected override async ETTask Run(Session session, CS_Login request, SC_Login response, Action reply)
        {
            // 隨機分配一個Gate
            StartConfig config = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
            IPEndPoint innerAddress = config.GetComponent<InnerConfig>().IPEndPoint;
            //創建一個內部 gate
            Session gateSession = Game.Scene.GetComponent<NetInnerComponent>().Get(innerAddress);
            // 向gate請求一個key,客戶端可以拿着這個key連接gate
            G2R_GetLoginKey g2RGetLoginKey = (G2R_GetLoginKey)await gateSession.Call(new R2G_GetLoginKey() { Account = request.User });

            //返回給客戶端的地址,客戶端用該地址建立 session
            string outerAddress = config.GetComponent<OuterConfig>().Address2;

            response.Address = outerAddress;
            response.Key = g2RGetLoginKey.Key;
            
            //返回 response 給客戶端
            reply();
        }
    }
}

右鍵 Sever.Hotfix  》 重新生成

啓動服務器 》 運行Unity。。。。。巴拉巴拉。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章