SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.5 KiB

4 years ago
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.XR.ARFoundation;
  5. [RequireComponent(typeof(ARRaycastManager))]
  6. public class PlacementWithD : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private GameObject placedPrefab;
  10. [SerializeField]
  11. private GameObject welcomePanel;
  12. [SerializeField]
  13. private Button dismissButton;
  14. [SerializeField]
  15. private Button lockButton;
  16. [SerializeField]
  17. private Camera arCamera;
  18. [SerializeField]
  19. private float defaultRotation = 0;
  20. private GameObject placedObject;
  21. private Vector2 touchPosition = default;
  22. private ARRaycastManager arRaycastManager;
  23. private bool isLocked = false;
  24. private bool onTouchHold = false;
  25. private static List<ARRaycastHit> hits = new List<ARRaycastHit>();
  26. void Awake()
  27. {
  28. arRaycastManager = GetComponent<ARRaycastManager>();
  29. dismissButton.onClick.AddListener(Dismiss);
  30. if (lockButton != null)
  31. {
  32. lockButton.onClick.AddListener(Lock);
  33. }
  34. }
  35. private void Dismiss() => welcomePanel.SetActive(false);
  36. private void Lock()
  37. {
  38. isLocked = !isLocked;
  39. lockButton.GetComponentInChildren<Text>().text = isLocked ? "Locked" : "Unlocked";
  40. if (placedObject != null)
  41. {
  42. placedObject.GetComponent<PlacementObject>()
  43. .SetOverlayText(isLocked ? "AR Object Locked" : "AR Object Unlocked");
  44. }
  45. }
  46. void Update()
  47. {
  48. // do not capture events unless the welcome panel is hidden
  49. if (welcomePanel.activeSelf)
  50. return;
  51. if (Input.touchCount > 0)
  52. {
  53. Touch touch = Input.GetTouch(0);
  54. touchPosition = touch.position;
  55. if (touch.phase == TouchPhase.Began)
  56. {
  57. Ray ray = arCamera.ScreenPointToRay(touch.position);
  58. RaycastHit hitObject;
  59. if (Physics.Raycast(ray, out hitObject))
  60. {
  61. PlacementObject placementObject = hitObject.transform.GetComponent<PlacementObject>();
  62. if (placementObject != null)
  63. {
  64. onTouchHold = isLocked ? false : true;
  65. placementObject.SetOverlayText(isLocked ? "AR Object Locked" : "AR Object Unlocked");
  66. }
  67. }
  68. }
  69. if (touch.phase == TouchPhase.Ended)
  70. {
  71. onTouchHold = false;
  72. }
  73. }
  74. if (arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon))
  75. {
  76. Pose hitPose = hits[0].pose;
  77. if (placedObject == null)
  78. {
  79. if (defaultRotation > 0)
  80. {
  81. placedObject = Instantiate(placedPrefab, hitPose.position, Quaternion.identity);
  82. placedObject.transform.Rotate(Vector3.up, defaultRotation);
  83. }
  84. else
  85. {
  86. placedObject = Instantiate(placedPrefab, hitPose.position, hitPose.rotation);
  87. }
  88. }
  89. else
  90. {
  91. if (onTouchHold)
  92. {
  93. placedObject.transform.position = hitPose.position;
  94. if (defaultRotation == 0)
  95. {
  96. placedObject.transform.rotation = hitPose.rotation;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }