Android设置
Android代码
package com.unity3d.player;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.bytedance.ads.convert.BDConvert;
import com.bytedance.ads.convert.config.BDConvertConfig;
import com.bytedance.ads.convert.event.ConvertReportHelper;
import org.json.JSONObject;
import org.json.JSONException;
public class MainActivity extends UnityPlayerActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//InitBDConvert();
}
public void InitBDConvert() {
final Context context = this;
final Activity activity = this;
BDConvert.INSTANCE.init(context, activity);
}
// 注册事件上报
public void reportRegister() {
try {
ConvertReportHelper.onEventRegister("wechat", true);
Log.d("MainActivity", "注册事件上报成功: " + "wechat" + ", " + true);
} catch (Exception e) {
Log.e("MainActivity", "注册事件上报失败: " + e.getMessage());
}
}
// 支付事件上报 商品类型,商品名称,商品ID,商品数量,支付渠道,币种,是否成功(必传),金额(必传)
public void reportPurchase(String productType, String productName, String productId,
int quantity, String paymentChannel, String currency,
boolean isSuccess, int amount) {
try {
ConvertReportHelper.onEventPurchase(productType, productName, productId,
quantity, paymentChannel, currency, isSuccess, amount);
Log.d("MainActivity", "支付事件上报成功: " + productName + ", 金额: " + amount);
} catch (Exception e) {
Log.e("MainActivity", "支付事件上报失败: " + e.getMessage());
}
}
}
Unity代码
using UnityEngine;
public partial class GameSDK : MonoBehaviour
{
private AndroidJavaObject activity;
public static GameSDK Instance;
private bool _isInit;
private void Awake()
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void Start()
{
#if !UNITY_EDITOR && UNITY_ANDROID
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
bool isAccept = PlayerPrefs.GetInt("LoginAccept", 0) == 1;
if (isAccept)
{
InitSDK();
}
#endif
}
public void InitSDK()
{
if (_isInit)
return;
Debug.Log("初始化SDK");
_isInit = true;
try
{
if (activity != null)
{
activity.Call("InitBDConvert");
Debug.Log("成功调用InitBDConvert");
}
else
{
Debug.LogError("activity为null,无法调用InitBDConvert");
}
}
catch (System.Exception e)
{
Debug.LogError($"调用InitBDConvert失败: {e.Message}");
Debug.LogError($"异常堆栈: {e.StackTrace}");
}
}
/// <summary>
/// 注册事件上报 成功上报即可
/// </summary>
public void ReportRegister()
{
try
{
if (activity != null)
{
activity.Call("reportRegister");
Debug.Log($"注册事件上报成功");
}
}
catch (System.Exception e)
{
Debug.LogError($"注册事件上报失败: {e.Message}");
}
}
/// <summary>
/// 支付事件上报 ConvertReportHelper.onEventPurchase("gift","flower", "008",1, "wechat","¥", true, 1);
/// </summary>
/// <param name="productType">商品类型</param>
/// <param name="productName">商品名称</param>
/// <param name="productId">商品ID</param>
/// <param name="quantity">商品数量</param>
/// <param name="paymentChannel">支付渠道</param>
/// <param name="currency">币种</param>
/// <param name="isSuccess">是否成功(必传)</param>
/// <param name="amount">金额(必传)</param>
public void ReportPurchase(string productType, string productName, string productId,
int quantity, string paymentChannel, string currency,
bool isSuccess, int amount)
{
try
{
if (activity != null)
{
activity.Call("reportPurchase", productType, productName, productId, quantity, paymentChannel, currency, isSuccess, amount);
Debug.Log($"支付事件上报: {productName}, 金额: {amount}");
}
}
catch (System.Exception e)
{
Debug.LogError($"支付事件上报失败: {e.Message}");
}
}
}