浏览代码

refactored place on plane

/1.5-preview
Dan Miller 7 年前
当前提交
ccad2d66
共有 2 个文件被更改,包括 29 次插入51 次删除
  1. 5
      Assets/Scenes/SampleScene.unity
  2. 75
      Assets/Scripts/PlaceOnPlane.cs

5
Assets/Scenes/SampleScene.unity


m_Name:
m_EditorClassIdentifier:
m_LightEstimation: 0
m_TryToInstallUpdateIfNeeded: 1
m_AttemptUpdate: 1
--- !u!4 &427002220
Transform:
m_ObjectHideFlags: 0

m_Name:
m_EditorClassIdentifier:
m_Camera: {fileID: 282840813}
m_Scale: 1
--- !u!4 &1335839535
Transform:
m_ObjectHideFlags: 0

m_Script: {fileID: 11500000, guid: 7d5359d5f13250e4485741ed685a32be, type: 3}
m_Name:
m_EditorClassIdentifier:
m_PlacedPrefab: {fileID: 1934569588295450, guid: b34b6c8f1206a6844b983e23b9da0441,
PlacedPrefab: {fileID: 1934569588295450, guid: b34b6c8f1206a6844b983e23b9da0441,
type: 2}

75
Assets/Scripts/PlaceOnPlane.cs


[RequireComponent(typeof(ARSessionOrigin))]
public class PlaceOnPlane : MonoBehaviour
{
[SerializeField]
[Tooltip("Instantiates this prefab on a plane at the touch location.")]
GameObject m_PlacedPrefab;
[SerializeField] [Tooltip("Instantiates this prefab on a plane at the touch location.")]
public GameObject PlacedPrefab;
/// <summary>
/// The prefab to instantiate on touch.
/// </summary>
public GameObject placedPrefab
{
get { return m_PlacedPrefab; }
set { m_PlacedPrefab = value; }
}
/// <summary>
/// The object instantiated as a result of a successful raycast intersection with a plane.
/// </summary>
public GameObject placedObject { get; private set; }
private List<ARRaycastHit> hits;
private bool spawnedPrefab = false;
private GameObject spawnedObject;
private ARSessionOrigin sessionOrigin;
m_SessionOrigin = GetComponent<ARSessionOrigin>();
sessionOrigin = GetComponent<ARSessionOrigin>();
hits = new List<ARRaycastHit>();
if (Input.touchCount == 0)
return;
var touch = Input.GetTouch(0);
var hits = s_RaycastHits;
if (!m_SessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
return;
placementHit = hits[0];
}
ARSessionOrigin m_SessionOrigin;
ARRaycastHit m_PlacementHit;
ARRaycastHit placementHit
{
get { return m_PlacementHit; }
set
if (Input.touchCount > 0)
m_PlacementHit = value;
Touch touch = Input.GetTouch(0);
if (placedObject == null && m_PlacedPrefab != null)
if (!spawnedPrefab)
placedObject = Instantiate(m_PlacedPrefab);
if (touch.phase == TouchPhase.Began)
{
if (sessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
{
Pose hitPose = hits[0].pose;
spawnedObject = Instantiate(PlacedPrefab, hitPose.position, hitPose.rotation);
spawnedPrefab = true;
}
}
if (placedObject != null)
else
var pose = m_PlacementHit.pose;
placedObject.transform.position = pose.position;
placedObject.transform.rotation = pose.rotation;
if (sessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
{
Pose hitPose = hits[0].pose;
spawnedObject.transform.position = hitPose.position;
}
List<ARRaycastHit> s_RaycastHits = new List<ARRaycastHit>();
}
正在加载...
取消
保存