Created
February 5, 2020 23:55
-
-
Save dilmerv/3ae6b517a2f548968be51ed7bbad6b1a to your computer and use it in GitHub Desktop.
Enforcing one object selection and scaling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
using UnityEngine.XR.ARFoundation; | |
[RequireComponent(typeof(ARRaycastManager))] | |
public class PlacementWithManySelectionWithScaleController : MonoBehaviour | |
{ | |
[SerializeField] | |
private GameObject placedPrefab; | |
[SerializeField] | |
private GameObject welcomePanel; | |
[SerializeField] | |
private Button dismissButton; | |
[SerializeField] | |
private bool applyScalingPerObject = false; | |
[SerializeField] | |
private Slider scaleSlider; | |
[SerializeField] | |
private Text scaleTextValue; | |
[SerializeField] | |
private Button toggleOptionsButton; | |
[SerializeField] | |
private GameObject options; | |
[SerializeField] | |
private Camera arCamera; | |
private bool selectedObjectAdded; | |
private GameObject placedObject; | |
private Vector2 touchPosition = default; | |
private ARRaycastManager arRaycastManager; | |
private ARSessionOrigin aRSessionOrigin; | |
private bool onTouchHold = false; | |
private static List<ARRaycastHit> hits = new List<ARRaycastHit>(); | |
private PlacementObject lastSelectedObject; | |
private GameObject PlacedPrefab | |
{ | |
get | |
{ | |
return placedPrefab; | |
} | |
set | |
{ | |
placedPrefab = value; | |
} | |
} | |
void Awake() | |
{ | |
arRaycastManager = GetComponent<ARRaycastManager>(); | |
aRSessionOrigin = GetComponent<ARSessionOrigin>(); | |
dismissButton.onClick.AddListener(Dismiss); | |
scaleSlider.onValueChanged.AddListener(ScaleChanged); | |
toggleOptionsButton.onClick.AddListener(ToggleOptions); | |
} | |
private void ToggleOptions() | |
{ | |
if(options.activeSelf) | |
{ | |
toggleOptionsButton.GetComponentInChildren<Text>().text = "O"; | |
options.SetActive(false); | |
} | |
else | |
{ | |
toggleOptionsButton.GetComponentInChildren<Text>().text = "X"; | |
options.SetActive(true); | |
} | |
} | |
private void Dismiss() => welcomePanel.SetActive(false); | |
private void ScaleChanged(float newValue) | |
{ | |
if(applyScalingPerObject){ | |
if(lastSelectedObject != null && lastSelectedObject.Selected) | |
{ | |
lastSelectedObject.transform.parent.localScale = Vector3.one * newValue; | |
} | |
} | |
else | |
aRSessionOrigin.transform.localScale = Vector3.one * newValue; | |
scaleTextValue.text = $"Scale {newValue}"; | |
} | |
void Update() | |
{ | |
// do not capture events unless the welcome panel is hidden or if UI is selected | |
if(welcomePanel.activeSelf || options.activeSelf) | |
return; | |
if(Input.touchCount > 0) | |
{ | |
Touch touch = Input.GetTouch(0); | |
if(EventSystem.current.IsPointerOverGameObject(touch.fingerId)) | |
return; | |
touchPosition = touch.position; | |
if(touch.phase == TouchPhase.Began) | |
{ | |
Ray ray = arCamera.ScreenPointToRay(touch.position); | |
RaycastHit hitObject; | |
if(Physics.Raycast(ray, out hitObject)) | |
{ | |
lastSelectedObject = hitObject.transform.GetComponent<PlacementObject>(); | |
if(lastSelectedObject != null) | |
{ | |
PlacementObject[] allOtherObjects = FindObjectsOfType<PlacementObject>(); | |
foreach(PlacementObject placementObject in allOtherObjects) | |
{ | |
if(placementObject != lastSelectedObject){ | |
placementObject.Selected = false; | |
} | |
else | |
placementObject.Selected = true; | |
} | |
} | |
} | |
if(arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon)) | |
{ | |
Pose hitPose = hits[0].pose; | |
if(lastSelectedObject == null && !selectedObjectAdded) | |
{ | |
lastSelectedObject = Instantiate(placedPrefab, hitPose.position, hitPose.rotation).GetComponent<PlacementObject>(); | |
selectedObjectAdded = true; | |
} | |
} | |
} | |
if(touch.phase == TouchPhase.Moved) | |
{ | |
if(arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon)) | |
{ | |
Pose hitPose = hits[0].pose; | |
if(lastSelectedObject != null && lastSelectedObject.Selected) | |
{ | |
lastSelectedObject.transform.parent.position = hitPose.position; | |
lastSelectedObject.transform.parent.rotation = hitPose.rotation; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment