Android App View 支持将安卓应用(包括第三方应用)以 App View 2D 或 App View 3D 的形式直接嵌入场景中。 从 Tuanjie 1.5.0 版本开始,Tuanjie Editor 中支持直接创建 Android App View 2D 和 Android App View 3D 组件,并提供了接入安卓应用的完整工作流,具体使用方法和注意事项请参考下文。
Android App View 在 Tuanjie Editor 中包含一个 Settings 和两个 Component:
Android App View 推荐使用流程如下:
打开 Tuanjie Editor 1.5.0 版本及以上;
切换至 HMI Android 平台;
勾选 Enable Android App View;
调整其余参数,导出工程或直接打包在真机运行(真机需确保已安装对应保包名的安卓应用)。
为了接入安卓应用并在安卓设备上正常运行,安卓工程中需对权限进行说明,需要在 AndroidManifest.xml 文件中增加如下内容(Tuanjie 导出工程中已自动新增,无需额外修改):
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INJECT_EVENTS" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.GET_PACKAGES" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />
android:sharedUserId="android.uid.system"
同时,需要使用系统签名,为应用设置签名可查看:Application signing
需要将接入了安卓应用的 App 设置为系统应用,因此,需要在安装和启动时按照下列操作进行设置,或编写脚本启动:
1. adb root
2. adb remount
3. adb push %apk本地路径% /system/app/appview/
4. 解压缩app拿到so
5. 如果是 ARM 64 位,则:adb push %so本地路径% /system/app/appview/lib/arm64/
6. 如果是 ARM 32 位,则:adb push %so本地路径% /system/app/appview/lib/arm/
7. adb reboot
Android App View 3D 当前不支持 New Input System,请检查 Edit >> Project Settings >> Player >> Other Settings >> Active Input Handling,确保选择的是 “Input Manager (Old)” 或 “Both”。
如果必须使用 New Input System,请添加如下脚本:
using UnityEngine;
using UnityEngine.Experimental.Android.AppView;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.UI;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
#endif
public class NewBehaviourScript : MonoBehaviour
{
public AndroidAppViewSettings settings;
private void Awake()
{
#if ENABLE_INPUT_SYSTEM
EnhancedTouchSupport.Enable();
#endif
AndroidAppViewManager.view3DTouchInputDelegate = (() =>
{
AndroidAppViewTouchRawData data = null;
#if ENABLE_INPUT_SYSTEM
if (Touch.activeTouches.Count < 1)
return null;
Touch touch = Touch.activeTouches[0];
data = new AndroidAppViewTouchRawData(touch.screenPosition, TransferTouchPhase(touch.phase), touch.displayIndex);
#endif
return data;
});
}
#if ENABLE_INPUT_SYSTEM
UnityEngine.TouchPhase TransferTouchPhase(TouchPhase phase)
{
UnityEngine.TouchPhase ret = UnityEngine.TouchPhase.Ended;
switch (phase)
{
case TouchPhase.Began:
ret = UnityEngine.TouchPhase.Began;
break;
case TouchPhase.Moved:
ret = UnityEngine.TouchPhase.Moved;
break;
case TouchPhase.Stationary:
ret = UnityEngine.TouchPhase.Stationary;
break;
case TouchPhase.Ended:
ret = UnityEngine.TouchPhase.Ended;
break;
case TouchPhase.Canceled:
ret = UnityEngine.TouchPhase.Canceled;
break;
default:
break;
}
return ret;
}
#endif
}