輕量級 Unity3D 設備唯一標識符獲取插件,支援 Android 和 iOS 平台。提供統一的 C# API 存取 OAID、IDFA、IMEI 和穩定的硬體指紋,Android 端零原生依賴。
AndroidJavaClass / AndroidJavaObject(JNI)直接呼叫系統 API 和廠商 SDK,無需 Java 程式碼、JAR 檔案或 Gradle 配置。SystemInfo.deviceUniqueIdentifier。ASIdentifierManager 獲取 IDFA 並支援 ATT 授權;設備 ID 持久化到 Keychain,應用重裝後依然有效。PlayerPrefs 快取結果,避免重複查詢系統介面。| API | 說明 | 返回值 |
|---|---|---|
DeviceGetOaid | 獲取設備 OAID(Android 獨有) | OAID 原始值(去 -,最長 32 位) |
DeviceGetIdfa | 獲取設備 IDFA(iOS 獨有) | IDFA 原始值(去 -,最長 32 位) |
DeviceGetImei | 獲取設備 IMEI | IMEI 原始值(去 -,最長 32 位) |
DeviceUniqueIdentifier | 獲取設備唯一機器碼 | MD5 雜湊值(32 位十六進制字串) |
所有 API 均通過 PlayerPrefs 快取結果,首次獲取後不再重複呼叫系統介面。
| 平台 | DeviceGetOaid | DeviceGetIdfa | DeviceGetImei | DeviceUniqueIdentifier |
|---|---|---|---|---|
| Android | JNI 反射獲取(MSA / 華為 / 小米 / OPPO / vivo / 三星) | SystemInfo.deviceUniqueIdentifier(降級) | JNI 呼叫 TelephonyManager | IMEI + 硬體資訊 + Android ID + WLAN MAC + BT MAC 的 MD5 |
| iOS | SystemInfo.deviceUniqueIdentifier(降級) | ASIdentifierManager.advertisingIdentifier | native __DeviceGetIMEI() (IDFV) | native DeviceUniqueId() (SSKeychain) |
| Editor / 其他 | SystemInfo.deviceUniqueIdentifier | SystemInfo.deviceUniqueIdentifier | SystemInfo.deviceUniqueIdentifier | SystemInfo.deviceUniqueIdentifier |
本插件不強制要求任何權限,無權限時降級運行不會崩潰。插件不自帶 AndroidManifest.xml,以下權限需由使用方專案在自己的 AndroidManifest.xml 中按需聲明。
| 權限 | 提升 DeviceUniqueIdentifier 唯一性 | 提升 DeviceGetImei 唯一性 |
|---|---|---|
READ_PHONE_STATE | IMEI 參與雜湊計算 | 可獲取真實 IMEI |
ACCESS_WIFI_STATE | WLAN MAC 參與雜湊計算 | - |
BLUETOOTH | 藍牙 MAC 參與雜湊計算 | - |
<!-- 在使用方專案的 AndroidManifest.xml 中按需添加 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
| 配置 | 用途 |
|---|---|
NSUserTrackingUsageDescription | IDFA 需要的 ATT 授權描述 |
<!-- 在使用方專案的 Info.plist 中添加 -->
<key>NSUserTrackingUsageDescription</key>
<string>您的廣告標識符將用於提供更好的服務</string>
using GameFrameX.SystemInfo.Runtime;
// 獲取設備 OAID(Android 獨有,iOS/Editor 降級為 SystemInfo.deviceUniqueIdentifier)
string oaid = BlankDeviceUniqueIdentifier.DeviceGetOaid;
// 獲取設備 IDFA(iOS 獨有,Android/Editor 降級為 SystemInfo.deviceUniqueIdentifier)
string idfa = BlankDeviceUniqueIdentifier.DeviceGetIdfa;
// 獲取設備 IMEI
string imei = BlankDeviceUniqueIdentifier.DeviceGetImei;
// 獲取設備唯一標識符
string deviceId = BlankDeviceUniqueIdentifier.DeviceUniqueIdentifier;
IDFA 需要使用者授權 ATT(App Tracking Transparency)。使用前需在 Info.plist 中添加:
<key>NSUserTrackingUsageDescription</key>
<string>您的廣告標識符將用於提供更好的服務</string>
並在呼叫 DeviceGetIdfa 前請求授權:
#if UNITY_IOS || UNITY_IPHONE
// iOS 14+ 需要先請求 ATT 授權
if (UnityEngine.iOS.Device.systemVersion.CompareTo("14") >= 0)
{
UnityEngine.iOS.Device.RequestUserAuthorization(UnityEngine.iOS.UserTracking.Authorization);
}
#endif
string idfa = BlankDeviceUniqueIdentifier.DeviceGetIdfa;
未授權時 DeviceGetIdfa 返回空串,不會崩潰。
Plugins/
iOS/
BlankDeviceUniqueIdentifier/
AHDeviceUniqueIdentifier.h # iOS native 標頭檔
AHDeviceUniqueIdentifier.mm # iOS native 實作
SSKeychain.h # SSKeychain 鑰匙串工具
SSKeychain.m
Runtime/
BlankDeviceUniqueIdentifier.cs # C# 統一介面(Android 透過 JNI 直接呼叫系統 API,無需 Java/JAR)
Android 端透過
AndroidJavaClass/AndroidJavaObject直接呼叫系統 API 和廠商 SDK,無需編譯和引入任何 Java 程式碼或 JAR 檔案。
本專案基於 Apache License 2.0 開源。
Copyright 2023 ALianBlank (alianblank@outlook.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.