当前位置:   article > 正文

Unity中开发Android调用Android及一系列的交互_unity安卓 new handler.post runonui

unity安卓 new handler.post runonui

笔者最近开发Unity,U3D调用android的开发,在其中遇到了许许多多的坑,接下来我会把坑都写出来,以及解决坑的一些办法,这里先起个头,如果你也在开发遇到了了坑可以留言一起交流。待我抽出时间整理出一个DEMO。

U3D调用android调用android还需要一个重要角色那就是classes.jar包,这个是unity与Android开发的一个中间桥梁:

需要导入到Lib中并引用,网上有说要类继承UnityPlayerActivity这个,笔者没有继承,可成功。

如果你有安装Unity,可按照这个路径去找:(Windos) C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin\classes.jar
 
(Mac ) Unity.app(show packages)Applications\Unity(rightclick ShowPackageContent)PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes

如果是把Android打包未arr或者jar,已打好的放入unity前需要做很重要的一步:

下载压缩工具

通过压缩工具打开我们的jar或者AAR就是上图这样,这里 的classesb不用管。

打开左侧lib里面还是会有一个classes,这里的classes需要删除,否则导入unity,unity在打包时就会出现冲突。

压缩工具随后一起上传到CSDN,不会设置积分,放心下载。

Unity调用代码:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class aarTest : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. AndroidJavaObject m_java;
  9. public Text text;
  10. private void Awake()
  11. {
  12. m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");//com.u3d.com.unityAndroid.Views.Unity1Android 包名加类名,不能错哦
  13. }
  14. void Start()
  15. {
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. }
  21. public void ArrTest1()
  22. {
  23. m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");
  24. text.text = "开始调用方法";
  25. text.text += "\n + " + m_java;
  26. m_java.Call("showView");//showView方法名
  27. text.text += "\n 调用结束";
  28. }
  29. }

Android部分代码:

  1. package com.u3d.com.unityAndroid.Views;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.text.TextUtils;
  12. import android.util.DisplayMetrics;
  13. import android.util.Log;
  14. import android.view.Gravity;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.Button;
  18. import android.widget.FrameLayout;
  19. import android.widget.ImageView;
  20. import android.widget.RelativeLayout;
  21. import android.widget.TextView;
  22. import com.u3d.com.lib.R;
  23. import com.u3d.com.unityAndroid.AdListener.OnListener;
  24. import com.u3d.com.unityAndroid.bean.ResultData;
  25. import com.u3d.com.unityAndroid.bean.ResultVedioDataBean;
  26. import com.u3d.com.unityAndroid.config.Api;
  27. import com.u3d.com.unityAndroid.utils.AdOnClick;
  28. import com.u3d.com.unityAndroid.utils.GetImgUtil;
  29. import com.u3d.com.unityAndroid.utils.HttpUtils;
  30. import com.u3d.com.unityAndroid.utils.Json;
  31. import org.json.JSONArray;
  32. import org.json.JSONException;
  33. import org.json.JSONObject;
  34. import java.io.IOException;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import okhttp3.OkHttpClient;
  38. import okhttp3.Request;
  39. import okhttp3.RequestBody;
  40. import okhttp3.Response;
  41. /**
  42. * Created by Admin on 2020/4/9.
  43. */
  44. public class Unity1Android {
  45. public static Activity activity = null;
  46. private static final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_PHONE_STATE"};
  47. protected static String UA = "";
  48. private Activity _unityActivity;
  49. HttpUtils httpUtils = new HttpUtils();
  50. Activity getDiActivity() {
  51. try {
  52. Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
  53. Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
  54. return activity;
  55. } catch (ClassNotFoundException classNotFoundException) {
  56. } catch (IllegalAccessException illegalAccessException) {
  57. } catch (NoSuchFieldException noSuchFieldException) {
  58. }
  59. return null;
  60. }
  61. /**
  62. * 测试
  63. */
  64. public void showView() {
  65. final Activity curActivity = getDiActivity();
  66. (new Handler(curActivity.getMainLooper())).post(new Runnable() {
  67. @SuppressLint("ResourceType")
  68. public void run() {
  69. TextView textView = new TextView((Context) curActivity);
  70. textView.setText("123");
  71. textView.setWidth(-1);
  72. textView.setHeight(-1);
  73. textView.setBackgroundColor(curActivity.getResources().getColor(17170459));
  74. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(400, 600);
  75. layoutParams.gravity = 17;
  76. ViewGroup totalView = (ViewGroup) curActivity.getWindow().getDecorView();
  77. totalView.addView((View) textView, (ViewGroup.LayoutParams) layoutParams);
  78. }
  79. });
  80. }
  81. }

其中这个特别重要:

  1. Activity getDiActivity() {
  2. try {
  3. Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
  4. Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
  5. return activity;
  6. } catch (ClassNotFoundException classNotFoundException) {
  7. } catch (IllegalAccessException illegalAccessException) {
  8. } catch (NoSuchFieldException noSuchFieldException) {
  9. }
  10. return null;
  11. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/118911
推荐阅读
相关标签
  

闽ICP备14008679号