Advertisement
Eriknem

ARmodel

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