Unity 桥接 sdk 交互实现
android 桥接SDK
流程可以参考这个链接,流程讲得很仔细,非常感谢大佬的笔记: blog.csdn.net/qq_37619255...
1.Unity 导出android 需注意的地方
-
增加Unity的点击事件
csharpusing System.Collections; using System.Collections.Generic; using UnityEngine; //对应事件 public class BtnManageClick : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } public void initBtnCLick() { Debug.Log("initBtnCLick"); print("initBtnCLick static"); } public void loginBtnCLick() { Debug.Log("loginBtnCLick"); AndroidJavaObject helper = new AndroidJavaObject("com.jiama.sdk.HWgame"); helper.CallStatic("autoLogin"); print("loginBtnCLick"); } public void payBtnClick() { Debug.Log("payBtnClick"); print("payBtnClick"); } }
注意的地方是: 点击事件需要增加 Add Component事件,我使用 Main Camera 监听点击事件
- android Studio 中增加android sdk处理事件
java
package com.jiama.sdk;
import android.app.Activity;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import org.json.JSONObject;
public class HWgame {
private static Activity activity;
public static void init(Activity act){
activity = act;
}
public static void autoLogin() {
Log.i("Unity", "autologin----------------");
GameSDKApi.login(activity, new ApiCallback() {
@Override
public void successCallBack(JSONObject jsonObject) {
String jsonStr = jsonObject.toString();
Log.i("Unity", "autologin----------------" + jsonStr);
UnityPlayer.UnitySendMessage("Main Camera","ReceiveMessageFromAndroid",jsonStr);
}
@Override
public void failCallBack(Error error) {
UnityPlayer.UnitySendMessage("Main Camera","ReceiveMessageFromAndroid",error.getMessage());
}
});
}
}
上面的demo 样式需要需要 UnityPlayer.UnitySendMessage 能让unity 接收到信息,需要增加 UnityMessageReceiver script
源码如下:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityMessageReceiver : MonoBehaviour
{
void ReceiveMessageFromAndroid(string message)
{
//接收adnroid 那边传递过来的信息
Debug.Log("Received message from Android: " + message);
}
}
增加这些代码接收adnroid 那边传递过来的信息。