当前位置:   article > 正文

Unity 第三方SDK框架接入 (Android Studio)_unity sdk框架

unity sdk框架

第三方SDK框架接入 (Android Studio)一 Android

Android 与 Unity 交互:

 UnityPlayer.UnitySendMessage("string","string", var2); 
  • 1

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");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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;
        }
    }
}*/


  • 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
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198

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);
    }
}

  • 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
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218

API类的理解:

API主要是实现MyActivity的接口,接入第三方SDK的相关代码都在这里完成,如: 初始化,登录,支付 ,信息上传等等,
接SDK的时候,几乎都是抄代码,你只需要写好一个模板。根据第三方的文档对着抄就可以了。

导出jar

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
双击exportJar等待生产的AndroidPlugin.jar文件,将他拷贝到unity的Plugins/Android目录下。
工程下载链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/323099?site
推荐阅读
相关标签
  

闽ICP备14008679号