标签:start key tip exce 数据结构 track int 方法 this
public class U8UnityContext extends UnityPlayerActivity{ public final static String CALLBACK_GAMEOBJECT_NAME = "(u8sdk_callback)"; //unity中接收回调通知的GameObject的名称 public final static String CALLBACK_INIT = "OnInitSuc"; //SDK初始化成功的回调方法名称和Unity中一致 public final static String CALLBACK_LOGIN = "OnLoginSuc"; //SDK登录成功的回调方法名称和Unity中一致 public final static String CALLBACK_SWITCH_LOGIN = "OnSwitchLogin"; //SDK切换帐号的回调方法名称和Unity中一致 public final static String CALLBACK_LOGOUT = "OnLogout"; //SDK登出的回调方法名称和Unity中一致 public final static String CALLBACK_PAY = "OnPaySuc"; //SDK支付成功回调方法名称和Unity中一致 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initSDK(); } //U8SDK 初始化 public void initSDK(){ U8SDK.getInstance().setSDKListener(new UnityU8SDKListener(this)); U8SDK.getInstance().init(this); U8SDK.getInstance().onCreate(); } //登录接口 public void login(){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().login(); } }); } //自己定义登录接口 public void loginCustom(final String customData){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().login(customData); } }); } //切换帐号接口 public void switchLogin(){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().switchLogin(); } }); } //显示用户中心接口 public void showAccountCenter(){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().showAccountCenter(); } }); } //登出 public void logout(){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().logout(); } }); } //提交扩展数据 public void submitExtraData(String data){ final UserExtraData extraData = parseGameData(data); U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().submitExtraData(extraData); } }); } //SDK退出接口 public void exit(){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8User.getInstance().exit(); } }); } //支付接口 public void pay(String data){ final PayParams params = parsePayParams(data); U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { U8Pay.getInstance().pay(params); } }); } //SDK是否支持退出确认功能 public boolean isSupportExit(){ return U8User.getInstance().isSupport("exit"); } //SDK是否支持用户中心 public boolean isSupportAccountCenter(){ return U8User.getInstance().isSupport("showAccountCenter"); } //SDK是否支持登出 public boolean isSupportLogout(){ return U8User.getInstance().isSupport("logout"); } //向Unity中发送消息 public void sendCallback(String name, String jsonParams){ if(jsonParams == null){ jsonParams = ""; } UnityPlayer.UnitySendMessage(CALLBACK_GAMEOBJECT_NAME, name, jsonParams); } private UserExtraData parseGameData(String str){ UserExtraData data = new UserExtraData(); try { JSONObject json = new JSONObject(str); data.setDataType(json.getInt("dataType")); data.setRoleID(json.getString("roleID")); data.setRoleName(json.getString("roleName")); data.setRoleLevel(json.getString("roleLevel")); data.setServerID(json.getInt("serverID")); data.setServerName(json.getString("serverName")); data.setMoneyNum(json.getInt("moneyNum")); } catch (JSONException e) { e.printStackTrace(); } return data; } private PayParams parsePayParams(String str){ PayParams params = new PayParams(); try{ JSONObject json = new JSONObject(str); params.setProductId(json.getString("productId")); params.setProductName(json.getString("productName")); params.setProductDesc(json.getString("productDesc")); params.setPrice(json.getInt("price")); params.setRatio(0);//该字段废弃不用 params.setBuyNum(json.getInt("buyNum")); params.setCoinNum(json.getInt("coinNum")); params.setServerId(json.getString("serverId")); params.setServerName(json.getString("serverName")); params.setRoleId(json.getString("roleId")); params.setRoleName(json.getString("roleName")); params.setRoleLevel(json.getInt("roleLevel")); params.setPayNotifyUrl("");//该字段废弃不用 params.setVip(json.getString("vip")); params.setOrderID(json.getString("orderID")); params.setExtension(json.getString("extension")); }catch(Exception e){ e.printStackTrace(); } return params; } public void onActivityResult(int requestCode, int resultCode, Intent data){ U8SDK.getInstance().onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); } public void onStart(){ U8SDK.getInstance().onStart(); super.onStart(); } public void onPause(){ U8SDK.getInstance().onPause(); super.onPause(); } public void onResume(){ U8SDK.getInstance().onResume(); super.onResume(); } public void onNewIntent(Intent newIntent){ U8SDK.getInstance().onNewIntent(newIntent); super.onNewIntent(newIntent); } public void onStop(){ U8SDK.getInstance().onStop(); super.onStop(); } public void onDestroy(){ U8SDK.getInstance().onDestroy(); super.onDestroy(); } public void onRestart(){ U8SDK.getInstance().onRestart(); super.onRestart(); } }
public class UnityU8SDKListener implements IU8SDKListener{ private U8UnityContext context; private boolean isSwitchAccount = false; //当前是否为切换帐号 public UnityU8SDKListener(U8UnityContext context){ this.context = context; } @Override public void onResult(final int code, String msg) { // TODO Auto-generated method stub U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { switch(code){ case U8Code.CODE_INIT_SUCCESS: context.sendCallback(U8UnityContext.CALLBACK_INIT, null); break; case U8Code.CODE_INIT_FAIL: Toast.makeText(context, "SDK初始化失败", Toast.LENGTH_SHORT).show(); break; case U8Code.CODE_LOGIN_FAIL: //这里不须要提示。一般SDK有提示 //Toast.makeText(context, "SDK登录失败", Toast.LENGTH_SHORT).show(); break; case U8Code.CODE_PAY_FAIL: Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show(); break; case U8Code.CODE_PAY_SUCCESS: Toast.makeText(context, "支付成功,到账时间可能稍有延迟", Toast.LENGTH_SHORT).show(); break; } } }); } //此接口已经废弃 @Override public void onInitResult(InitResult result) { // TODO Auto-generated method stub // 此接口已经废弃 } //SDK登录成功的回调 @Override public void onLoginResult(String data) { Log.d("U8SDK", "SDK 登录成功,不用做处理,在onAuthResult中处理登录成功, 參数例如以下:"); Log.d("U8SDK", data); this.isSwitchAccount = false; tip("SDK登录成功"); } //切换帐号。须要回到登录界面,并弹出SDK登录界面 @Override public void onSwitchAccount() { context.sendCallback(U8UnityContext.CALLBACK_SWITCH_LOGIN, null); } //切换帐号,并登录成功。到这里和Login的回调onLoginResult一样 @Override public void onSwitchAccount(String data) { Log.d("U8SDK", "SDK 切换帐号并登录成功,不用做处理。在onAuthResult中处理登录成功, 參数例如以下:"); Log.d("U8SDK", data); this.isSwitchAccount = true; tip("切换帐号成功"); } //登出,须要回到登录界面,并弹出SDK登录界面 @Override public void onLogout() { context.sendCallback(U8UnityContext.CALLBACK_LOGOUT, null); } //SDK登录成功之后。去U8Server进行登录认证 @Override public void onAuthResult(UToken authResult) { if(!authResult.isSuc()){ tip("SDK登录认证失败,确认U8Server是否配置"); return; } JSONObject json = new JSONObject(); try{ json.put("isSuc", authResult.isSuc()); json.put("isSwitchAccount", isSwitchAccount); if(authResult.isSuc()){ json.put("userID", authResult.getUserID()); json.put("sdkUserID", authResult.getSdkUserID()); json.put("username", authResult.getUsername()); json.put("sdkUsername", authResult.getSdkUsername()); json.put("token", authResult.getToken()); } }catch(Exception e){ e.printStackTrace(); } context.sendCallback(U8UnityContext.CALLBACK_LOGIN, json.toString()); } //对于手机网游,不须要实现这个接口,由于网游支付是通过server回调通知加虚拟币的。 //这个接口主要用于单机游戏。作为单机的支付结果回调接口 @Override public void onPayResult(PayResult result) { //TODO } private void tip(final String tip){ U8SDK.getInstance().runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(context, tip, Toast.LENGTH_SHORT).show(); } }); } }
比方Login,switchLogin,logout等。而这些方法本身不过对U8SDK抽象层提供的方法进行了一个简单的包裹。通过,U8SDK.getInstance().runOnMainThread()来让全部的接口都在UI主线程中完毕。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.u8.sdk" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.u8.sdk.U8Application"> <activity android:name="com.u8.sdk.U8UnityContext" android:label="@string/app_name" android:screenOrientation="landscape" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
教你高速高效接入SDK——Unity统一接入渠道SDK(Android篇)
标签:start key tip exce 数据结构 track int 方法 this
原文地址:http://www.cnblogs.com/clnchanpin/p/6920548.html