一、前言
上一篇Vuforia用在安卓設備中外接USB攝像頭(一)主要介紹的是純安卓上使用Vuforia+UVC Camera,本篇介紹在Unity中開發Vuforia+UVCCamera。有了上一篇配置的環境和生成的SO文件基礎,本篇纔可以順利的完成在Unity中使用Vuforia+UVCCamera。
二、開發過程
1、unity中環境搭建:首先,在unity中新建Plugins/Android/libs文件夾,然後將上一篇生成的samples\UVCDriver\build\bin\Android文件夾裏的文件都拖入到剛剛在unity中建立的libs文件夾中,最後在unity中如圖2.1所示:
2、勾選Player Settings/XR Settings的Vuforia Augmented Reality Support,如圖2.2所示unity在勾選之後會自動導入開發Vuforia相關的文
件,在unity2019.2.11中勾選完會有提示不能使用vuforia,需要刪掉Graphics APIs中的其他選項,只保留OpenGLES3,,如圖2.3所示
3、新建一個Scene,添加ARCamera和ImageTarget,搭建一個簡易的AR單圖識別場景,並配置好ARCamera中的Key
4、新建一個腳本命名爲UVCManager,該腳本爲呼出對話框設置UVC調用攝像頭的權限,並自動將驅動進行設置,以便Vuforia的引擎在初始化的時候採用的是外接的USB攝像頭,完整的代碼爲:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class UVCManager : MonoBehaviour
{
private void Awake()
{
#if UNITY_ANDROID
bool driverLibrarySet = false;
driverLibrarySet = VuforiaUnity.SetDriverLibrary("libUVCDriver.so");
if (driverLibrarySet)
{
// Load your applications scene here
// InitAndLoadScene(VUFORIA_DRIVER_CAMERA_SCENE_INDEX);
// The application needs to ask for USB permissions to run the USB camera
// this is done after the driver is loaded. We call a method in the UVC driver
// Java code to request permissions, passing in the Unity app's activity.
AndroidJavaClass unityJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityJC.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass usbControllerJC = new AndroidJavaClass("com.vuforia.samples.uvcDriver.USBController");
usbControllerJC.CallStatic("requestUSBPermission", unityActivity);
}
else
{
Debug.Log("Failed to initialize the UVC driver - defaulting to the standard scene");
// Fall back to the in-built camera
}
#endif
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
將該腳本隨便拖入到一個場景的物體中。
5、打包發佈APK
三、總結
1、使用unity2019.2.11發佈的APK可以成功使用,但是在unity2018.4.12中發佈的APK就出現初始化失敗的界面,尚還不知道具體原因是什麼;