第三方SDK框架接入 (Android Studio)一 Android
Android 与 Unity 交互:
UnityPlayer.UnitySendMessage("string","string", var2);
UnitySendMessage(“string”,“string”, ***),这是方法,我们至少需要传入两个参数,第一个参数为unity中的一个gameobject名称,第二个参数为这个gameobject身上捆绑的脚本中的一个方法,而第三参数事实上是这个对应方法上的参数,有没有参数就看你自己的需求了
Unity 与 Android
private AndroidJavaObject GetAndroidJavaObject()
{
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
return jc.GetStatic<AndroidJavaObject>("currentActivity");
}
private AndroidJavaObject sdk
{
get { return _sdk ?? (_sdk = GetAndroidJavaObject()); }
}
private AndroidJavaObject _sdk;
public void ShowToolBar()
{
sdk.Call("requestShowToolBar");
}
Call 方法和 UnitySendMessage 方法都是固定的形式,只是参数不一样而已。想了解更深的东西自行百度吧。
新建一个Library
我们采用Android Studio 打包成jar给Unity项目。怎么操作,百度一大堆,这里就不描述了。
1.新建一个Library
这里说明一下,我们需要把unity的classes.jar文件添加到我们的libs引用中去,因为我们的Activity需求继承于UnityPlayerActivity才能实现和Android 和 unity 之间的交互。
新建MyActivity类
package com.example.unitylibrary;
import com.unity3d.player.UnityPlayerActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
/**
* Created by LJ on 2019/5/11.
*/
public class MyActivity extends UnityPlayerActivity {
// public static MyHandler myHandler;
public static Activity currentActivity;
private API apl;
@Override protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
currentActivity = this;
// myHandler = new MyHandler();
apl = new API();
apl.requestInit(0);
}
@Override protected void onStart() {
super.onStart();
apl.onStart();
}
@Override protected void onRestart(){
super.onRestart();
apl.onRestart();
}
@Override protected void onPause(){
super.onPause();
apl.onPause();
}
@Override protected void onResume(){
super.onResume();
apl.onResume();
}
@Override protected void onStop() {
super.onStop();
apl.onStop();
}
@Override protected void onDestroy(){
super.onDestroy();
apl.onDestroy();
}
@Override public void finish() {
super.finish();
apl.finish();
}
public void requestLogin()
{
apl.requestLogin();
}
public void requestLogout()
{
apl.requestLogout();
}
public void requestExit()
{
apl.requestExit();
}
public void requestHideToolBar()
{
apl.requestHideToolBar();
}
public boolean requestHasUserCenter()
{
return apl.requestHasUserCenter();
}
public void requestEnterUserCenter()
{
apl.requestEnterUserCenter();
}
public boolean requestHasCustomerCenter()
{
return apl.requestHasCustomerCenter();
}
public void requestEnterCustomerCenter()
{
apl.requestEnterCustomerCenter();
}
public void requestHasForum()
{
apl.requestHasForum();
}
public void requestEnterForum()
{
apl.requestEnterForum();
}
public boolean requestCommand(String command,String arg0)
{
return apl.requestCommand(command, arg0);
}
public String requestConfig(String key)
{
return apl.requestConfig(key);
}
public boolean requestUpgrade(String downloadUrl)
{
return apl.requestUpgrade(downloadUrl);
}
public void reportRoleInfo(String reason, String roleID, String roleName, String roleLevel, String roleGemder,
String rolePower, String roleCreatedTime, String roleLoginAt, String roleLogoutAt,
String roleProfessionId, String roleProfession, String roleServerId, String roleServerName,
String roleUnionId, String roleUnionName, String roleUnionMemberId, String roleUnionMemberName,
String roleFriendIds, String roleVipLevel, String roleVipExp, String roleDiamondCoun, String roleGoldCoins)
{
apl.reportRoleInfo(reason, roleID, roleName, roleLevel, roleGemder, rolePower, roleCreatedTime, roleLoginAt, roleLogoutAt, roleProfessionId, roleProfession, roleServerId, roleServerName, roleUnionId, roleUnionName, roleUnionMemberId, roleUnionMemberName, roleFriendIds, roleVipLevel, roleVipExp, roleDiamondCoun, roleGoldCoins);
}
public void requestPay(String goodID, String goodName, String goodCount, String firstRewardCount, String goodrewardCount,
String quantifier, String currency, String price, boolean isFirstPaymentOfGoods, String cpOrderId,
String callbackUrl, String extraParams)
{
apl.requestPay(goodID, goodName, goodCount, firstRewardCount, goodrewardCount, quantifier,
currency, price, isFirstPaymentOfGoods, cpOrderId, callbackUrl, extraParams);
}
public void reportPaymentResult(String goodID, String goodName, String goodCount, String firstRewardCount, String goodrewardCount,
String quantifier, String currency, String price, boolean isFirstPaymentOfGoods, String rewardCount,
String success)
{
apl.reportPaymentResult(goodID, goodName, goodCount, firstRewardCount, goodrewardCount, quantifier,
currency, price, isFirstPaymentOfGoods, rewardCount, success);
}
}
/*
class MyHandler extends Handler {
public MyHandler() {
}
public MyHandler(Looper L) {
super(L);
}
// 子类必须重写此方法,接受数据
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SDKUtility.INIT: {
Log.i("UNITY", "调用了sdk初始化方法");
}
break;
case SDKUtility.LOGIN: {
Log.i("UNITY", "调用了sdk登陆方法");
}
break;
case SDKUtility.LOGOUT: {
Log.i("UNITY", "调用了sdk登陆出方法");
}
break;
case SDKUtility.PAY: {
Log.i("UNITY", "调用了sdk支付方法");
}
case SDKUtility.GAMECENTER: {
Log.i("UNITY", "调用了sdk游戏中心方法");
}
break;
default:
break;
}
}
}*/
MyActivity 类 提供底层的所有接口。功能不在这个类实现。他只是负责接口的调度。我们新建一个实现接口的APL类。
package com.example.unitylibrary;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
/**
* Created by LJ on 2019/5/11.
*/
public class API {
public void onStart()
{
Log.i(SDKUtility.tag,"onStart");
}
public void onRestart()
{
Log.i(SDKUtility.tag,"onRestart");
}
public void onResume()
{
Log.i(SDKUtility.tag,"onResume");
}
public void onStop()
{
Log.i(SDKUtility.tag,"onStop");
}
public void onPause()
{
Log.i(SDKUtility.tag,"onPause");
}
public void onDestroy()
{
Log.i(SDKUtility.tag,"onDestroy");
}
public void finish()
{
Log.i(SDKUtility.tag,"finish");
}
//---------------------------------------------------------------------------------------黄金分割线---------------------------------------------------------------------------------------
/**
* sdk初始化
* @param flag
*/
public void requestInit(int flag )
{
Log.i(SDKUtility.tag,"初始化");
}
//初始化成功
private void InitSuccess()
{
Log.i(SDKUtility.tag,"初始化成功");
requestLogin();
}
//初始化失败
private void InitFail()
{
Log.i(SDKUtility.tag,"初始化失败");
String str ="";
UnityPlayer.UnitySendMessage("PlatformSdkHelper","SdkInitHandler", str);
}
/**
* sdk登陆
*/
public void requestLogin(){
Log.i(SDKUtility.tag,"requestLogin");
}
//sdk登陆成功
private void LoginSuccess(){
Log.i(SDKUtility.tag,"sdk登陆成功");
}
//sdk登陆失败
private void LoginFail(){
Log.i(SDKUtility.tag,"sdk登陆失败");
}
/**
* 登出
*/
public void requestLogout(){
Log.i(SDKUtility.tag, "requestLogout");
}
//登出成功
public void LogOutSuccess(){
Log.i(SDKUtility.tag,"sdk登出成功");
}
//登出失败
public void LogOutFail(){
Log.i(SDKUtility.tag,"sdk登出失败");
}
public void requestExit()
{
Log.i(SDKUtility.tag, "requestExit");
}
/**
* 游戏中心
*/
public static void GameCenter()
{
Log.i(SDKUtility.tag, "GameCenter");
}
public void requestShowToolBar()
{
Log.i(SDKUtility.tag, "requestShowToolBar");
}
public void requestHideToolBar()
{
Log.i(SDKUtility.tag, "requestHideToolBar");
}
public boolean requestHasUserCenter()
{
Log.i(SDKUtility.tag, "requestHasUserCenter");
return false;
}
public void requestEnterUserCenter()
{
Log.i(SDKUtility.tag, "requestEnterUserCenter");
}
public boolean requestHasCustomerCenter()
{
Log.i(SDKUtility.tag, "requestHasCustomerCenter");
return false;
}
public void requestEnterCustomerCenter()
{
Log.i(SDKUtility.tag, "requestEnterCustomerCenter");
}
public boolean requestHasForum()
{
Log.i(SDKUtility.tag, "requestHasForum");
return false;
}
public void requestEnterForum()
{
Log.i(SDKUtility.tag, "requestEnterForum");
}
public boolean requestCommand(String command,String arg0)
{
Log.i(SDKUtility.tag, "requestCommand");
return false;
}
public String requestConfig(String key)
{
Log.i(SDKUtility.tag, "requestConfig");
return "";
}
public boolean requestUpgrade(String downloadUrl)
{
Log.i(SDKUtility.tag, "requestUpgrade");
return false;
}
public void reportRoleInfo(String reason, String roleID, String roleName, String roleLevel, String roleGemder,
String rolePower, String roleCreatedTime, String roleLoginAt, String roleLogoutAt,
String roleProfessionId, String roleProfession, String roleServerId, String roleServerName,
String roleUnionId, String roleUnionName, String roleUnionMemberId, String roleUnionMemberName,
String roleFriendIds, String roleVipLevel, String roleVipExp, String roleDiamondCoun, String roleGoldCoins)
{
Log.i(SDKUtility.tag, "reason: " + reason + " roleID" + roleID + " roleName" + roleName + " roleLevel" + roleLevel + " roleGemder" + roleGemder
+ " rolePower" + rolePower + " roleCreatedTime" + roleCreatedTime + " roleLoginAt" + roleLoginAt + " roleLogoutAt" + roleLogoutAt
+ " roleProfessionId" + roleProfessionId + " roleProfession" + roleProfession + " roleServerId" + roleServerId + " roleServerName" + roleServerName
+ " roleUnionId" + roleUnionId + " roleUnionName" + roleUnionName + " roleUnionMemberId" + roleUnionMemberId + " roleUnionMemberName" + roleUnionMemberName
+ " roleFriendIds" + roleFriendIds + " roleVipLevel" + roleVipLevel + " roleVipExp" + roleVipExp + " roleDiamondCoun" + roleDiamondCoun + " roleGoldCoins" + roleGoldCoins);
}
public void requestPay(String goodID, String goodName, String goodCount, String firstRewardCount, String goodrewardCount,
String quantifier, String currency, String price, boolean isFirstPaymentOfGoods, String cpOrderId,
String callbackUrl, String extraParams)
{
Log.i(SDKUtility.tag , "goodID: " + goodID + " goodName" + goodName + " goodCount" + goodCount + " firstRewardCount" + firstRewardCount
+ " goodrewardCount" + goodrewardCount+ " quantifier" + quantifier+ " currency" + currency + " price" + price + " isFirstPaymentOfGoods" + isFirstPaymentOfGoods
+ " cpOrderId" + cpOrderId + " callbackUrl" + callbackUrl + " extraParams" + extraParams);
}
public void reportPaymentResult(String goodID, String goodName, String goodCount, String firstRewardCount, String goodrewardCount,
String quantifier, String currency, String price, boolean isFirstPaymentOfGoods, String rewardCount,
String success)
{
Log.i(SDKUtility.tag , "goodID: " + goodID + " goodName" + goodName + " goodCount" + goodCount+ " firstRewardCount" + firstRewardCount
+ " goodrewardCount" + goodrewardCount+ " quantifier" + quantifier+ " currency" + currency+ " price" + price+ " isFirstPaymentOfGoods" + isFirstPaymentOfGoods
+ " rewardCount" + rewardCount+ " success" + success);
}
}
API类的理解:
API主要是实现MyActivity的接口,接入第三方SDK的相关代码都在这里完成,如: 初始化,登录,支付 ,信息上传等等,
接SDK的时候,几乎都是抄代码,你只需要写好一个模板。根据第三方的文档对着抄就可以了。
导出jar
双击exportJar等待生产的AndroidPlugin.jar文件,将他拷贝到unity的Plugins/Android目录下。