PlaceOnAPlane.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.XR.ARFoundation;
  12. using Immersal.AR;
  13. namespace Immersal.Samples.ContentPlacement
  14. {
  15. public class PlaceOnAPlane : MonoBehaviour
  16. {
  17. [SerializeField]
  18. private List<GameObject> m_objects = new List<GameObject>();
  19. [SerializeField]
  20. private ARSpace m_ARSpace = null;
  21. public void Place(int index)
  22. {
  23. Transform cam = Camera.main?.transform;
  24. m_ARSpace = FindObjectOfType<ARSpace>();
  25. if (cam != null && m_objects[index] != null && m_ARSpace != null)
  26. {
  27. RaycastHit hit;
  28. Vector3 direction = cam.forward;
  29. Vector3 origin = cam.position;
  30. if (Physics.Raycast(origin, direction, out hit, Mathf.Infinity))
  31. {
  32. if (hit.collider != null)
  33. {
  34. ARPlane arPlane = hit.collider.GetComponentInParent<ARPlane>();
  35. if (arPlane)
  36. {
  37. Vector3 pos = hit.point;
  38. GameObject go = Instantiate(m_objects[index], m_ARSpace.transform);
  39. go.transform.localRotation = Quaternion.identity;
  40. go.transform.position = pos;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }