UE5.3 虚幻引擎 安卓Android地图插件开发打包

I'm trying to develop Unreal Engine 4 plugin for Android camera API 2.

As I could read on unreal engine forums, there two possibilities to make a plugin for Android.

The first, consist on modifying the UE Android base project (GameActivity).

The second is a standalone plugin, which brings portability to add the plugins in any project.

According to this post, it is possible to make a camera Api1 standalone plugin, which uses APL.xml file to add java code.

But I think it is very limited to a Game activity, like the code below.

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--ARToolKit plugin additions-->
<root xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- init section is always evaluated once per architecture -->
 <init>
   <log text="AndroidCamera init"/>
 </init>

 <androidManifestUpdates>
   <addPermission android:name="android.permission.CAMERA"/>
   <addFeature android:name="android.hardware.camera"/>
   <addFeature android:name="android.hardware.camera.autofocus"/>
 </androidManifestUpdates>

 <!-- optional additions to the GameActivity imports in GameActivity.java -->
 <gameActivityImportAdditions>
   <insert>
import android.widget.Toast;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.graphics.SurfaceTexture;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import java.util.List; 
import java.io.IOException;
import android.util.Log;
   </insert>
 </gameActivityImportAdditions>

 <gameActivityClassAdditions>
   <insert>
     static String msg = "yes i am a rock!";
     SurfaceTexture surfaceTexture;
     Camera camera;
     public native boolean nativeGetFrameData(int frameWidth, int frameHeight, byte[] data);

     public void AndroidThunkJava_Toast()
     {
       try
       {
         _activity.runOnUiThread(new Runnable()
         {
           public void run()
           {
             Toast.makeText(_activity.getApplicationContext(), "cam o yeah!", Toast.LENGTH_SHORT).show();
           }
         });
       }
       catch (Exception e)
       {
         Log.debug("Toast failed with exception " + e.getMessage());
       }
     }

     public void AndroidThunkJava_startCamera()
     {
       surfaceTexture = new SurfaceTexture(10);
       surfaceTexture.setDefaultBufferSize(320,240);
       camera = Camera.open();


       try { 
         camera.setPreviewTexture(surfaceTexture);
       } catch (IOException t) {
         android.util.Log.e("ARToolKitLog", "Cannot set preview texture target!", t);
       } 

       Parameters cameraParam = camera.getParameters();

       cameraParam.setPreviewFormat(ImageFormat.NV21);
       cameraParam.setPreviewSize(320, 240);
       camera.setParameters(cameraParam);

       camera.setPreviewCallback(new PreviewCallback() {
         @Override
         public void onPreviewFrame(byte[] data, Camera camera) {
           int Height = camera.getParameters().getPreviewSize().height;
           int Width = camera.getParameters().getPreviewSize().width;
           nativeGetFrameData(Width, Height, data);
         }
       });

       camera.startPreview();
     }

     public void AndroidThunkJava_stopCamera()
     {
       if (camera != null) 
       {
         camera.stopPreview();
         camera.release();
         camera = null;
       }
     }
   </insert>
 </gameActivityClassAdditions>

 <!-- optional additions to GameActivity onCreate in GameActivity.java -->
 <gameActivityOnCreateAdditions>
   <insert>
   //Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
   //AndroidThunkJava_Toast();
   </insert>
 </gameActivityOnCreateAdditions>


</root>

So my questions are:

Is this the only way to make a standalone plugin for Unreal Engine 4?

Is there a XML tag to add custom classes in this APL files?

I found another way to use custom Java classes:

If I am not wrong, the unreal engine _APL.xml file is connected with ANT build system.

So, as in _APL.xml files in addition to define Java code in gameActivityClassAdditions, there is a tag to copy our .java files to Unreal Engine build directory.

I have taken the idea from this Unreal Engine plugin: https://github.com/jeevcat/GoogleMapsUE4Plugin

So, I made a plugin in 4 steps:

  1. Copy the custom java classes to the plugin source directory with package folder order, so the resultant folder structure should be something like this.

2.- Add the prebuilt copies tag to apply the Java class into the build directory:

复制代码
<prebuildCopies>
        <copyDir src="$S(PluginDir)/Java" dst="$S(BuildDir)" />
</prebuildCopies>

3.- Add the imports in gameActivityImportAdditions:

复制代码
import org.samples.camera2.CameraHandler;

4.- use custom class

复制代码
           public void AndroidThunkJava_startCamera()
            {
                        m_camHandler = new CameraHandler();
                        m_camHandler.setCallback(new CameraCallback() {
                        @Override
                        public void onGetFrame(byte[] data, int width, int height) {
                            Log.d(LOG_TAG,"MY CUSTOM CALLBACK"+width);
                            nativeGetFrameData(width, height, data);
                        }
            });
            m_camHandler.init(_activity, 0, 320, 240);
            m_camHandler.start();
            }

Finally I show the resultant _APL.xml file:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--ARToolKit plugin additions-->
<root xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- init section is always evaluated once per architecture -->
    <init>
        <log text="AndroidCamera init"/>
    </init>

<androidManifestUpdates>
        <addPermission android:name="android.permission.CAMERA"  />
        <addFeature android:name="android.hardware.camera"  />
        <addFeature android:name="android.hardware.camera.autofocus"  />
        <addFeature android:name="android.hardware.camera2" />
</androidManifestUpdates>

<prebuildCopies>
        <copyDir src="$S(PluginDir)/Java" dst="$S(BuildDir)" />
</prebuildCopies>

    <!-- optional additions to the GameActivity imports in GameActivity.java -->
    <gameActivityImportAdditions>
        <insert>
import android.widget.Toast;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.graphics.SurfaceTexture;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import java.util.List;
import java.io.IOException;
import android.util.Log;

import org.samples.camera2.CameraHandler;
相关推荐
恋猫de小郭3 小时前
腾讯 Kuikly 正式开源,了解一下这个基于 Kotlin 的全平台框架
android·前端·ios
贫道绝缘子3 小时前
【Android】四大组件之Activity
android
人生游戏牛马NPC1号4 小时前
学习Android(四)
android·kotlin
_祝你今天愉快4 小时前
安卓触摸事件分发机制分析
android
fyr897574 小时前
Ubuntu 下编译goldfish内核并使用模拟器运行
android·linux
心之所向,自强不息4 小时前
关于Android Studio的Gradle各项配置
android·ide·gradle·android studio
隐-梵4 小时前
Android studio学习之路(八)---Fragment碎片化页面的使用
android·学习·android studio
百锦再4 小时前
Kotlin学习基础知识大全(上)
android·xml·学习·微信·kotlin·studio·mobile
前期后期5 小时前
Android 智能家居开发:串口是什么,为什么android版本都比较低?粘包半包的原因以及处理思路,缓冲区处理,以及超时清空缓冲区....
android·智能家居
Wgllss5 小时前
按需下载!!全动态插件化框架WXDynamicPlugin解析怎么支持的
android·架构·android jetpack