Unity進階之ET網絡游戲開發框架 06-游客登錄


版權申明:

  • 本文原創首發於以下網站:
  1. 博客園『優夢創客』的空間:https://www.cnblogs.com/raymondking123
  2. 優夢創客的官方博客:https://91make.top
  3. 優夢創客的游戲講堂:https://91make.ke.qq.com
  4. 『優夢創客』的微信公眾號:umaketop
  • 您可以自由轉載,但必須加入完整的版權聲明!

概要:

  • ET4示例程序中的登錄流程大致為:實例化UILogin進行登錄->實例化UILobby進行匹配→加載Map場景進行游戲
  • 由於其流程對於新手有點復雜,這里介紹一種簡單的學習方式:
    • 直接創建一個StartUI
    • 點擊“游客登錄”按鈕執行登錄功能

創建一個StartUI:

  • 將啟動UI做成一個預制體,並命名為StartUI,UI界面如下:

  • 由於ET4采用ab包的方式加載資源,所以要:
    • 預制體設置Asset Label為:startui.unity3d
    • 用ET4的編輯器擴展:Tools→打包工具→選擇PC平台進行打包
    • 打包后的資源放在???
    • 不需要啟動服務端或文件服務器,ET4框架可以找到打包后的ab包
    • 編輯器狀態下會查找預制體而不從ab包加載
  • 用代碼加載該預制體:
namespace ETHotfix
{
	public static class MyStartUIFactory
	{
		public static UI Create()
		{
			try
			{
				ResourcesComponent rc = ETModel.Game.Scene.GetComponent<ResourcesComponent>();
				rc.LoadBundle(MyUIType.MyStartUI.StringToAB());
				GameObject goPrefab = rc.GetAsset(MyUIType.MyStartUI.StringToAB(), MyUIType.MyStartUI.ToString()) as GameObject;
				GameObject go = UnityEngine.Object.Instantiate(goPrefab);

				UI ui = ComponentFactory.Create<UI,string,GameObject>(MyUIType.MyStartUI.ToString(), go, false);

				ui.AddComponent<MyStartUIComponent>();

				return ui;
			}
			catch (Exception e)
			{
				Log.Error(e);
				return null;
			}
		}
	}
}

點擊“游客登錄”按鈕執行登錄功能

namespace ETHotfix
{
	[ObjectSystem]
	public class MyStartUIAwakeSystem : AwakeSystem<MyStartUIComponent>
	{
		public override void Awake(MyStartUIComponent self)
		{
			self.OnAwake();
		}
	}

	public class MyStartUIComponent : Component
	{
		GameObject goStartButton;

		public void OnAwake()
		{
			var rc = this.Parent.GameObject.GetComponent<ReferenceCollector>();
			goStartButton = rc.Get<GameObject>("StartButton");
			goStartButton.GetComponent<Button>().onClick.AddListener(OnStartClick);
		}

		private void OnStartClick()
		{
			// Coroutine()方法模擬將OnStartClickAsync()作為協程執行的效果(確保在單線程中執行)
			OnStartClickAsync().Coroutine();
		}

		private async ETVoid OnStartClickAsync()
		{
			// 處理游客登錄點擊
			try
			{
				// 1: 創建一個realmSession,並利用此session去call一個rpc消息(模型層session表示具體網絡參數和連接,熱更層負責通信邏輯,它調用模型層)
				ETModel.Session realmModelsession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
				Session realmHotfixSession = ComponentFactory.Create<Session, ETModel.Session>(realmModelsession);

				// 2:若登錄失敗,會收到異常;若成功,則會得到認證信息(模型層沒有C2R_Login的類定義,所以必須配套由熱更層的session去call)
				var r2cLogin = await realmHotfixSession.Call(new C2R_Login()
				{
					Account = Guid.NewGuid().ToString(),
					Password = "111111",
				}) as R2C_Login;

				// 3:再利用此認證信息中的網關服務器地址,去創建gateSession
				ETModel.Session gateModelsession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2cLogin.Address);
				Session gateHotfixSession = ComponentFactory.Create<Session, ETModel.Session>(gateModelsession);

				// 4:用gateSession去登錄gate服(傳入登錄key)
				var g2cLoginGate = await gateHotfixSession.Call(new C2G_LoginGate()
				{
					Key = r2cLogin.Key
				}) as G2C_LoginGate;

				// 5:登錄成功后,用返回的playerId,創建Player對象
				var p = ETModel.ComponentFactory.CreateWithId<Player>(g2cLoginGate.PlayerId);
				ETModel.Game.Scene.GetComponent<PlayerComponent>().MyPlayer = p;

				// 6:發送登錄完成事件
				Game.EventSystem.Run(MyEventType.MyLoginFinish);

				// 7: 保存熱更和模型層Session,以便將來直接通過SessionComponent.Instance單件訪問
				Game.Scene.AddComponent<SessionComponent>().Session = gateHotfixSession;
				ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateModelsession;
			}
			catch (Exception e)
			{
				Log.Error(e);
			}
		}
	}
}
  • EventSystem.Run引發本地事件,Session.Call|Send引發網絡消息

處理登錄成功消息:

namespace ETHotfix
{
	[Event(MyEventType.MyLoginFinish)]
	class MyLoginFinish : AEvent
	{
		public override void Run()
		{
			// 移除登錄UI
			UI uiLogin = Game.Scene.GetComponent<UIComponent>().Get(MyUIType.MyStartUI.ToString());
			uiLogin.GameObject.SetActive(false);

			// 加載游戲場景(也可以從ab包加載)
			SceneManager.LoadScene(1);
		}
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM