Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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 explanation.

View VRInputModule.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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;
}
}

Hi Ralph
Thanks for supplying this code.
Can you put the code for your InputManager or a complete demo scene here as well?
Also, what is FlarbCrosshair3D? I am guessing it is a singleton of you custom cursor.

grobm commented

Danke,

This was the kind of code I was looking for, however I am getting an override error. As LumenDigital stated, could you post a InputManage Demo? It would be very helpful.

grobm, if you take a look at the Base class it is inheriting from then it looks like the method name has changed to public virtual bool IsPointerOverGameObject(int pointerId);

ShamezW commented

Yeah could you please post the code for your InputManager please :)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.