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(); DontDestroyOnLoad(go); } return _instance; } } // 原有方法保留 public void GetOAID() { if (oaidActivity == null) { // 获取当前的 Android Activity using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { oaidActivity = unityPlayer.GetStatic("currentActivity"); } } // 调用 Android 的 GetOAID 方法 AndroidJavaObject utils = new AndroidJavaObject("com.example.oaidtest2.OAID_Activity"); utils.Call("GetOAID", gameObject.name, "OnOAIDReceived"); } // 新增:从 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(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 // 获取当前 Android Activity 并调用插件初始化接口 try { using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { oaidActivity = unityPlayer.GetStatic("currentActivity"); } AndroidJavaObject utils = new AndroidJavaObject("com.example.oaidtest2.OAID_Activity"); try { utils.Call("SetCertContent", cert); // <- 根据 SDK 修改方法名与签名 } catch (System.Exception ex) { Debug.LogWarning("SetCertificate call failed (check method name/signature): " + ex); } // 初始化/设置证书后再请求 OAID utils.Call("GetOAID", gameObject.name, "OnOAIDReceived"); } 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 } // 回调方法,用于接收 OAID private void OnOAIDReceived(string oaid) { Debug.Log("Received OAID: " + oaid); } }