LoginSignup
3
1

More than 5 years have passed since last update.

Unity ARkit Plugin のメモ

Posted at

環境

  • Unity 2017.2.0p4
  • Unity ARKit Plugin 1.0.14
  • macOS 10.12 Sierra
    (2018年2月)

UnityARCameraManager

  • ARKitの起動や座標の移動、回転処理を更新している。

UnityARAnchorManager

  • 認識した情報からPlaneを生成してキー情報を保持している。

■Generate Planeした平面を削除する

UnityARAnchorManagerのDestroy()を呼び出すと、認識した平面オブジェクトを削除できる(キー情報含む)。
同時にNativeInterfaceのイベントも削除される。

UnityARAnchorManager.cs
public void Destroy()
{
   foreach (ARPlaneAnchorGameObject arpag in GetCurrentPlaneAnchors() {
      GameObject.Destroy (arpag.gameObject);   //平面オブジェクト削除
   }
   planeAnchorMap.Clear();   //アンカー情報削除
   UnsubscribeEvents();      //イベント削除
}

public void UnsubscribeEvents()
{
   UnityARSessionNativeInterface.ARAnchorAddedEvent -= AddAnchor;
   UnityARSessionNativeInterface.ARAnchorUpdatedEvent -= UpdateAnchor;
   UnityARSessionNativeInterface.ARAnchorRemovedEvent -= RemoveAnchor;
}

平面オブジェクトをリセットして再配置する場合

UnsubscribeEvents()をコメントアウトしてPlane生成イベントを削除させないようにする。

■床を認識して足元の高さを取得(VR向け)

UnityARAnchorManagerのGetCurrentPlaneAnchors()に、認識した平面のオブジェクトがリスト化されている。

UnityARAnchorManager.cs
public List<ARPlaneAnchorGameObject> GetCurrentPlaneAnchors()
{
   return planeAnchorMap.Values.ToList ();
}

必要な平面のオブジェクトのY軸を利用して足元の基準を取得する。

FloorSet.cs

[SerializeField]
private GameObject objFloor;

private UnityARAnchorManager unityAnchor;

void Start()
{
   unityAnchor = new UnityARAnchorManager();
}

//ボタンなどに設定
public void OnClick()
{
   //平面が認識されている時
   if(unityAnchor.GetCurrentPlaneAnchors().Count != 0) {

      //1個目の平面オブジェクトのポジション取得
      Vector3 vFloor = unityAnchor.GetCurrentPlaneAnchors()[0].gameObject.transform.position;

      //VR空間の高さを変更する
      objFloor.transform.position = new Vector3 (0f, vFloor.y, 0f);
   }
}
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1