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

代码段如下

在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
public class GameMain : MonoBehaviour {
    public Text text;
    public Button Button;
    AndroidJavaObject _ajc;
	void Start () {
        _ajc = new AndroidJavaObject("com.example.myunitylib.unity");
        Button.onClick.AddListener(Touch);
    }

    public void Touch()
    {
        text.text += string.Format("已经点击!!正在获取\n");
        text.text += _ajc.Call<string>("GetID");// GetID是安卓端的代码,这里通过call arr包调用这一方法,返回一个string值,就可以获得手机的唯一验证码了
    }
}

Unity端不难...主要就是Unity->安卓的交互了

这里采用一个非常方便的方法,之前在网上看到的,比那种拖jar方便很多

首先下载Android Studio 最新版本

官网如下

https://developer.android.google.cn/

建议配置好java环境

sdk api包下载好

接着开始正式步骤

1.打开Android Studio 创建安卓项目,如下

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

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

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

新建库

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

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

2创建新的MainActivity

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

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

3代码

import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class unity {
    private Activity _unityActivity;
    unity(){
        if(null==_unityActivity){
            getActivity();
        }
    }
    /*
        获取unity项目的上下文
     */
    Activity getActivity(){
        if(null==_unityActivity){
            try{
                Class<?> classtype=Class.forName("com.unity3d.player.UnityPlayer");
                Activity activity=(Activity)classtype.getDeclaredField("currentActivity").get(classtype);
                _unityActivity=activity;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        return  _unityActivity;
    }

    /*
        call unity的方法
     */
    boolean callUnity(String gameObjectName, String functionName, String args){
        try {
            Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
            Method method =classtype.getMethod("UnitySendMessage", String.class,String.class,String.class);
            method.invoke(classtype,gameObjectName,functionName,args);
            return true;
        } catch (ClassNotFoundException e) {

        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {

        }
        return false;
    }


///获取唯一ID   返回一个String 直接接收使用就可以
    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位号码
    }


    //安卓端弹出系统Tip 可以直接调用
    public void SendTip(String congtent){
        Toast.makeText(getActivity(),congtent,Toast.LENGTH_SHORT).show();
    }
}

4打成arr文件

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

arr包位置在Output文件夹

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

5将arr包移动到unity  自创文件夹plugins中

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

完成

相关文章:

  • 2021-06-11
  • 2023-02-23
  • 2021-09-26
  • 2022-12-23
  • 2021-04-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-07
  • 2021-05-10
  • 2022-12-23
  • 2021-08-13
  • 2021-08-12
  • 2021-07-23
  • 2021-12-20
相关资源
相似解决方案