Eriknem

ARmodel

Nov 11th, 2020 (edited)
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.ARFoundation;
  5. using Unity.XR.CoreUtils;
  6. using UnityEngine.EventSystems;
  7.  
  8.  
  9. public class PlaceOnPlane : MonoBehaviour
  10. {
  11.  
  12.     private XROrigin sessionOrigin;
  13.  
  14.     private ARRaycastManager castManager;
  15.  
  16.     private List<ARRaycastHit> hits;
  17.  
  18.     public GameObject model;
  19.     public GameObject canvas;
  20.  
  21.     bool IsPointerOverUIObject(Vector2 pos)
  22.     {
  23.         if (EventSystem.current == null)
  24.             return false;
  25.         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
  26.         eventDataCurrentPosition.position = new Vector2(pos.x, pos.y);
  27.         List<RaycastResult> results = new List<RaycastResult>();
  28.         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
  29.         return results.Count > 0;
  30.     }
  31.  
  32.     void Start()
  33.  
  34.     {
  35.  
  36.         sessionOrigin = GetComponent<XROrigin>();
  37.         castManager = GetComponent<ARRaycastManager>();
  38.         hits = new List<ARRaycastHit>();
  39.  
  40.         model.SetActive(false);
  41.         canvas.SetActive(false);
  42.     }
  43.  
  44.  
  45.  
  46.     void Update()
  47.  
  48.     {
  49.  
  50.         if (Input.touchCount == 0) return;
  51.  
  52.  
  53.         Touch touch = Input.GetTouch(0);
  54.  
  55.         if (castManager.Raycast(touch.position, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon) && !IsPointerOverUIObject(touch.position))
  56.         {
  57.             Pose pose = hits[0].pose;
  58.  
  59.  
  60.             if (!model.activeInHierarchy)
  61.             {
  62.                 model.SetActive(true);
  63.                 model.transform.position = pose.position;
  64.                 model.transform.rotation = pose.rotation;
  65.  
  66.                 canvas.SetActive(true);
  67.             }
  68.             else
  69.             {
  70.                 model.transform.position = pose.position;
  71.             }
  72.         }
  73.  
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment