private void KeyWordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args) { var cmd = args.text; if (cmd == HidePlaneCmd) { Plane.SetActive(false); } else if (cmd == ShootCmd) { Cannon.Shoot(); } else if (cmd == ResetSceneCmd) { SceneManager.LoadSceneAsync(0, LoadSceneMode.Single); } }
/// <summary> /// The Unity Update() method. /// </summary> public void Update() { _QuitOnConnectionErrors(); // The tracking state must be FrameTrackingState.Tracking in order to access the Frame. if (Frame.TrackingState != FrameTrackingState.Tracking) { const int LOST_TRACKING_SLEEP_TIMEOUT = 15; Screen.sleepTimeout = LOST_TRACKING_SLEEP_TIMEOUT; return; } Screen.sleepTimeout = SleepTimeout.NeverSleep; Frame.GetNewPlanes(ref m_newPlanes); // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them. for (int i = 0; i < m_newPlanes.Count; i++) { // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to // the origin with an identity rotation since the mesh for our prefab is updated in Unity World // coordinates. GameObject planeObject = Instantiate(m_trackedPlanePrefab, Vector3.zero, Quaternion.identity, transform); planeObject.GetComponent <TrackedPlaneVisualizer>().SetTrackedPlane(m_newPlanes[i]); // Apply a random color and grid rotation. planeObject.GetComponent <Renderer>().material.SetColor("_GridColor", m_planeColors[Random.Range(0, m_planeColors.Length - 1)]); planeObject.GetComponent <Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f)); } // Disable the snackbar UI when no planes are valid. bool showSearchingUI = true; Frame.GetAllPlanes(ref m_allPlanes); for (int i = 0; i < m_allPlanes.Count; i++) { if (m_allPlanes[i].IsValid) { showSearchingUI = false; break; } } m_searchingForPlaneUI.SetActive(showSearchingUI); if (Input.GetKeyDown(KeyCode.Escape)) { _isSpawned = false; } Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } if (!_isSpawned) { TrackableHit hit; TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon; if (Session.Raycast(m_firstPersonCamera.ScreenPointToRay(touch.position), raycastFilter, out hit)) { // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical // world evolves. var anchor = Session.CreateAnchor(hit.Point, Quaternion.identity); // Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit // from the anchor's tracking. var containerInstance = Instantiate(m_containerPrefab, hit.Point, Quaternion.identity, anchor.transform); // Andy should look at the camera but still be flush with the plane. containerInstance.transform.LookAt(m_firstPersonCamera.transform); containerInstance.transform.rotation = Quaternion.Euler(0.0f, containerInstance.transform.rotation.eulerAngles.y, containerInstance.transform.rotation.z); // Use a plane attachment component to maintain Andy's y-offset from the plane // (occurs after anchor updates). containerInstance.GetComponent <PlaneAttachment>().Attach(hit.Plane); _isSpawned = true; } } else { Cannon.Shoot(); } }