NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/DeviceIdHandle.cs

116 lines
3.9 KiB
C#
Raw Normal View History

2025-11-03 15:37:16 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeviceIdHandle : MonoBehaviour
{
private static DeviceIdHandle _instance;
private AndroidJavaObject oaidActivity;
public static DeviceIdHandle Instance
{
get
{
if (_instance is null)
{
var go = new GameObject("DeviceIdHandle");
_instance = go.AddComponent<DeviceIdHandle>();
DontDestroyOnLoad(go);
}
return _instance;
}
}
2025-11-12 19:29:37 +08:00
// 原有方法保留
2025-11-03 15:37:16 +08:00
public void GetOAID()
{
if (oaidActivity == null)
{
// 获取当前的 Android Activity
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
oaidActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
}
// 调用 Android 的 GetOAID 方法
AndroidJavaObject utils = new AndroidJavaObject("com.example.oaidtest2.OAID_Activity");
utils.Call("GetOAID", gameObject.name, "OnOAIDReceived");
2025-11-12 19:29:37 +08:00
}
// 新增:从 Resources 加载证书并在调用 GetOAID 之前初始化 SDK(同步、用于快速验证)
// resourceName 为 Resources 下资源名(不含扩展名),例如文件 Assets/Resources/cert.pem -> resourceName = "cert"
public void GetOAID_FromResources(string resourceName = "com.phxh.official.nld.cert")
{
// 同步加载证书 TextAsset
TextAsset txt = Resources.Load<TextAsset>(resourceName);
if (txt == null)
{
Debug.LogError($"Load certificate failed: Resources/{resourceName} not found. Put your cert file under Assets/Resources and omit extension when calling.");
#if UNITY_EDITOR
// 在编辑器中为了便于调试,也可以输出示例路径
Debug.Log($"Editor path suggestion: Assets/Resources/{resourceName}.pem or .txt");
#endif
return;
}
string cert = txt.text;
if (string.IsNullOrEmpty(cert))
{
Debug.LogError("Certificate content is empty.");
return;
}
#if UNITY_ANDROID && !UNITY_EDITOR
2025-11-14 12:19:27 +08:00
//获取当前 Android Activity 并调用插件初始化接口
2025-11-12 19:29:37 +08:00
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
oaidActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
AndroidJavaObject utils = new AndroidJavaObject("com.example.oaidtest2.OAID_Activity");
// 初始化/设置证书后再请求 OAID
2025-11-14 12:19:27 +08:00
utils.Call("GetOAID", gameObject.name, "OnOAIDReceived", cert);
2025-11-12 19:29:37 +08:00
}
catch (System.Exception e)
{
Debug.LogError("GetOAID_FromResources failed: " + e);
}
#else
Debug.Log("GetOAID_FromResources: certificate loaded (editor or non-Android). Android call skipped.\ncontent: " + cert);
#endif
2025-11-03 15:37:16 +08:00
}
2025-11-14 13:39:58 +08:00
public void GetIMEI()
{
if (oaidActivity == null)
{
// 获取当前的 Android Activity
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
oaidActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
}
// 调用 Android 的 GetOAID 方法
AndroidJavaObject utils = new AndroidJavaObject("com.example.oaidtest2.OAID_Activity");
utils.Call("GetIMEI", gameObject.name, "OnIMEIReceived");
}
2025-11-03 15:37:16 +08:00
// 回调方法,用于接收 OAID
private void OnOAIDReceived(string oaid)
{
Debug.Log("Received OAID: " + oaid);
}
2025-11-14 13:39:58 +08:00
// 回调方法,用于接收 IMEI
private void OnIMEIReceived(string imei)
{
Debug.Log("Received IMEI: " + imei);
}
2025-11-03 15:37:16 +08:00
}