1. What is the PhysicsRaycaster component?
PhysicsRaycaster is a component in Unity UGUI that is used 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 delivers the event to the corresponding UI element.
3. Common properties of PhysicsRaycaster
- Event Mask : Specify which layers of UI elements can receive events.
- Max Raycast Distance : Specifies the maximum detection distance of the ray.
- Blocking Objects : Specify which types of objects can block ray detection.
4. Common functions of PhysicsRaycaster
- Raycast : emits a ray and returns the UI element that intersects it.
5. Complete example code
Example 1: Click a button to change color
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonColorChanger : MonoBehaviour, IPointerClickHandler
{
private Image image;
private void Start()
{
image = GetComponent<Image>();
}
public void OnPointerClick(PointerEventData eventData)
{
image.color = Random.ColorHSV();
}
}
Operation steps :
- Create a Canvas object and create a Button object under the Canvas.
- Set the Color property of the Button object's Image component to any color.
- Set the Event Mask property of the Button object's PhysicsRaycaster component to its default value.
- Attach the ButtonColorChanger script of the Button object to the Button object.
- Run the game, click the Button object, and the color will change randomly.
Things to note :
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.
Example 2: Dragging objects
using UnityEngine;
using UnityEngine.EventSystems;
public class ObjectDragger : MonoBehaviour, IPointerDownHandler, IDragHandler
{
private RectTransform rectTransform;
private void Start()
{
rectTransform = GetComponent<RectTransform>();
}
public void OnPointerDown(PointerEventData eventData)
{
rectTransform.SetAsLastSibling();
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta;
}
}
Operation steps :
- Create a Canvas object and create an Image object under the Canvas.
- Set the Event Mask property of the Image object's PhysicsRaycaster component to its 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 follow the mouse or touch movement.
Things to note :
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.
Example 3: Click the button to play sound effects
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonSoundPlayer : MonoBehaviour, IPointerClickHandler
{
public AudioClip soundClip;
private Button button;
private AudioSource audioSource;
private void Start()
{
button = GetComponent<Button>();
audioSource = GetComponent<AudioSource>();
audioSource.clip = soundClip;
}
public void OnPointerClick(PointerEventData eventData)
{
audioSource.Play();
}
}
Operation steps :
- Create a Canvas object and create a Button object under the Canvas.
- Set the Event Mask property of the Button object's PhysicsRaycaster component to its default value.
- Add an AudioSource object to the scene and drag the sound effect file to the soundClip property of the ButtonSoundPlayer script.
- Attach the ButtonSoundPlayer script of the Button object to the Button object.
- Run the game, click the Button object, and the sound effects will play.
Things to note :
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.
Example 4: Click the button to show/hide the object
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ButtonObjectToggler : MonoBehaviour, IPointerClickHandler
{
public GameObject targetObject;
private Button button;
private void Start()
{
button = GetComponent<Button>();
}
public void OnPointerClick(PointerEventData eventData)
{
targetObject.SetActive(!targetObject.activeSelf);
}
}
Operation steps :
- Create a Canvas object and create a Button object under the Canvas.
- Set the Event Mask property of the Button object's PhysicsRaycaster component to its default value.
- Create an object that needs to be shown/hidden in the scene, and drag the object to the targetObject property of the ButtonObjectToggler script.
- Attach the ButtonObjectToggler script of the Button object to the Button object.
- Run the game, click the Button object, and the object will be displayed or hidden.
Things to note :
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.
Example 5: Click the button to switch scenes
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ButtonSceneSwitcher : MonoBehaviour, IPointerClickHandler
{
public string targetSceneName;
private Button button;
private void Start()
{
button = GetComponent<Button>();
}
public void OnPointerClick(PointerEventData eventData)
{
SceneManager.LoadScene(targetSceneName);
}
}
Operation steps :
- Create a Canvas object and create a Button object under the Canvas.
- Set the Event Mask property of the Button object's PhysicsRaycaster component to its default value.
- Create a target scene that needs to be switched to in the scene, and drag the name of the target scene to the targetSceneName property of the ButtonSceneSwitcher script.
- Attach the ButtonSceneSwitcher script of the Button object to the Button object.
- Run the game, click the Button object, and the scene will switch to the target scene.
Things to note :
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.