Skip to content

Instantly share code, notes, and snippets.

@flarb
Created August 20, 2014 22:27
Show Gist options
  • Save flarb/052467190b84657f10d2 to your computer and use it in GitHub Desktop.
Save flarb/052467190b84657f10d2 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;
}
}
@grobm
Copy link

grobm commented Mar 25, 2015

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.

@monkeyfire
Copy link

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
Copy link

ShamezW commented May 30, 2015

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