Netty游戲服務器之五Unity3d登陸消息


今天我們來講客戶端Unity和服務器收發消息的具體過程。

 

首先,我們要在unity上搭建登陸界面的UI,這里呢,我用的是NGUI插件。

 

相信做過unity3d前端的都對這個非常的熟悉,最近官方的UGUI也非常的火,但是畢竟是剛出來的,有些功能還不夠完善。我期待UGUI干翻NGUI的那天。

 

算了,不意淫了。這里我不詳細的講怎么搭建UI,我只是走個過場,講下重點的。

 

這里有三個重要的gameobject,分別是userInput(賬號輸入框),passInput(密碼輸入框),加上BtnLogin(開始游戲按鈕),其他自己搭建很快的

 

這里我講下思路,就是輸入賬號和密碼后,點擊開始游戲按鈕,客戶端就會發送一條消息給服務器,

 

然后服務器收到消息,讀取里面的賬號密碼去數據庫驗證是否存在該用戶或密碼是否正確,

 

如果正確就發送成功的消息給客戶端,客戶端就可以加載其他場景了。(這里只是我個人的思路,可能有很多不好的地方,需要你們大神的指正)

 

ok,我們來寫發送消息額機制,不得說ngui的事件監聽寫的太牛了。而且我最近向一個大神學了一個ui的底層框架,用起來就更加得心應手了。

public class Protocol{
	public const int  TYPE_LOGIN = 0;

	public const int TYPE_USER = 1;

	public const int TYPE_WIZARD = 2;
	
	public const int TYPE_BATTLE = 3;
}

  

public class LoginProtocol {
	public const int Area_LoginRequest = 0; // 請求登陸
	public const int Area_LoginResponse = 1; //登陸響應

	public const int Login_InvalidMessage = 0;//無效消息
	public const int Login_InvalidUsername = 1;//無效用戶名
	public const int Login_InvalidPassword = 2;//密碼錯誤
	
	public const int Login_Succeed = 10;//登陸成功
}

  

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LoginUI : SceneBase {

	#region 界面加載相關
	protected override void OnInitFront()
	{
		base.OnInitFront();
		_type = SceneType.LoginUI;
	}
	protected override void OnInitSkinFront()
	{
		base.OnInitSkinFront();
		SetMainSkinPath("UI/Login/LoginUI");
	}
	protected override void OnInitDone()
	{
		base.OnInitDone();
		InitData();
	}
	protected override void OnDestroyDone()
	{
		base.OnDestroyDone();
		
	}
	protected override void OnDestroyFront()
	{
		base.OnDestroyFront();
	}
	protected override void OnClick(GameObject target)
	{
		base.OnClick(target);
		ButtonClick(target);
	}
	
	public override void OnInit(params object[] sceneArgs)
	{
		base.OnInit(sceneArgs);
	}
	public override void OnShowed()
	{
		base.OnShowed();
		
	}
	#endregion
	
	#region 數據定義
	
	private UIInput UserInput;
	private UIInput PassInput;
	
	#endregion
	
	#region ui邏輯
	
	void InitData()
	{
		UserInput = skinTransform.Find("UserInput").GetComponent<UIInput>();//獲得相關組件
		PassInput = skinTransform.Find("PassInput").GetComponent<UIInput>();
		if (PlayerPrefs.GetString("Username") != null && PlayerPrefs.GetString("Username") != "")
		{
			UserInput.value = PlayerPrefs.GetString("Username");
		}
	}
	
	void ButtonClick(GameObject click)//按鈕監聽
	{
		if(click.name.Equals("BtnLogin"))//如果是登錄按鈕
		{
			if (UserInput.value == "")//賬號為空什么都不做
			{
				return ;
			}
			else if (PassInput.value == "")//密碼為空什么都不做
			{
				return ;
			}
			else
			{
				SocketModel LoginRequset = new SocketModel();
				LoginRequset.SetType(Protocol.TYPE_LOGIN);//消息的類型為Login
				LoginRequset.SetArea(LoginProtocol.Area_LoginRequest);
				LoginRequset.SetCommand(0);
				List<string> message = new List<string>();//這里存的是用戶的賬號密碼
				message.Add(UserInput.value);
				message.Add(PassInput.value);
				LoginRequset.SetMessage(message);
				MainClient.instance.SendMsg(LoginRequset);//發送這條消息給服務器
			}
		}
	}
	#endregion
}

  

 


免責聲明!

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



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