赞
踩
笔者最近开发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调用代码:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class aarTest : MonoBehaviour
- {
- // Start is called before the first frame update
- AndroidJavaObject m_java;
- public Text text;
- private void Awake()
- {
- m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");//com.u3d.com.unityAndroid.Views.Unity1Android 包名加类名,不能错哦
- }
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- public void ArrTest1()
- {
- m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");
- text.text = "开始调用方法";
- text.text += "\n + " + m_java;
- m_java.Call("showView");//showView方法名
- text.text += "\n 调用结束";
-
- }
-
-
- }
Android部分代码:
- package com.u3d.com.unityAndroid.Views;
-
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.TextUtils;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
-
- import com.u3d.com.lib.R;
-
- import com.u3d.com.unityAndroid.AdListener.OnListener;
- import com.u3d.com.unityAndroid.bean.ResultData;
- import com.u3d.com.unityAndroid.bean.ResultVedioDataBean;
- import com.u3d.com.unityAndroid.config.Api;
- import com.u3d.com.unityAndroid.utils.AdOnClick;
- import com.u3d.com.unityAndroid.utils.GetImgUtil;
- import com.u3d.com.unityAndroid.utils.HttpUtils;
- import com.u3d.com.unityAndroid.utils.Json;
-
-
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
-
- /**
- * Created by Admin on 2020/4/9.
- */
- public class Unity1Android {
- public static Activity activity = null;
-
- private static final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_PHONE_STATE"};
- protected static String UA = "";
- private Activity _unityActivity;
-
-
-
- HttpUtils httpUtils = new HttpUtils();
-
- Activity getDiActivity() {
- try {
- Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
- Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
- return activity;
- } catch (ClassNotFoundException classNotFoundException) {
-
- } catch (IllegalAccessException illegalAccessException) {
-
- } catch (NoSuchFieldException noSuchFieldException) {
- }
-
- return null;
- }
-
-
-
-
- /**
- * 测试
- */
- public void showView() {
- final Activity curActivity = getDiActivity();
- (new Handler(curActivity.getMainLooper())).post(new Runnable() {
- @SuppressLint("ResourceType")
- public void run() {
- TextView textView = new TextView((Context) curActivity);
- textView.setText("123");
- textView.setWidth(-1);
- textView.setHeight(-1);
- textView.setBackgroundColor(curActivity.getResources().getColor(17170459));
- FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(400, 600);
- layoutParams.gravity = 17;
- ViewGroup totalView = (ViewGroup) curActivity.getWindow().getDecorView();
- totalView.addView((View) textView, (ViewGroup.LayoutParams) layoutParams);
- }
- });
- }
-
-
-
-
- }
其中这个特别重要:
- Activity getDiActivity() {
- try {
- Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
- Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
- return activity;
- } catch (ClassNotFoundException classNotFoundException) {
-
- } catch (IllegalAccessException illegalAccessException) {
-
- } catch (NoSuchFieldException noSuchFieldException) {
- }
-
- return null;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。