---
# System prepended metadata

title: RealSense Android

---

# RealSense Android

官方 unitypackage (我是使用 v2.54.1): https://github.com/IntelRealSense/librealsense/releases

這裡假設你已經匯入好官方提供的 unitypackage

## 更改 Project Settings


### Target API

官方文件寫 Target API 必須要 <= 27，這裡就設成 27 吧

![](https://hackmd.io/_uploads/SJAobmLo2.png)


可以 build 看看，如果提示沒有相應的 android SDK 版本，檢查 `C:\Program Files\Unity\Hub\Editor\{你的UNITY版本}\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms`是不是有 `android-27`
如果沒有，你可以選擇[使用 platform-tools](https://developer.android.com/studio/releases/platform-tools?hl=zh-tw)、或是從 Android Studio 的 SDK Manager 去載
或是更簡單[直接載我的](https://gofile.io/d/kgOJM7)

![](https://hackmd.io/_uploads/rJUp-QUon.png)

### Scripting Backend

另外 Scripting Backend 須為 mono，**不能**是 IL2CPP

![](https://hackmd.io/_uploads/r1xZ84Ij2.png)


## Rebuild AssetBundle



更改 `Assets\RealSenseSDK2.0\Misc\Editor\CreateAssetBundles.cs`

```csharp=15
// #if UNITY_EDITOR_LINUX
//         BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneLinux64);
// #else
//         BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
// #endif
            BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);

```

回 Unity 按 Assets > Build AssetBundles

![](https://hackmd.io/_uploads/ry_p7mUo2.png)


## Android 權限與服務啟動

新增以下腳本 `AndroidPermission` 並放進啟動場景任一 GameObject：

```csharp=
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Android;

public class AndroidPermission : MonoBehaviour
{


#if UNITY_ANDROID && !UNITY_EDITOR
    void Awake()
    {
        if (!UnityEngine.Android.Permission.HasUserAuthorizedPermission(UnityEngine.Android.Permission.Camera))
        {
            UnityEngine.Android.Permission.RequestUserPermission(UnityEngine.Android.Permission.Camera);

        }

        using (var javaUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        using (var currentActivity = javaUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
        using (var rsContext = new AndroidJavaClass("com.intel.realsense.librealsense.RsContext"))
        {
            Debug.Log(rsContext);
            rsContext.CallStatic("init", currentActivity);
        }
    }
#endif

    
}
```


###### Reference
Oculus Quest - Intel RealSense Integration
- https://github.com/GeorgeAdamon/quest-realsense
