Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gvr Daydream NGUI Integration

Discussion in 'AR/VR (XR) Discussion' started by FarhanGul, Aug 29, 2017.

  1. FarhanGul

    FarhanGul

    Joined:
    Jul 19, 2017
    Posts:
    2
    In order for day dream controller to work with NGUI you must attach the UICamera(NGUI event system) on Laser prefab under GvrControllerPointer. Then disable all event sources and assign onCustomInput delegate to the function below in UICamera script. It is a slight modification of process mouse. Hope this helps!

    public void ProcessGvrController()
    {
    // Is any button currently pressed?
    bool isPressed = false;
    bool justPressed = false;

    if (GvrControllerInput.ClickButtonDown)
    {
    justPressed = true;
    isPressed = true;
    }
    else if (GvrControllerInput.ClickButton)
    {
    isPressed = true;
    }


    currentTouch = mMouse[0];

    // Update the position and delta
    Vector2 pos = currentCamera.ViewportToScreenPoint(new Vector3(0.5f, 0.5f, 1f));

    if (currentTouch.ignoreDelta == 0)
    {
    currentTouch.delta = pos - currentTouch.pos;
    }
    else
    {
    --currentTouch.ignoreDelta;
    currentTouch.delta.x = 0f;
    currentTouch.delta.y = 0f;
    }

    float sqrMag = currentTouch.delta.sqrMagnitude;
    currentTouch.pos = pos;
    mLastPos = pos;

    bool posChanged = false;


    if (sqrMag > 0.001f) posChanged = true;


    // No need to perform raycasts every frame
    if (isPressed || posChanged || mNextRaycast < RealTime.time)
    {
    mNextRaycast = RealTime.time + 0.02f;
    Raycast(currentTouch);
    mMouse[0].current = currentTouch.current;
    }

    bool highlightChanged = (currentTouch.last != currentTouch.current);
    bool wasPressed = (currentTouch.pressed != null);

    if (!wasPressed)
    hoveredObject = currentTouch.current;

    currentTouchID = -1;

    if (!isPressed && posChanged && (!stickyTooltip || highlightChanged))
    {
    if (mTooltipTime != 0f)
    {
    // Delay the tooltip
    mTooltipTime = Time.unscaledTime + tooltipDelay;
    }
    else if (mTooltip != null)
    {
    // Hide the tooltip
    ShowTooltip(null);
    }
    }

    // Generic mouse move notifications
    if (posChanged && onMouseMove != null)
    {
    onMouseMove(currentTouch.delta);
    currentTouch = null;
    }

    // The button was released over a different object -- remove the highlight from the previous
    if (highlightChanged && (justPressed || (wasPressed && !isPressed)))
    hoveredObject = null;

    // Process all 3 mouse buttons as individual touches
    for (int i = 0; i < 3; ++i)
    {
    bool pressed = GvrControllerInput.ClickButtonDown;
    bool unpressed = GvrControllerInput.ClickButtonUp;
    currentTouch = mMouse[0];

    // We don't want to update the last camera while there is a touch happening
    if (pressed)
    {
    currentTouch.pressedCam = currentCamera;
    currentTouch.pressTime = RealTime.time;
    }
    else if (currentTouch.pressed != null) currentCamera = currentTouch.pressedCam;

    // Process the mouse events
    ProcessTouch(pressed, unpressed);
    }

    // If nothing is pressed and there is an object under the touch, highlight it
    if (!isPressed && highlightChanged)
    {
    currentTouch = mMouse[0];
    mTooltipTime = RealTime.time + tooltipDelay;
    currentTouchID = -1;
    hoveredObject = currentTouch.current;
    }
    currentTouch = null;

    // Update the last value
    mMouse[0].last = mMouse[0].current;
    }