SoFunction
Updated on 2025-03-07

Unity UGUI's PhysicsRaycaster PhysicsRaycaster PhysicsRaycaster

Introduction and use of PhysicsRaycaster (Physics Ray Detection) components of Unity UGUI

1. What is the PhysicsRaycaster component?

PhysicsRaycaster is a component in Unity UGUI for physical ray detection on UI elements. It can detect whether a mouse or touch event occurs on a UI element and pass the event to the corresponding UI element.

2. How PhysicsRaycaster works

PhysicsRaycaster detects UI elements by emitting a ray. When a ray intersects a UI element, the PhysicsRaycaster passes the event to the corresponding UI element.

3. Common properties of PhysicsRaycaster

  • Event Mask: Specifies which layers of UI elements can receive events.
  • Max Raycast Distance: Specify the maximum detection distance of the ray.
  • Blocking Objects: Specify which types of objects can block ray detection.

4. Common functions of PhysicsRaycaster

  • Raycast: Explode a ray and return the UI element that intersects it.

5. Complete example code

Example 1: Click the button to change the color

using UnityEngine;
using ;
using ;
public class ButtonColorChanger : MonoBehaviour, IPointerClickHandler
{
    private Image image;
    private void Start()
    {
        image = GetComponent<Image>();
    }
    public void OnPointerClick(PointerEventData eventData)
    {
         = ();
    }
}

Operation steps

  • Create a Canvas object and create a Button object under Canvas.
  • Set the Color property of the Image component of the Button object to any color.
  • Set the Event Mask property of the PhysicsRaycaster component of the Button object to the default value.
  • Mount the ButtonColorChanger script of the Button object to the Button object.
  • Run the game, click on the Button object, and the color will change randomly.

Things to note

  • You need to add a Graphics Raycaster component to the Canvas object so that PhysicsRaycaster can work.

Example 2: Dragging an object

using UnityEngine;
using ;
public class ObjectDragger : MonoBehaviour, IPointerDownHandler, IDragHandler
{
    private RectTransform rectTransform;
    private void Start()
    {
        rectTransform = GetComponent<RectTransform>();
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        ();
    }
    public void OnDrag(PointerEventData eventData)
    {
         += ;
    }
}

Operation steps

  • Create a Canvas object and create an Image object under Canvas.
  • Set the Event Mask property of the PhysicsRaycaster component of the Image object to the default value.
  • Mount the ObjectDragger script of the Image object to the Image object.
  • Run the game, click on the Image object and drag it, the Image object will move with the mouse or touch.

Things to note

  • You need to add a Graphics Raycaster component to the Canvas object so that PhysicsRaycaster can work.

Example 3: Click the button to play sound effects

using UnityEngine;
using ;
using ;
public class ButtonSoundPlayer : MonoBehaviour, IPointerClickHandler
{
    public AudioClip soundClip;
    private Button button;
    private AudioSource audioSource;
    private void Start()
    {
        button = GetComponent<Button>();
        audioSource = GetComponent<AudioSource>();
         = soundClip;
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        ();
    }
}

Operation steps

  • Create a Canvas object and create a Button object under Canvas.
  • Set the Event Mask property of the PhysicsRaycaster component of the Button object to the default value.
  • Add an AudioSource object to the scene and drag the sound file to the soundClip property of the ButtonSoundPlayer script.
  • Mount the ButtonSoundPlayer script of the Button object to the Button object.
  • Run the game, click on the Button object, and the sound effect will be played.

Things to note

  • You need to add a Graphics Raycaster component to the Canvas object so that PhysicsRaycaster can work.

Example 4: Click the button to display/hide objects

using UnityEngine;
using ;
using ;
public class ButtonObjectToggler : MonoBehaviour, IPointerClickHandler
{
    public GameObject targetObject;
    private Button button;
    private void Start()
    {
        button = GetComponent<Button>();
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        (!);
    }
}

Operation steps

  • Create a Canvas object and create a Button object under Canvas.
  • Set the Event Mask property of the PhysicsRaycaster component of the Button object to the default value.
  • Create an object that needs to be displayed/hidden in the scene and drag the object onto the targetObject property of the ButtonObjectToggler script.
  • Mount the ButtonObjectToggler script of the Button object to the Button object.
  • Run the game and click on the Button object, the object will be displayed or hidden.

Things to note

  • You need to add a Graphics Raycaster component to the Canvas object so that PhysicsRaycaster can work.

Example 5: Click the button to switch the scene

using UnityEngine;
using ;
using ;
using ;
public class ButtonSceneSwitcher : MonoBehaviour, IPointerClickHandler
{
    public string targetSceneName;
    private Button button;
    private void Start()
    {
        button = GetComponent<Button>();
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        (targetSceneName);
    }
}

Operation steps

  • Create a Canvas object and create a Button object under Canvas.
  • Set the Event Mask property of the PhysicsRaycaster component of the Button object to the default value.
  • Create a target scene in the scene that you need to switch to, and drag the name of the target scene onto the targetSceneName property of the ButtonSceneSwitcher script.
  • Mount the ButtonSceneSwitcher script of the Button object to the Button object.
  • Run the game, click on the Button object, and the scene will switch to the target scene.

Things to note

  • You need to add a Graphics Raycaster component to the Canvas object so that PhysicsRaycaster can work.

References

  • Unity official documentation:PhysicsRaycaster
  • Unity official forum:PhysicsRaycaster

The above is the introduction and detailed content of the physical ray detection component of Unity UGUI. For more information about PhysicsRaycaster of Unity UGUI, please follow my other related articles!