[Unity][安卓]unity获取唯一ID,游客登陆

本文介绍了如何在Unity中通过Android Studio创建一个库项目,获取安卓设备的唯一ID,并实现游客登录功能。具体步骤包括创建Android项目,编写Java代码,打包为ARR文件,最后将ARR导入Unity的plugins目录进行调用。
摘要由CSDN通过智能技术生成

之前做的时候在网上找了好多有现在整理下一个可以用的

代码段如下

在java中编写

public String GetID(){

        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length()%10+ Build.BRAND.length()%10 +

                Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +

                Build.DISPLAY.length()%10 + Build.HOST.length()%10 +

                Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +

                Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +

                Build.TAGS.length()%10 + Build.TYPE.length()%10 +

                Build.USER.length()%10 ; //13 位
        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
            return  new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        //使用硬件信息拼凑出来的15位号码
    }

这段代码在unity中Call   arr包,然后直接调用就可以使用了.

详细流程如下:

Unity端代码

//Demo
pub
除了使用SystemInfo.deviceUniqueIdentifier获取Android ID外,还可以使用其他方法获取唯一ID。以下是一些常用的方法: 1. 使用TelephonyManager获取IMEI号 ```csharp using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; using System; #if UNITY_ANDROID using UnityEngine.Android; #endif public class AndroidDeviceID : MonoBehaviour { public static string GetDeviceID() { string IMEI = ""; #if UNITY_ANDROID AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity").Call<AndroidJavaObject>("getApplicationContext"); AndroidJavaObject tm = context.Call<AndroidJavaObject>("getSystemService", "phone"); if (tm != null) { IMEI = tm.Call<string>("getDeviceId"); } #endif return IMEI; } } ``` 2. 使用Secure.ANDROID_ID获取设备ID ```csharp using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; using System; public class AndroidDeviceID : MonoBehaviour { public static string GetDeviceID() { string androidID = ""; #if UNITY_ANDROID androidID = Android.Provider.Settings.Secure.GetString( context.ContentResolver, Android.Provider.Settings.Secure.AndroidId); #endif return androidID; } } ``` 需要注意的是,使用IMEI号获取设备ID需要动态获取READ_PHONE_STATE权限。而使用ANDROID_ID获取设备ID则不需要权限,但可能会存在相同的ID和空ID的情况。因此,需要根据具体需求选择合适的方法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值