/// <summary> /// Update location marker state. /// </summary> private void _UpdateLocationMarker() { if (m_menuShown) { // ignore touch input when menu is shown, the button will handle it return; } if (Input.touchCount == 1) { // Single tap -- place new location or select existing location. Touch t = Input.GetTouch(0); Vector2 guiPosition = new Vector2(t.position.x, Screen.height - t.position.y); Camera cam = Camera.main; RaycastHit hitInfo; if (t.phase != TouchPhase.Began) { return; } if (m_selectedRect.Contains(guiPosition) || m_hideAllRect.Contains(guiPosition)) { // do nothing, the button will handle it } else if (Physics.Raycast(cam.ScreenPointToRay(t.position), out hitInfo)) { // Found a marker, select it (so long as it isn't disappearing)! GameObject tapped = hitInfo.collider.gameObject; if (!tapped.GetComponent <Animation>().isPlaying) { m_selectedMarker = tapped.GetComponent <OurPlane>(); } } else { // Place a new point at that location, clear selection m_selectedMarker = null; StartCoroutine(_WaitForDepthAndFindPlane(t.position)); // Because we may wait a small amount of time, this is a good place to play a small // animation so the user knows that their input was received. RectTransform touchEffectRectTransform = (RectTransform)Instantiate(m_prefabTouchEffect); touchEffectRectTransform.transform.SetParent(m_canvas.transform, false); Vector2 normalizedPosition = t.position; normalizedPosition.x /= Screen.width; normalizedPosition.y /= Screen.height; touchEffectRectTransform.anchorMin = touchEffectRectTransform.anchorMax = normalizedPosition; m_menuShown = true; } } }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition) { m_findPlaneWaitingForDepth = true; // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { yield break; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } Instantiate(m_prefabMarker, planeCenter, Quaternion.LookRotation(forward, up)); m_selectedMarker = null; }
/// <summary> /// Display simple GUI. /// </summary> public void OnGUI() { float scale = 4.5f; float centerX = (Screen.width / 2); float centerY = (Screen.height / 2); float menuPadding = 20; float buttonHeight = 20 * scale; float buttonSpacing = buttonHeight + menuPadding; float menuWidth = 150 * scale; float menuHeight = menuPadding * 3 + buttonSpacing * 4; float buttonWidth = menuWidth - menuPadding * 2; float menuX = centerX - (menuWidth / 2); float menuY = centerY - (menuHeight / 2); float buttonStartY = menuY + menuPadding * 3; OurPlane[] planes; if (m_currentMaterial != null) { planes = FindObjectsOfType <OurPlane> (); if (planes != null) { planes [0].setMaterial(m_currentMaterial); m_currentMaterial = null; } } if (m_menuShown) { GUI.Box(new Rect(menuX, menuY, menuWidth, menuHeight), FONT_SIZE + "Choose:</size>"); if (GUI.Button(new Rect(menuX + menuPadding, buttonStartY + buttonSpacing * 0, buttonWidth, buttonHeight), new GUIContent(FONT_SIZE + "Wheel</size>", m_iconWheel))) { print("option 1"); m_menuShown = false; m_currentMaterial = m_wheelMaterial; } if (GUI.Button(new Rect(menuX + menuPadding, buttonStartY + buttonSpacing * 1, buttonWidth, buttonHeight), new GUIContent(FONT_SIZE + "Steering Wheel</size>", m_iconSteeringWheel))) { print("option 2"); m_menuShown = false; m_currentMaterial = m_steeringWheelMaterial; } if (GUI.Button(new Rect(menuX + menuPadding, buttonStartY + buttonSpacing * 2, buttonWidth, buttonHeight), new GUIContent(FONT_SIZE + "Silencer</size>", m_iconSilencer))) { print("option 3"); m_menuShown = false; m_currentMaterial = m_silencerMaterial; } if (GUI.Button(new Rect(menuX + menuPadding, buttonStartY + buttonSpacing * 3, buttonWidth, buttonHeight), new GUIContent(FONT_SIZE + "Plug</size>", m_iconPlug))) { print("option 4"); m_menuShown = false; m_currentMaterial = m_plugMaterial; } } if (m_selectedMarker != null) { Renderer selectedRenderer = m_selectedMarker.GetComponent <Renderer>(); // GUI's Y is flipped from the mouse's Y Rect screenRect = WorldBoundsToScreen(Camera.main, selectedRenderer.bounds); float yMin = Screen.height - screenRect.yMin; float yMax = Screen.height - screenRect.yMax; screenRect.yMin = Mathf.Min(yMin, yMax); screenRect.yMax = Mathf.Max(yMin, yMax); if (GUI.Button(screenRect, FONT_SIZE + "Hide</size>")) { m_selectedMarker.SendMessage("Hide"); m_selectedMarker = null; m_selectedRect = new Rect(); } else { m_selectedRect = screenRect; } } else { m_selectedRect = new Rect(); } if (GameObject.FindObjectOfType <OurPlane>() != null) { m_hideAllRect = new Rect(Screen.width - UI_BUTTON_SIZE_X - UI_BUTTON_GAP_X, Screen.height - UI_BUTTON_SIZE_Y - UI_BUTTON_GAP_X, UI_BUTTON_SIZE_X, UI_BUTTON_SIZE_Y); if (GUI.Button(m_hideAllRect, FONT_SIZE + "Hide All</size>")) { foreach (OurPlane marker in GameObject.FindObjectsOfType <OurPlane>()) { marker.SendMessage("Hide"); } m_menuShown = false; } } else { m_hideAllRect = new Rect(0, 0, 0, 0); } }