赞
踩
上一篇,我们着重讲解了用Unity开发的手游,在接入U8SDK时,Android中的部分。接下来,这一篇,我们就来看看,在Unity工程中,我们需要怎么调用上一篇中我们提供的各种接口,以及怎么接收Android中的消息通知,比如登录成功,切换帐号成功等。
- public abstract class U8SDKInterface{
-
- public delegate void LoginSucHandler(U8LoginResult data);
- public delegate void LogoutHandler();
-
- private static U8SDKInterface _instance;
-
- public LoginSucHandler OnLoginSuc;
- public LogoutHandler OnLogout;
-
-
- public static U8SDKInterface Instance
- {
- get
- {
- if (_instance == null)
- {
- #if UNITY_EDITOR || UNITY_STANDLONE
- _instance = new SDKInterfaceDefault();
- #elif UNITY_ANDROID
- _instance = new SDKInterfaceAndroid();
- #elif UNITY_IOS
- _instance = new SDKInterfaceIOS();
- #endif
- }
-
- return _instance;
- }
- }
-
- //初始化
- public abstract void Init();
-
- //登录
- public abstract void Login();
-
- //自定义登录,用于腾讯应用宝,QQ登录,customData="QQ";微信登录,customData="WX"
- public abstract void LoginCustom(string customData);
-
- //切换帐号
- public abstract void SwitchLogin();
-
- //登出
- public abstract bool Logout();
-
- //显示个人中心
- public abstract bool ShowAccountCenter();
-
- //上传游戏数据
- public abstract void SubmitGameData(U8ExtraGameData data);
-
- //调用SDK的退出确认框,返回false,说明SDK不支持退出确认框,游戏需要使用自己的退出确认框
- public abstract bool SDKExit();
-
- //调用SDK支付界面
- public abstract void Pay(U8PayParams data);
-
- //SDK是否支持退出确认框
- public abstract bool IsSupportExit();
-
- //SDK是否支持用户中心
- public abstract bool IsSupportAccountCenter();
-
- //SDK是否支持登出
- public abstract bool IsSupportLogout();
-
- //去U8Server获取游戏订单号,这里逻辑是访问游戏服务器,然后游戏服务器去U8Server获取订单号
- //并返回
- public U8PayParams reqOrder(U8PayParams data)
- {
- //TODO 去游戏服务器获取订单号
-
- //测试
- data.orderID = "345435634534";
- data.extension = "test";
-
- return data;
- }
-
- }
- public class SDKInterfaceAndroid : U8SDKInterface
- {
-
- private AndroidJavaObject jo;
-
- public SDKInterfaceAndroid()
- {
- using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
- }
- }
-
- private T SDKCall<T>(string method, params object[] param)
- {
- try
- {
- return jo.Call<T>(method, param);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- return default(T);
- }
-
- private void SDKCall(string method, params object[] param)
- {
- try
- {
- jo.Call(method, param);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
-
- //这里Android中,在onCreate中直接调用了initSDK,所以这里就不用调用了
- public override void Init()
- {
- //SDKCall("initSDK");
-
- }
-
- public override void Login()
- {
- SDKCall("login");
- }
-
- public override void LoginCustom(string customData)
- {
- SDKCall("loginCustom", customData);
- }
-
- public override void SwitchLogin()
- {
- SDKCall("switchLogin");
- }
-
- public override bool Logout()
- {
- if (!IsSupportLogout())
- {
- return false;
- }
-
- SDKCall("logout");
- return true;
- }
-
- public override bool ShowAccountCenter()
- {
- if (!IsSupportAccountCenter())
- {
- return false;
- }
-
- SDKCall("showAccountCenter");
- return true;
- }
-
- public override void SubmitGameData(U8ExtraGameData data)
- {
- string json = encodeGameData(data);
- SDKCall("submitExtraData", json);
- }
-
- public override bool SDKExit()
- {
- if (!IsSupportExit())
- {
- return false;
- }
-
- SDKCall("exit");
- return true;
- }
-
- public override void Pay(U8PayParams data)
- {
- string json = encodePayParams(data);
- SDKCall("pay", json);
- }
-
- public override bool IsSupportExit()
- {
- return SDKCall<bool>("isSupportExit");
- }
-
- public override bool IsSupportAccountCenter()
- {
- return SDKCall<bool>("isSupportAccountCenter");
- }
-
- public override bool IsSupportLogout()
- {
- return SDKCall<bool>("isSupportLogout");
- }
-
- private string encodeGameData(U8ExtraGameData data)
- {
- Dictionary<string, object> map = new Dictionary<string, object>();
- map.Add("dataType", data.dataType);
- map.Add("roleID", data.roleID);
- map.Add("roleName", data.roleName);
- map.Add("roleLevel", data.roleLevel);
- map.Add("serverID", data.serverID);
- map.Add("serverName", data.serverName);
- map.Add("moneyNum", data.moneyNum);
- return MiniJSON.Json.Serialize(map);
- }
-
- private string encodePayParams(U8PayParams data)
- {
- Dictionary<string, object> map = new Dictionary<string, object>();
- map.Add("productId", data.productId);
- map.Add("productName", data.productName);
- map.Add("productDesc", data.productDesc);
- map.Add("price", data.price);
- map.Add("buyNum", data.buyNum);
- map.Add("coinNum", data.coinNum);
- map.Add("serverId", data.serverId);
- map.Add("serverName", data.serverName);
- map.Add("roleId", data.roleId);
- map.Add("roleName", data.roleName);
- map.Add("roleLevel", data.roleLevel);
- map.Add("vip", data.vip);
- map.Add("orderID", data.orderID);
- map.Add("extension", data.extension);
-
- return MiniJSON.Json.Serialize(map);
- }
- }
- public class U8SDKCallback : MonoBehaviour
- {
-
- private static U8SDKCallback _instance;
-
- private static object _lock = new object();
-
- //初始化回调对象
- public static U8SDKCallback InitCallback()
- {
- UnityEngine.Debug.LogError("Callback->InitCallback");
-
- lock (_lock)
- {
- if (_instance == null)
- {
- GameObject callback = GameObject.Find("(u8sdk_callback)");
- if (callback == null)
- {
- callback = new GameObject("(u8sdk_callback)");
- UnityEngine.Object.DontDestroyOnLoad(_instance);
- _instance = callback.AddComponent<U8SDKCallback>();
-
- }
- else
- {
- _instance = callback.GetComponent<U8SDKCallback>();
- }
- }
-
- return _instance;
- }
- }
-
-
- //初始化成功回调
- public void OnInitSuc()
- {
- //一般不需要处理
- UnityEngine.Debug.LogError("Callback->OnInitSuc");
- }
-
- //登录成功回调
- public void OnLoginSuc(string jsonData)
- {
- UnityEngine.Debug.LogError("Callback->OnLoginSuc");
-
- U8LoginResult data = parseLoginResult(jsonData);
- if (data == null)
- {
- UnityEngine.Debug.LogError("The data parse error." + jsonData);
- return;
- }
-
- if (U8SDKInterface.Instance.OnLoginSuc != null)
- {
- U8SDKInterface.Instance.OnLoginSuc.Invoke(data);
- }
- }
-
- //切换帐号回调
- public void OnSwitchLogin()
- {
-
- UnityEngine.Debug.LogError("Callback->OnSwitchLogin");
-
- if (U8SDKInterface.Instance.OnLogout != null)
- {
- U8SDKInterface.Instance.OnLogout.Invoke();
- }
- }
-
- //登出回调
- public void OnLogout()
- {
- UnityEngine.Debug.LogError("Callback->OnLogout");
-
- if (U8SDKInterface.Instance.OnLogout != null)
- {
- U8SDKInterface.Instance.OnLogout.Invoke();
- }
- }
-
- //支付回调,网游不需要实现该接口,该接口用于单机游戏
- public void OnPaySuc(string jsonData)
- {
- //Nothing...
- }
-
- private U8LoginResult parseLoginResult(string str)
- {
- object jsonParsed = MiniJSON.Json.Deserialize(str);
- if (jsonParsed != null)
- {
- Dictionary<string, object> jsonMap = jsonParsed as Dictionary<string, object>;
- U8LoginResult data = new U8LoginResult();
- if (jsonMap.ContainsKey("isSuc"))
- {
- data.isSuc = bool.Parse(jsonMap["isSuc"].ToString());
- }
- if (jsonMap.ContainsKey("isSwitchAccount"))
- {
- data.isSwitchAccount = bool.Parse(jsonMap["isSwitchAccount"].ToString());
- }
- if (jsonMap.ContainsKey("userID"))
- {
- data.userID = jsonMap["userID"].ToString();
- }
- if (jsonMap.ContainsKey("sdkUserID"))
- {
- data.sdkUserID = jsonMap["sdkUserID"].ToString();
-
- }
- if (jsonMap.ContainsKey("username"))
- {
- data.username = jsonMap["username"].ToString();
- }
-
- if (jsonMap.ContainsKey("sdkUsername"))
- {
- data.sdkUsername = jsonMap["sdkUsername"].ToString();
- }
- if (jsonMap.ContainsKey("token"))
- {
- data.token = jsonMap["token"].ToString();
- }
-
- return data;
- }
-
- return null;
- }
- }
- public class ClickObject : MonoBehaviour {
-
- // Use this for initialization
-
- private Text txtState;
-
- void Start () {
-
- U8SDKCallback.InitCallback();
-
- GameObject loginObj = GameObject.Find("BtnLogin");
- Button btnLogin = loginObj.GetComponent<Button>();
- btnLogin.onClick.AddListener(delegate()
- {
- OnLoginClick();
- });
-
- GameObject payObj = GameObject.Find("BtnPay");
- Button btnPay = payObj.GetComponent<Button>();
- btnPay.onClick.AddListener(delegate()
- {
- OnPayClick();
- });
-
- GameObject stateObj = GameObject.Find("TxtState");
- txtState = stateObj.GetComponent<Text>();
-
-
- U8SDKInterface.Instance.OnLoginSuc = delegate(U8LoginResult result)
- {
- OnLoginSuc(result);
- };
-
- U8SDKInterface.Instance.OnLogout = delegate()
- {
- OnLogout();
- };
- }
-
- void Update()
- {
- if (Input.GetKeyUp(KeyCode.Escape))
- {
- if (!U8SDKInterface.Instance.SDKExit())
- {
- //TODO 退出确认框
- Application.Quit();
- }
- }
- }
-
-
- void OnLoginSuc(U8LoginResult result)
- {
- if (!result.isSuc)
- {
- txtState.text = "登录失败";
- return;
- }
-
- if (result.isSwitchAccount)
- {
- txtState.text = "切换帐号成功:" + result.token;
- }
- else
- {
- txtState.text = "登录成功:" + result.token;
- }
-
-
- }
-
- void OnLogout()
- {
- txtState.text = "未登录";
- }
-
- void OnLoginClick()
- {
- U8SDKInterface.Instance.Login();
- }
-
- void OnPayClick()
- {
- U8PayParams data = new U8PayParams();
- data.productId = "1";
- data.productName = "元宝";
- data.productDesc = "购买100元宝,赠送20元宝";
- data.price = 100;
- data.buyNum = 1;
- data.coinNum = 300;
- data.serverId = "10";
- data.serverName = "地狱之恋";
- data.roleId = "u8_24532452";
- data.roleName = "麻利麻利吼";
- data.roleLevel = 15;
- data.vip = "v15";
-
- data = U8SDKInterface.Instance.reqOrder(data);
-
- U8SDKInterface.Instance.Pay(data);
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。