using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.SceneManagement;
|
using KTGMGemClient;
|
using UnityEngine.UI;
|
using LitJson;
|
using UnityEngine.Networking;
|
using Protobuf;
|
using Google.Protobuf;
|
|
public class LoginUI : MonoBehaviour
|
{
|
private AsyncOperation async = null;
|
bool isLogining;
|
private JsonData loginData;
|
|
// Start is called before the first frame update
|
void Start()
|
{
|
GA_SDK_Manager.Ins.SDKInit(ChannelID.Gm.ToString());
|
GA_SDK_Manager.Ins.Statistics(1);//成功加载登陆界面的人数
|
isLogining = false;
|
|
transform.Find("Panel/Button").GetComponent<Button>().onClick.AddListener(OnClickLoginBtn);
|
|
if (Application.platform == RuntimePlatform.Android)
|
{
|
//GetetDeviceIMEI();//获取安卓手机IMEI
|
imei0 = GA_SDK_Manager.Ins.GetDeviceId();//使用设备ID
|
}
|
else if (Application.platform == RuntimePlatform.WindowsEditor)
|
{
|
imei0 = "MyTestGemBattle123";
|
}
|
|
|
|
}
|
|
private void OnEnable()
|
{
|
SocketEvent.Ins.Add<IMessage>((int)Opcode.LoginS2C, LoginS2C);
|
|
}
|
|
private void OnDestroy()
|
{
|
SocketEvent.Ins.Remove<IMessage>((int)Opcode.LoginS2C, LoginS2C);
|
|
}
|
|
#region 获得安卓手机上的IMEI号
|
public string imei0 = "";
|
public string imei1 = "";
|
public string meid = "";
|
|
void GetetDeviceIMEI()
|
{
|
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
var telephoneyManager = context.Call<AndroidJavaObject>("getSystemService", "phone");
|
imei0 = telephoneyManager.Call<string>("getImei", 0);//如果手机双卡 双待 就会有两个MIEI号
|
imei1 = telephoneyManager.Call<string>("getImei", 1);
|
meid = telephoneyManager.Call<string>("getMeid");//电信的手机 是MEID
|
|
CommonDebugHelper.Debug($"获取到了安卓手机的IMEI!!! imei0:{imei0} imei1:{imei1} meid:{meid}");
|
}
|
|
#endregion
|
|
private void OnClickLoginBtn()
|
{
|
LoginRequest();
|
GA_SDK_Manager.Ins.Statistics(2);//埋点
|
|
}
|
|
/// <summary>
|
/// 登录请求处理
|
/// </summary>
|
/// <param name="req"></param>
|
private void LoginRequest()
|
{
|
if (!isLogining)
|
{
|
Debug.Log("登录");
|
if (Application.platform == RuntimePlatform.WindowsEditor)
|
{
|
Debug.Log("编辑器直接登录");
|
|
GA_SDK_Manager.Ins.Login(imei0);
|
GA_SDK_Manager.Ins.Statistics(3);//埋点
|
|
//StartCoroutine(loginMy());
|
StartCoroutine(LoadScene());
|
return;
|
}
|
|
isLogining = true;
|
string url = GameConfig.IsDebug ? GameConfig.TestLoginUrl : GameConfig.LoginUrl;
|
JsonData data = new JsonData();
|
data["account"] = imei0;
|
data["password"] = "123456";
|
data["channelid"] = (int)ChannelID.Gm;
|
byte[] postBytes = System.Text.Encoding.Default.GetBytes(data.ToJson());
|
StartCoroutine(Post(url, postBytes));
|
}
|
}
|
|
IEnumerator Post(string url, byte[] postBytes)
|
{
|
UnityWebRequest request = UnityWebRequest.Post(url, "POST");
|
request.uploadHandler = new UploadHandlerRaw(postBytes);
|
request.downloadHandler = new DownloadHandlerBuffer();
|
request.SetRequestHeader("Content-Type", "application/json");
|
yield return request.SendWebRequest();
|
|
if (request.isDone)
|
{
|
string text = request.downloadHandler.text;
|
loginData = JsonMapper.ToObject(text);
|
if (loginData.Keys.Contains("errorcode"))
|
{
|
JsonData errorData = loginData["errorcode"];
|
isLogining = false;
|
CommonDebugHelper.DebugSocket(int.Parse(errorData.ToString()));
|
|
yield break;
|
}
|
|
if (!string.IsNullOrEmpty(text))
|
{
|
EventCenter.Ins.Add<bool>((int)KTGMGemClient.EventType.MasterSocketConnectResult, MasterSocketConnectSuccess);
|
MasterSocket.Ins.MasterIp = loginData["masterip"].ToString();
|
MasterSocket.Ins.MasterPort = int.Parse(loginData["masterport"].ToString());
|
MasterSocket.Ins.StartConnect();
|
|
// System.DateTime dt = GameUtils.GetTime(loginData["timestamp"].ToString());
|
// GameConfig.dateTime = dt;
|
// GameConfig.timeSpan = dt - System.DateTime.Now;
|
}
|
else
|
{
|
isLogining = false;
|
//UIManager.Instance.ShowWarningWind("没有登录数据返回,text:" + text);
|
//CommonDebugHelper.DebugError("--------------------- 没有登录数据返回 ---------------------text:" + text);
|
}
|
}
|
else
|
{
|
isLogining = false;
|
//UIManager.Instance.ShowWarningWind(request.error);
|
|
//CommonDebugHelper.DebugError("登录失败 request.error" + request.error);
|
}
|
}
|
|
private void MasterSocketConnectSuccess(bool res)
|
{
|
if (res)
|
{
|
Login_C2S login = new Login_C2S();
|
login.Username = loginData["username"].ToString();
|
login.Channel = (int)ChannelID.Gm;
|
login.SessionKey = loginData["sessionKey"].ToString();
|
MasterSocket.Ins.SendMsg(Opcode.LoginC2S, login);
|
}
|
else
|
{
|
CommonDebugHelper.DebugError("Socket 链接失败了");
|
}
|
}
|
|
private void LoginS2C(IMessage msg)
|
{
|
Login_S2C login = (Login_S2C)msg;
|
if (login.Errorcode == 0)
|
{
|
Debug.Log("--------------------- 登录成功 ---------------------");
|
//UserDataMsg userData = login.Userdata;
|
|
GA_SDK_Manager.Ins.Login(imei0);
|
GA_SDK_Manager.Ins.Statistics(3);//埋点
|
|
//StartCoroutine(loginMy());
|
StartCoroutine(LoadScene());
|
|
}
|
else
|
{
|
CommonDebugHelper.DebugSocket(login.Errorcode);
|
isLogining = false;
|
}
|
}
|
|
|
IEnumerator LoadScene()
|
{
|
async = SceneManager.LoadSceneAsync(GameConfig.NextSceneName);
|
async.allowSceneActivation = true;
|
/*while (!async.isDone)
|
{
|
|
if (async.progress < 0.9f)
|
progressValue = async.progress;
|
else
|
progressValue = 1.0f;
|
slider.value = progressValue;
|
progress.text = (int)(slider.value * 100) + " %";
|
|
if (async.progress >= 0.89)
|
{
|
Debug.Log("进展时间分别是:" + async.progress + "," + totalTime);
|
if( totalTime >= 3.0f )
|
async.allowSceneActivation = true;
|
|
progress.text = "按任意键继续";
|
if (Input.anyKeyDown)
|
{
|
async.allowSceneActivation = true;
|
}
|
|
}
|
|
//yield return null;
|
}*/
|
yield return null;
|
}
|
|
|
IEnumerator loginMy()
|
{
|
int displayProgress = 0;
|
int toProgress = 0;
|
AsyncOperation op = SceneManager.LoadSceneAsync(GameConfig.NextSceneName);
|
op.allowSceneActivation = false;
|
while (op.progress < 0.9f) //此处如果是 <= 0.9f 则会出现死循环所以必须小0.9
|
{
|
toProgress = (int)op.progress * 100;
|
while (displayProgress < toProgress)
|
{
|
++displayProgress;
|
SetLoadingPercentage(displayProgress);
|
yield return new WaitForEndOfFrame();//ui渲染完成之后
|
}
|
}
|
toProgress = 100;
|
while (displayProgress < toProgress)
|
{
|
++displayProgress;
|
SetLoadingPercentage(displayProgress);
|
yield return new WaitForEndOfFrame();
|
}
|
op.allowSceneActivation = true;
|
yield break;
|
|
}
|
|
private void SetLoadingPercentage(int displayProgress)
|
{
|
CommonDebugHelper.Debug($"当前进度{displayProgress}%");
|
|
}
|
|
}
|