void LateUpdate() { if (head == null) // Pointer not being controlled by user's head, so we bail. { pointerVisible = true; // Draw pointer wherever Unity thinks the mouse is. return; } if (!CardboardOnGUI.IsGUIVisible) { pointerVisible = false; // No GUI == no pointer to worry about. return; } // Find which CardboardOnGUIWindow the user's gaze intersects first, if any. Ray ray = head.Gaze; CardboardOnGUIWindow hitWindow = null; float minDist = Mathf.Infinity; Vector2 pos = Vector2.zero; foreach (var guiWindow in GetComponentsInChildren <CardboardOnGUIWindow>()) { RaycastHit hit; if (guiWindow.GetComponent <Collider>().Raycast(ray, out hit, Mathf.Infinity) && hit.distance < minDist) { minDist = hit.distance; hitWindow = guiWindow; pos = hit.textureCoord; } } if (hitWindow == null) { // Don't draw the pointer unless the user is looking at a pixel of the GUI screen. pointerVisible = false; return; } // Convert from the intersected window's texture coordinates to screen coordinates. pos.x = hitWindow.rect.xMin + hitWindow.rect.width * pos.x; pos.y = hitWindow.rect.yMin + hitWindow.rect.height * pos.y; int x = (int)(pos.x * Screen.width); // Unity GUI Y-coordinates ascend top-to-bottom, as do the quad's texture coordinates, // while screen Y-coordinates ascend bottom-to-top. int y = (int)((1 - pos.y) * Screen.height); // Send the necessary event to Unity - next frame it will update the mouse. if (Cardboard.SDK.CardboardTriggered) { Cardboard.SDK.InjectMouseClick(x, y); } else { Cardboard.SDK.InjectMouseMove(x, y); } // OK to draw the pointer image. pointerVisible = true; }
void LateUpdate() { StereoController controller = Cardboard.Controller; CardboardHead head = controller ? controller.Head : null; if (head == null) // Pointer not being controlled by user's head, so we bail. { pointerVisible = true; // Draw pointer wherever Unity thinks the mouse is. return; } if (!CardboardOnGUI.IsGUIVisible) { pointerVisible = false; // No GUI == no pointer to worry about. return; } // Find which CardboardOnGUIWindow the user's gaze intersects first, if any. Ray ray = head.Gaze; CardboardOnGUIWindow hitWindow = null; float minDist = Mathf.Infinity; Vector2 pos = Vector2.zero; foreach (var guiWindow in GetComponentsInChildren <CardboardOnGUIWindow>()) { RaycastHit hit; if (guiWindow.GetComponent <Collider>().Raycast(ray, out hit, Mathf.Infinity) && hit.distance < minDist) { minDist = hit.distance; hitWindow = guiWindow; pos = hit.textureCoord; } } if (hitWindow == null) { // Don't draw the pointer unless the user is looking at a pixel of the GUI screen. pointerVisible = false; return; } // Convert from the intersected window's texture coordinates to screen coordinates. pos.x = hitWindow.rect.xMin + hitWindow.rect.width * pos.x; pos.y = hitWindow.rect.yMin + hitWindow.rect.height * pos.y; pointerX = (int)(pos.x * Screen.width); pointerY = (int)(pos.y * Screen.height); // Move the mouse/touch point to the determined screen point. // Unity GUI Y-coordinates ascend top-to-bottom, as do the quad's texture coordinates, // while screen Y-coordinates ascend bottom-to-top. Cardboard.SDK.SetTouchCoordinates(pointerX, Screen.height - pointerY); // OK to draw the pointer image. pointerVisible = true; }