public virtual void OnPointerOut(PointerEventArguments e) { if (PointerOut != null) { PointerOut(this, e); } }
public virtual void OnPointerIn(PointerEventArguments e) { if (PointerIn != null) //Unity causes an error if we invoke an event with no subscribers, therefore we should always make sure that the event != null before invoking it { PointerIn(this, e); } }
// Update is called once per frame void Update() { trigger_pressed = GetComponent <VR_inputs_controller>().trigger_pressed; if (laserIsActive) { transform.Find("laser_ray_holder").gameObject.SetActive(true); } else { transform.Find("laser_ray_holder").gameObject.SetActive(false); } //draw the ray float dist = 100f; //length of the ray Ray raycast = new Ray(transform.position, transform.forward); //specify the origin and direction of the ray RaycastHit hit; //this variable stores anything that gets hit by the ray bool bHit = Physics.Raycast(raycast, out hit); //boolean to check for collisions with any colliders. You can also specify the float maxDistance = max distance the ray should check for collisions; since it is not specified, it will be automatically set to infinity if (previousContact && previousContact != hit.transform) { PointerEventArguments args = new PointerEventArguments(); args.distance = 0f; args.target = previousContact; OnPointerOut(args); previousContact = null; } if (bHit && previousContact != hit.transform) { PointerEventArguments argsIn = new PointerEventArguments(); argsIn.distance = hit.distance; argsIn.target = hit.transform; OnPointerIn(argsIn); previousContact = hit.transform; } if (bHit) //if the ray hits a collider { dist = hit.distance; pointer.transform.localScale = new Vector3(thickness, thickness, dist); //the ray has as length the distance from the ray's origin to the impact point touchingPoint.transform.localPosition = new Vector3(0, 0, dist); if (trigger_pressed) //if the controller's trigger is pressed while the ray hits a collider { //Debug.Log(hit.collider.name); //return the name of the collider pointer.transform.localScale = new Vector3(thickness * 2f, thickness * 2f, hit.distance); //increase the thickness of the laser beam selectedObject = hit.transform.gameObject; //select an object } } else if (!bHit) //if the ray does not hit any collider { previousContact = null; pointer.transform.localScale = new Vector3(thickness, thickness, dist); //the ray has full length touchingPoint.transform.localPosition = new Vector3(0, 0, dist); if (trigger_pressed) { selectedObject = null; //if the user presses the trigger in free space, there won't be any selected object } } pointer.transform.localPosition = new Vector3(0f, 0f, dist / 2f); }
private void HandlePointerOut(object sender, PointerEventArguments e) //when the laser is outside the button (PointerOut), you Deselect the button (button.SetSelectedGameObject(null)) --> it will return to white color { var button = e.target.GetComponent <Button>(); if (button != null) { EventSystem.current.SetSelectedGameObject(null); //Debug.Log("HandlePointerOut " + e.target.name); } var toggle = e.target.GetComponent <Toggle>(); if (toggle != null) { EventSystem.current.SetSelectedGameObject(null); } }
//NOTE: Selecting a button is different from clicking on it private void HandlePointerIn(object sender, PointerEventArguments e) //when the laser is on the button (PointerIn), you Select the button (button.Select()) --> it will change color { var button = e.target.GetComponent <Button>(); if (button != null) { button.Select(); //Debug.Log("HandlePointerIn " + e.target.name); } var toggle = e.target.GetComponent <Toggle>(); if (toggle != null) { toggle.Select(); } }