Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CiberX15 · Jul 11, 2015 at 07:45 PM · worldspaceoculusrift

How do I use custom input module and ignore the default

I am making a game for Occulus VR and I am using a world space canvas and cursor. I created a copy of the StandAloneInput module and I replaced all instances of "Input.mousePosition" with "CiberCursor.mousePosition" which is populated with the screen position of my world space cursor. I am running into two problems though.

A) I hide the "real" cursor by using Screen.lockcursor, however buttons that appear in the center of the screen (where the cursor is hidden) still light up and are clicked even though the cursor is hidden. Simply not putting any buttons in the center of the screen seems like a silly and clumsy way of trying to fix this. Is there a way to disable the default input so that my UI buttons ignore the default cursor.

B) The buttons do not light up or respond to my world space cursor at all. I am assuming there is something I need to do to force it to use my custom input module ?

Script attached to the cursor:

 using UnityEngine;
 using System.Collections;
 
 public class CiberCursor : MonoBehaviour {
 
     public float Sensitivity = 10;
     public Camera NormalCamera;
     public Camera OVRCamera;
 
     public static Vector2 mousePosition;
 
     protected Vector2 GetMousePos()
     {
         if(OVRCamera.enabled)
         {
             return OVRCamera.WorldToScreenPoint(transform.position);
         }
         else
         {
             return NormalCamera.WorldToScreenPoint(transform.position);
         }
     }
 
     void Update()
     {
         //transform.localPosition.x += Input.GetAxis("Mouse X") * Sensitivity;
         //transform.localPosition.y += Input.GetAxis("Mouse Y") * Sensitivity;
 
         Vector3 NewPos = transform.localPosition;
         NewPos.x += Input.GetAxis("Mouse X") * Sensitivity;
         NewPos.y += Input.GetAxis("Mouse Y") * Sensitivity;
         transform.localPosition = NewPos;
 
         mousePosition = GetMousePos();
     }
 
 }
 

Custom input module:

 using System;
 
 namespace UnityEngine.EventSystems
 {
     [AddComponentMenu("Event/Ciber Input Module")]
     public class CiberInput : PointerInputModule
     {
         private float m_NextAction;
         
         private Vector2 m_LastMousePosition;
         private Vector2 m_MousePosition;
         
         protected CiberInput()
         {}
         
         [Obsolete("Mode is no longer needed on input module as it handles both mouse and keyboard simultaneously.", false)]
         public enum InputMode
         {
             Mouse,
             Buttons
         }
         
         [Obsolete("Mode is no longer needed on input module as it handles both mouse and keyboard simultaneously.", false)]
         public InputMode inputMode
         {
             get { return InputMode.Mouse; }
         }
         
         [SerializeField]
         private string m_HorizontalAxis = "Horizontal";
         
         /// <summary>
         /// Name of the vertical axis for movement (if axis events are used).
         /// </summary>
         [SerializeField]
         private string m_VerticalAxis = "Vertical";
         
         /// <summary>
         /// Name of the submit button.
         /// </summary>
         [SerializeField]
         private string m_SubmitButton = "Submit";
         
         /// <summary>
         /// Name of the submit button.
         /// </summary>
         [SerializeField]
         private string m_CancelButton = "Cancel";
         
         [SerializeField]
         private float m_InputActionsPerSecond = 10;
         
         [SerializeField]
         private bool m_AllowActivationOnMobileDevice;
         
         public bool allowActivationOnMobileDevice
         {
             get { return m_AllowActivationOnMobileDevice; }
             set { m_AllowActivationOnMobileDevice = value; }
         }
         
         public float inputActionsPerSecond
         {
             get { return m_InputActionsPerSecond; }
             set { m_InputActionsPerSecond = value; }
         }
         
         /// <summary>
         /// Name of the horizontal axis for movement (if axis events are used).
         /// </summary>
         public string horizontalAxis
         {
             get { return m_HorizontalAxis; }
             set { m_HorizontalAxis = value; }
         }
         
         /// <summary>
         /// Name of the vertical axis for movement (if axis events are used).
         /// </summary>
         public string verticalAxis
         {
             get { return m_VerticalAxis; }
             set { m_VerticalAxis = value; }
         }
         
         public string submitButton
         {
             get { return m_SubmitButton; }
             set { m_SubmitButton = value; }
         }
         
         public string cancelButton
         {
             get { return m_CancelButton; }
             set { m_CancelButton = value; }
         }
         
         public override void UpdateModule()
         {
             m_LastMousePosition = m_MousePosition;
             m_MousePosition = CiberCursor.mousePosition;
         }
         
         public override bool IsModuleSupported()
         {
             // Check for mouse presence instead of whether touch is supported,
             // as you can connect mouse to a tablet and in that case we'd want
             // to use StandaloneInputModule for non-touch input events.
             return m_AllowActivationOnMobileDevice || Input.mousePresent;
         }
         
         public override bool ShouldActivateModule()
         {
             if (!base.ShouldActivateModule())
                 return false;
             
             var shouldActivate = Input.GetButtonDown(m_SubmitButton);
             shouldActivate |= Input.GetButtonDown(m_CancelButton);
             shouldActivate |= !Mathf.Approximately(Input.GetAxisRaw(m_HorizontalAxis), 0.0f);
             shouldActivate |= !Mathf.Approximately(Input.GetAxisRaw(m_VerticalAxis), 0.0f);
             shouldActivate |= (m_MousePosition - m_LastMousePosition).sqrMagnitude > 0.0f;
             shouldActivate |= Input.GetMouseButtonDown(0);
             return shouldActivate;
         }
         
         public override void ActivateModule()
         {
             base.ActivateModule();
             m_MousePosition = CiberCursor.mousePosition;
             m_LastMousePosition = CiberCursor.mousePosition;
             
             var toSelect = eventSystem.currentSelectedGameObject;
             if (toSelect == null)
                 toSelect = eventSystem.firstSelectedGameObject;
             
             eventSystem.SetSelectedGameObject(toSelect, GetBaseEventData());
         }
         
         public override void DeactivateModule()
         {
             base.DeactivateModule();
             ClearSelection();
         }
         
         public override void Process()
         {
             bool usedEvent = SendUpdateEventToSelectedObject();
             
             if (eventSystem.sendNavigationEvents)
             {
                 if (!usedEvent)
                     usedEvent |= SendMoveEventToSelectedObject();
                 
                 if (!usedEvent)
                     SendSubmitEventToSelectedObject();
             }
             
             ProcessMouseEvent();
         }
         
         /// <summary>
         /// Process submit keys.
         /// </summary>
         private bool SendSubmitEventToSelectedObject()
         {
             if (eventSystem.currentSelectedGameObject == null)
                 return false;
             
             var data = GetBaseEventData();
             if (Input.GetButtonDown(m_SubmitButton))
                 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.submitHandler);
             
             if (Input.GetButtonDown(m_CancelButton))
                 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.cancelHandler);
             return data.used;
         }
         
         private bool AllowMoveEventProcessing(float time)
         {
             bool allow = Input.GetButtonDown(m_HorizontalAxis);
             allow |= Input.GetButtonDown(m_VerticalAxis);
             allow |= (time > m_NextAction);
             return allow;
         }
         
         private Vector2 GetRawMoveVector()
         {
             Vector2 move = Vector2.zero;
             move.x = Input.GetAxisRaw(m_HorizontalAxis);
             move.y = Input.GetAxisRaw(m_VerticalAxis);
             
             if (Input.GetButtonDown(m_HorizontalAxis))
             {
                 if (move.x < 0)
                     move.x = -1f;
                 if (move.x > 0)
                     move.x = 1f;
             }
             if (Input.GetButtonDown(m_VerticalAxis))
             {
                 if (move.y < 0)
                     move.y = -1f;
                 if (move.y > 0)
                     move.y = 1f;
             }
             return move;
         }
         
         /// <summary>
         /// Process keyboard events.
         /// </summary>
         private bool SendMoveEventToSelectedObject()
         {
             float time = Time.unscaledTime;
             
             if (!AllowMoveEventProcessing(time))
                 return false;
             
             Vector2 movement = GetRawMoveVector();
             // Debug.Log(m_ProcessingEvent.rawType + " axis:" + m_AllowAxisEvents + " value:" + "(" + x + "," + y + ")");
             var axisEventData = GetAxisEventData(movement.x, movement.y, 0.6f);
             if (!Mathf.Approximately(axisEventData.moveVector.x, 0f)
                 || !Mathf.Approximately(axisEventData.moveVector.y, 0f))
             {
                 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, axisEventData, ExecuteEvents.moveHandler);
             }
             m_NextAction = time + 1f / m_InputActionsPerSecond;
             return axisEventData.used;
         }
         
         /// <summary>
         /// Process all mouse events.
         /// </summary>
         private void ProcessMouseEvent()
         {
             var mouseData = GetMousePointerEventData();
             
             var pressed = mouseData.AnyPressesThisFrame();
             var released = mouseData.AnyReleasesThisFrame();
             
             var leftButtonData = mouseData.GetButtonState(PointerEventData.InputButton.Left).eventData;
             
             if (!UseMouse(pressed, released, leftButtonData.buttonData))
                 return;
             
             // Process the first mouse button fully
             ProcessMousePress(leftButtonData);
             ProcessMove(leftButtonData.buttonData);
             ProcessDrag(leftButtonData.buttonData);
             
             // Now process right / middle clicks
             ProcessMousePress(mouseData.GetButtonState(PointerEventData.InputButton.Right).eventData);
             ProcessDrag(mouseData.GetButtonState(PointerEventData.InputButton.Right).eventData.buttonData);
             ProcessMousePress(mouseData.GetButtonState(PointerEventData.InputButton.Middle).eventData);
             ProcessDrag(mouseData.GetButtonState(PointerEventData.InputButton.Middle).eventData.buttonData);
             
             if (!Mathf.Approximately(leftButtonData.buttonData.scrollDelta.sqrMagnitude, 0.0f))
             {
                 var scrollHandler = ExecuteEvents.GetEventHandler<IScrollHandler>(leftButtonData.buttonData.pointerCurrentRaycast.gameObject);
                 ExecuteEvents.ExecuteHierarchy(scrollHandler, leftButtonData.buttonData, ExecuteEvents.scrollHandler);
             }
         }
         
         private static bool UseMouse(bool pressed, bool released, PointerEventData pointerData)
         {
             if (pressed || released || pointerData.IsPointerMoving() || pointerData.IsScrolling())
                 return true;
             
             return false;
         }
         
         private bool SendUpdateEventToSelectedObject()
         {
             if (eventSystem.currentSelectedGameObject == null)
                 return false;
             
             var data = GetBaseEventData();
             ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.updateSelectedHandler);
             return data.used;
         }
         
         /// <summary>
         /// Process the current mouse press.
         /// </summary>
         private void ProcessMousePress(MouseButtonEventData data)
         {
             var pointerEvent = data.buttonData;
             var currentOverGo = pointerEvent.pointerCurrentRaycast.gameObject;
             
             // PointerDown notification
             if (data.PressedThisFrame())
             {
                 pointerEvent.eligibleForClick = true;
                 pointerEvent.delta = Vector2.zero;
                 pointerEvent.dragging = false;
                 pointerEvent.useDragThreshold = true;
                 pointerEvent.pressPosition = pointerEvent.position;
                 pointerEvent.pointerPressRaycast = pointerEvent.pointerCurrentRaycast;
                 
                 DeselectIfSelectionChanged(currentOverGo, pointerEvent);
                 
                 // search for the control that will receive the press
                 // if we can't find a press handler set the press
                 // handler to be what would receive a click.
                 var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.pointerDownHandler);
                 
                 // didnt find a press handler... search for a click handler
                 if (newPressed == null)
                     newPressed = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
                 
                 // Debug.Log("Pressed: " + newPressed);
                 
                 float time = Time.unscaledTime;
                 
                 if (newPressed == pointerEvent.lastPress)
                 {
                     var diffTime = time - pointerEvent.clickTime;
                     if (diffTime < 0.3f)
                         ++pointerEvent.clickCount;
                     else
                         pointerEvent.clickCount = 1;
                     
                     pointerEvent.clickTime = time;
                 }
                 else
                 {
                     pointerEvent.clickCount = 1;
                 }
                 
                 pointerEvent.pointerPress = newPressed;
                 pointerEvent.rawPointerPress = currentOverGo;
                 
                 pointerEvent.clickTime = time;
                 
                 // Save the drag handler as well
                 pointerEvent.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(currentOverGo);
                 
                 if (pointerEvent.pointerDrag != null)
                     ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.initializePotentialDrag);
             }
             
             // PointerUp notification
             if (data.ReleasedThisFrame())
             {
                 // Debug.Log("Executing pressup on: " + pointer.pointerPress);
                 ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
                 
                 // Debug.Log("KeyCode: " + pointer.eventData.keyCode);
                 
                 // see if we mouse up on the same element that we clicked on...
                 var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
                 
                 // PointerClick and Drop events
                 if (pointerEvent.pointerPress == pointerUpHandler && pointerEvent.eligibleForClick)
                 {
                     ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerClickHandler);
                 }
                 else if (pointerEvent.pointerDrag != null)
                 {
                     ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler);
                 }
                 
                 pointerEvent.eligibleForClick = false;
                 pointerEvent.pointerPress = null;
                 pointerEvent.rawPointerPress = null;
                 
                 if (pointerEvent.pointerDrag != null && pointerEvent.dragging)
                     ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.endDragHandler);
                 
                 pointerEvent.dragging = false;
                 pointerEvent.pointerDrag = null;
                 
                 // redo pointer enter / exit to refresh state
                 // so that if we moused over somethign that ignored it before
                 // due to having pressed on something else
                 // it now gets it.
                 if (currentOverGo != pointerEvent.pointerEnter)
                 {
                     HandlePointerExitAndEnter(pointerEvent, null);
                     HandlePointerExitAndEnter(pointerEvent, currentOverGo);
                 }
             }
         }
     }
 }


Thanks in advance for any help you can provide!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CiberX15 · Jul 13, 2015 at 12:18 AM 0
Share

This might all be in error. I am beginning to think I was using the wrong camera for the world to screen point calculation...

avatar image Baelgren · Jan 27, 2017 at 03:02 PM 0
Share

Have you found the answer to your question? I'm trying to do the same as I can't seem to find an already made OVR Input $$anonymous$$odule anywhere. :\

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by peterept · Jul 29, 2015 at 09:42 AM

I wrote a blog entry about making a VR input module:

http://talesfromtherift.com/vr-gaze-input/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dropdown not working with Oculus 3 Answers

Silly Oculus Question 2 Answers

oculus wrong view in unity 0 Answers

how to make Oculus Touch door opening 0 Answers

Create a memory buffer for delayed rendering 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges