PlatMsgManager
给PlatMsgManager
添加QQAuthorValid
和GetQQSession
方法,用来调用安卓底层对应的方法,从而验证、刷新和获取QQ票据。然后添加几个用于QQ和微信登录和注销的const值,最后添加用于转换底层json消息的PlatformMsg
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public const int PLATFORM_MSG_QQLOGIN = 1; public const int PLATFORM_MSG_QQLOGOUT = 2; public const int PLATFORM_MSG_WXLOGIN = 3; public const int PLATFORM_MSG_WXLOGOUT = 4;
public const int UNITY_GET_STRING_QQAUTHORVALID = 1; public const int UNITY_GET_STRING_QQREFRESHSESSION = 2;
public bool QQAuthorValid() { return System.Convert.ToBoolean(GetStringFromPlatform(UNITY_GET_STRING_QQAUTHORVALID)); } public string GetQQSession() { string qqData = GetStringFromPlatform(UNITY_GET_STRING_QQREFRESHSESSION); return qqData; }
public class PlatformMsg { public string openid; public string token; public string unionid; public string refreshtoken; public string expires; public string paytoken; public string pf; public string pfkey; public string expirestime; }
|
PhoneManager
在Assets——Scripts——Manager内新建PhoneManager
,我们在这个类里维护登录状态,并且对接游戏内登录相关的UI。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| using UnityEngine; using Newtonsoft.Json; public enum PlatformEnum { None, QQ = 1, WX = 2 }
public class PhoneManager : SingletonPattern<PhoneManager> { public bool IsLogin { get; set; } = false; public PlatformEnum CurrentPlatform { get; set; } = PlatformEnum.None; public System.Action<PlatformEnum> LoginSuccess; public System.Action<PlatformEnum> LoginFailure; public System.Action<PlatformEnum> LoginCancel;
public void LoginCallback(string data, PlatformEnum plat, int result = 0) { switch (result) { case 0: { Debug.Log("票据获取:" + data); var platMsg = JsonConvert.DeserializeObject<PlatformMsg>(data); string openid = platMsg.openid; string unionid = platMsg.unionid; string token = platMsg.token; IsLogin = true; CurrentPlatform = plat; if (LoginSuccess != null) { LoginSuccess.Invoke(plat); } PlayerPrefs.SetString("Login",plat.ToString()); PlayerPrefs.Save(); break; } case 1: if (LoginFailure != null) { LoginFailure.Invoke(plat); } break; case 2: if (LoginCancel != null) { LoginCancel.Invoke(plat); } break; } } public void Login(PlatformEnum platform) { if(platform == PlatformEnum.QQ) { PlatMsgManager.Instance.SendUnityMessageToPlatform(PlatMsgManager.PLATFORM_MSG_QQLOGIN); } else if (platform == PlatformEnum.WX) { PlatMsgManager.Instance.SendUnityMessageToPlatform(PlatMsgManager.PLATFORM_MSG_WXLOGIN); } } public void AutoLogin() { #if !UNITY_EDITOR if (PlayerPrefs.HasKey("Login")) { PlatformEnum plat = (PlatformEnum)Enum.Parse(typeof(PlatformEnum),PlayerPrefs.GetString("Login")); if (plat == PlatformEnum.QQ) { if (PlatMsgManager.Instance.QQAuthorValid()) { string qqData = PlatMsgManager.Instance.GetQQSession(); LoginCallback(qqData, PlatformEnum.QQ); } } else if (plat == PlatformEnum.WX) { if (PlatMsgManager.Instance.WXAuthorValid()) { string wxData = PlatMsgManager.Instance.GetWXSession(); LoginCallback(wxData, PlatformEnum.WX); } } } else { if (PlatMsgManager.Instance.QQAuthorValid()) { string qqData = PlatMsgManager.Instance.GetQQSession(); LoginCallback(qqData, PlatformEnum.QQ); } else if (PlatMsgManager.Instance.WXAuthorValid()) { string wxData = PlatMsgManager.Instance.GetWXSession(); LoginCallback(wxData, PlatformEnum.WX); }
} #endif } public void Logout() { switch(CurrentPlatform) { case PlatformEnum.QQ: PlatMsgManager.Instance.SendUnityMessageToPlatform(PlatMsgManager.PLATFORM_MSG_QQLOGOUT); break; case PlatformEnum.WX: PlatMsgManager.Instance.SendUnityMessageToPlatform(PlatMsgManager.PLATFORM_MSG_WXLOGOUT); break; } } }
|
修改PlatformScript
,在其中获得底层的消息后调用回调方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private void Update() { while (msgQueue.Count > 0) { PlatformMsg msg = msgQueue.Dequeue(); switch (msg.iMsgId) { case PLATFORM_MSG_QQLOGINCALLBACK: PhoneManager.Instance.LoginCallback(msg.strParam1, PlatformEnum.QQ, msg.iParam1); break; case PLATFORM_MSG_WXLOGINCALLBACK: break; default: break; } } }
|
登录界面
具体登录界面的设计略,主要是放了QQ、微信登录两个按钮和登出一个按钮。
我们看一下界面的逻辑,我们在Scripts——UGUI——Window中添加LoginWnd
文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| using UnityEngine;
public class LoginWnd : WindowBase { public override void OnAwake(object param1 = null, object param2 = null, object param3 = null) { #if UNITY_ANDROID || UNITY_IOS PhoneManager.Instance.LoginSuccess += LoginSuccess; PhoneManager.Instance.LoginFailure += LoginFailure; PhoneManager.Instance.LoginCancel += LoginCancel;
if (!PhoneManager.Instance.IsLogin) { PhoneManager.Instance.AutoLogin(); }
#endif } public override void OnClose() { PhoneManager.Instance.LoginSuccess -= LoginSuccess; PhoneManager.Instance.LoginFailure -= LoginFailure; PhoneManager.Instance.LoginCancel -= LoginCancel; } void LoginSuccess(PlatformEnum platform) { if(platform == PlatformEnum.QQ) { Debug.Log("QQ登录成功"); } else if(platform == PlatformEnum.WX) { Debug.Log("WX登录成功"); } } public void LoginFailure(PlatformEnum platform) { Debug.Log("登录失败"); } void LoginCancel(PlatformEnum platform) { Debug.Log("取消登录"); } void OnClickQQ() { PhoneManager.Instance.Login(PlatformEnum.QQ); } void OnClickWX() { PhoneManager.Instance.Login(PlatformEnum.WX); } void OnClickLogout() { PhoneManager.Instance.Logout(); } }
|
注意,我们这里在OnClickLogout
使用伪代码进行登录按钮的还原,更推荐写在LoginCancel
里