Search Unity

Quick Observation: How to navigate the new Raycasting Wizardy

Discussion in 'UGUI & TextMesh Pro' started by SweetBro, Aug 22, 2014.

  1. SweetBro

    SweetBro

    Joined:
    Jan 14, 2013
    Posts:
    69
    Okay, so 4.6 uses another raycasting system that essentially deals with click on stuff, IE raycast from screen to point. Using the old method of raycasting from camera is not recommended with the new GUI system since it will always cast through GUI element, which from a user perspective almost always undesirable.

    So first of all you need to add an Raycaster component to whichever camera you want to raycast. It's under Events.

    Second of all, any object that you want to be affected by this raycaster will need to implementing the IPointerClickHandler interface, which will allow you to have the OnPointerClick callback has
    PointerEventData as a parameter. The PointerEventData is the payload of the raycast hit and you can extrapolate whatever data is there to whatever it is you were doing previously.

    And obviously you still need to have a collider for the game object you want to click on.
     
  2. CelestinLoup

    CelestinLoup

    Joined:
    Aug 24, 2014
    Posts:
    3
    It also seems that you can block raycast on UI elements using CanvasGroup.

    Would there be any recommendable practice between the two methods ?C