95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Guru
|
||
|
|
{
|
||
|
|
public class AnalyticsAgentAndroid: IAnalyticsAgent
|
||
|
|
{
|
||
|
|
|
||
|
|
#if UNITY_ANDROID
|
||
|
|
|
||
|
|
public static readonly string AnalyticsClassName = "com.guru.unity.analytics.Analytics";
|
||
|
|
private static AndroidJavaClass _classAnalytics;
|
||
|
|
private static AndroidJavaClass ClassAnalytics => _classAnalytics ??= new AndroidJavaClass(AnalyticsClassName);
|
||
|
|
|
||
|
|
#endif
|
||
|
|
private static bool _isDebug = false;
|
||
|
|
public static bool UseWorker = true;
|
||
|
|
|
||
|
|
#region 工具方法
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 调用静态方法
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="methodName"></param>
|
||
|
|
/// <param name="args"></param>
|
||
|
|
private static void CallStatic(string methodName, params object[] args)
|
||
|
|
{
|
||
|
|
#if UNITY_ANDROID
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (ClassAnalytics != null)
|
||
|
|
{
|
||
|
|
ClassAnalytics.CallStatic(methodName, args);
|
||
|
|
if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static :: {methodName}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Debug.LogError(e.Message);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 调用静态方法
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="methodName"></param>
|
||
|
|
/// <param name="args"></param>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
private static T CallStatic<T>(string methodName, params object[] args)
|
||
|
|
{
|
||
|
|
#if UNITY_ANDROID
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (ClassAnalytics != null)
|
||
|
|
{
|
||
|
|
if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static <{typeof(T).ToString()}> :: {methodName}");
|
||
|
|
return ClassAnalytics.CallStatic<T>(methodName, args);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Debug.LogError(e.Message);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
return default(T);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region 接口实现
|
||
|
|
|
||
|
|
public void Init(string appId, string deviceInfo, bool isDebug = false)
|
||
|
|
{
|
||
|
|
_isDebug = isDebug;
|
||
|
|
string bundleId = Application.identifier;
|
||
|
|
CallStatic("init", appId, deviceInfo, bundleId, UseWorker, isDebug); // 调用接口
|
||
|
|
}
|
||
|
|
public void SetScreen(string screenName) => CallStatic("setScreen", screenName);
|
||
|
|
public void SetAdId(string id) => CallStatic("setAdId", id);
|
||
|
|
public void SetUserProperty(string key, string value) => CallStatic("setUserProperty", key, value);
|
||
|
|
public void SetFirebaseId(string id) => CallStatic("setFirebaseId", id);
|
||
|
|
public void SetAdjustId(string id) => CallStatic("setAdjustId", id);
|
||
|
|
public void SetDeviceId(string deviceId) => CallStatic("setDeviceId", deviceId);
|
||
|
|
public void SetUid(string uid) => CallStatic("setUid", uid);
|
||
|
|
public bool IsDebug => CallStatic<bool>("isDebug");
|
||
|
|
public void LogEvent(string eventName, string parameters) => CallStatic("logEvent", eventName, parameters);
|
||
|
|
public void ReportEventSuccessRate() => CallStatic("reportEventRate");
|
||
|
|
public void SetTch02Value(double value) => CallStatic("setTch02Value", value);
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|