using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class ModelTouchInteraction : MonoBehaviour { private Touch touch; private GameObject touched; private float clickTime; public ModelExplorerController controller; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { CheckTouch(); } private void CheckTouch() { #if UNITY_IPHONE || UNITY_ANDROID if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return; #else if (EventSystem.current.IsPointerOverGameObject()) return; #endif if (CheckPointerDown()) { clickTime = Time.time; Ray ray = Camera.main.ScreenPointToRay(GetPointerPosition()); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { touched = hit.transform.gameObject; } else { touched = null; } } else if (CheckPointerUp()) { float clickSpeed = Time.time - clickTime; if (clickSpeed < 0.25f) { Ray ray = Camera.main.ScreenPointToRay(GetPointerPosition()); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (hit.transform.gameObject == touched) { if (touched.GetComponent() != null) touched.GetComponent().SetHighlighted(); } } else if (touched == null && !EventSystem.current.IsPointerOverGameObject()) { if(controller && controller.Selected) controller.Selected.GetComponent().RemoveHighlighting(); } } } } private bool CheckPointerDown() { #if UNITY_IPHONE || UNITY_ANDROID if (Input.touchCount > 0) { touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) return true; } #else if (Input.GetMouseButtonDown(0)) return true; #endif return false; } private bool CheckPointerUp() { #if UNITY_IPHONE || UNITY_ANDROID if (Input.touchCount > 0) { touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Ended) return true; } #else if (Input.GetMouseButtonUp(0)) return true; #endif return false; } private Vector3 GetPointerPosition() { #if UNITY_IPHONE || UNITY_ANDROID return Input.GetTouch(0).position; #else return Input.mousePosition; #endif } }