Skip to content

Instantly share code, notes, and snippets.

@omgwtfgames
Forked from flarb/VRInputModule.cs
Created April 22, 2016 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omgwtfgames/ec06b50032c09d890295d2b8ab078258 to your computer and use it in GitHub Desktop.
Save omgwtfgames/ec06b50032c09d890295d2b8ab078258 to your computer and use it in GitHub Desktop.
Lets you use a VR world space cursor with World Space Canvases in Unity3D 4.6. Add this to the EventSystem object. Put some box colliders on your buttons in the World Space Canvas. Then, do a trace to see if you're looking at a menu object--if the trace hits one of those buttons, pass it to SetTargetObject. See ralphbarbagallo.com for a longer e…
using UnityEngine;
using UnityEngine.EventSystems;
//by Ralph Barbagallo
//www.flarb.com
//www.ralphbarbagallo.com
//@flarb
public class VRInputModule : BaseInputModule {
public static GameObject targetObject;
static VRInputModule _singleton;
void Awake() {
_singleton = this;
}
public override void Process()
{
if (targetObject == null)
return;
if (InputManager.press) { //I poll my own inputmanager class to see if the select button has been pressed
//We tapped the select button so SUBMIT (not select).
BaseEventData data = GetBaseEventData();
data.selectedObject = targetObject;
ExecuteEvents.Execute(targetObject, data, ExecuteEvents.submitHandler);
}
}
public override bool IsPointerOverEventSystemObject (int pointerId)
{
if (targetObject != null)
return true;
return false;
}
public static void SetTargetObject(GameObject obj) {
//we're hovering over a new object, so unhover the current one
if ((targetObject != null) && (targetObject != obj)) {
PointerEventData pEvent = new PointerEventData(_singleton.eventSystem);
pEvent.worldPosition = FlarbCrosshair3D.GetRaycastStart();
ExecuteEvents.Execute(targetObject, pEvent, ExecuteEvents.pointerExitHandler);
}
if (obj != null) {
//this is the same object that was hovered last time, so bail
if (obj == targetObject)
return;
//we've entered a new GUI object, so excute that event to highlight it
PointerEventData pEvent = new PointerEventData(_singleton.eventSystem);
pEvent.pointerEnter = obj;
pEvent.worldPosition = FlarbCrosshair3D.GetRaycastStart();
ExecuteEvents.Execute(obj, pEvent, ExecuteEvents.pointerEnterHandler);
}
targetObject = obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment