单击活动表面以放置带有 8th Wall XR 的 GameObject?

Click on active surface to place GameObject with 8th Wall XR?

使用 8th Wall XR 是否可以点击一个表面并使其成为活动表面并在点击的位置放置一个游戏对象?有点像 ARKit,它只会在点击游戏对象后增加它。

像这样的东西应该可以解决问题。您需要一个附加了 XRSurfaceController 的游戏对象(即一个平面),并将其放在一个名为 "Surface":

的图层上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaceObject : MonoBehaviour {

  // Adjust this if the transform isn't at the bottom edge of the object
  public float heightAdjustment = 0.0f;

  // Prefab to instantiate.  If null, the script will instantiate a Cube
  public GameObject prefab;

  // Scale factor for instantiated GameObject
  public float objectScale = 1.0f;

  private GameObject myObj;

  void Update() {
    // Tap to place
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began ) {

      RaycastHit hit;
      Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
      // The "Surface" GameObject with an XRSurfaceController attached should be on layer "Surface"
      // If tap hits surface, place object on surface
      if(Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Surface"))) {
        CreateObject(new Vector3(hit.point.x, hit.point.y + heightAdjustment, hit.point.z));
      } 
    }
  }

  void CreateObject(Vector3 v) {
    // If prefab is specified, Instantiate() it, otherwise, place a Cube
    if (prefab) {
      myObj = GameObject.Instantiate(prefab);
    } else {
      myObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
    myObj.transform.position = v;
    myObj.transform.localScale = new Vector3(objectScale, objectScale, objectScale);
  }
}