• unity调用安卓arr方法


    1、新建empty activity

    2、新建new module,选Android libarary,新建新的java class

    3、获得android中activity的方法:

    安卓端:

    public static 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;
    }
    unity端:
    AndroidJavaObject activity = new AndroidJavaClass("com.example.mainlibrary.unity").GetStatic<AndroidJavaObject>("_unityActivity");
    (包名为com.example.mainlibrary,class名为unity时)
    4、使用android端的方法
    安卓端:
    ///获取唯一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位号码
    }

    public static ClipboardManager clipboard = null;

    // 向剪贴板中添加文本
    public void copyTextToClipboard(final Context activity, final String str) throws Exception {
    clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);
    ClipData textCd = ClipData.newPlainText("data", str);
    clipboard.setPrimaryClip(textCd);
    }

    unity端:
    AndroidJavaObject _ajc;

    _ajc = new AndroidJavaObject("com.example.mainlibrary.unity");

     uiText.text += _ajc.Call<string>("GetID");

     _ajc.Call("copyTextToClipboard", activity, input);

    5、打包安卓端arr(build->make module)放到unity端的plugins/Android文件夹中

    完整代码

    package com.example.mainlibrary;

    import android.content.ClipData;
    import android.content.ClipDescription;
    import android.content.ClipboardManager;
    import android.content.Context;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Build;
    import android.app.Activity;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.widget.Toast;

    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.UUID;

    public class unity{

    public static 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位号码
    }

    public String GetXX(){

    return "xxxx";

    }

    public static ClipboardManager clipboard = null;

    // 向剪贴板中添加文本
    public void copyTextToClipboard(final Context activity, final String str) throws Exception {
    clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);
    ClipData textCd = ClipData.newPlainText("data", str);
    clipboard.setPrimaryClip(textCd);
    }

    // 从剪贴板中获取文本
    public String getTextFromClipboard() {
    if (clipboard != null && clipboard.hasPrimaryClip()
    && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
    ClipData cdText = clipboard.getPrimaryClip();
    ClipData.Item item = cdText.getItemAt(0);
    return item.getText().toString();
    }
    return "null";
    }



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

    }


    public Text uiText;

    AndroidJavaObject _ajc;
    void Start()
    {
    #if UNITY_ANDROID
    _ajc = new AndroidJavaObject("com.example.mainlibrary.unity");
    uiText.text += string.Format("已经点击!!正在获取 ");
    uiText.text += _ajc.Call<string>("GetID");// GetID是安卓端的代码,这里通过call arr包调用这一方法,返回一个string值,就可以获得手机的唯一验证码了
    uiText.text += " ";
    // uiText.text += _ajc.Call<string>("getMacAddress");// GetID是安卓端的代码,这里通过call arr包调用这一方法,返回一个string值,就可以获得手机的唯一验证码了
    string input = "aa";
    uiText.text += _ajc.Call<string>("GetXX");
    uiText.text += " ";

    AndroidJavaObject androidObject = new AndroidJavaObject("com.example.mainlibrary.unity");
    uiText.text += input;
    AndroidJavaObject activity = new AndroidJavaClass("com.example.mainlibrary.unity").GetStatic<AndroidJavaObject>("_unityActivity");//("currentActivity");
    if (activity == null)
    return;
    uiText.text += "5";
    // 复制到剪贴板
    androidObject.Call("copyTextToClipboard", activity, input);
    uiText.text += "6";
    // 从剪贴板中获取文本
    string text = androidObject.Call<string>("getTextFromClipboard");
    uiText.text += text;
    #endif
    }

  • 相关阅读:
    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
    LeetCode 15 3Sum [sort] <c++>
    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
    将博客搬至CSDN
    leetcode
    (转载) 图像领域常用资源
    Unity3D 之 C# 脚本
    Kinect 人机交互开发实践
  • 原文地址:https://www.cnblogs.com/tqvdong/p/15160197.html
Copyright © 2020-2023  润新知