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. The working principle of PhysicsRaycaster
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. Commonly used functions of PhysicsRaycaster
- Raycast: emits a ray and returns the UI elements that intersect it.
5. Complete example code
Example 1: Click the button to change the 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(); } }
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.
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; } }
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.
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 the sound effect
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(); } }
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.
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 display/hide objects
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); } }
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.
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); } }
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.
Note:
- A Graphics Raycaster component needs to be added to the Canvas object in order for PhysicsRaycaster to work.