A shader-based effect that crops portions of a UI image along the X and Y axes without using masks, providing efficient partial image rendering.
1 Overview
3 Methods
4 Usage
6 See also
The UIImageCrop effect allows cropping portions of a UI image along X and Y axes without requiring mask components. It applies a custom shader (UI Extensions/UI Image Crop) to the MaskableGraphic component, enabling efficient partial image display through shader-based cropping rather than masking. The component automatically applies the shader material on Start() and in the editor via OnValidate().
The properties of the UIImageCrop effect are as follows:
| Property | Description |
|---|---|
| XCrop | Normalized X-axis crop factor from 0 to 1 (default: 0). Crops the image horizontally |
| YCrop | Normalized Y-axis crop factor from 0 to 1 (default: 0). Crops the image vertically |
| Method | Arguments | Description |
|---|---|---|
| SetMaterial | (none) | Initializes the shader material on the MaskableGraphic component. Automatically called on Start() |
| SetXCrop | float xcrop | Sets the X crop factor (clamped 0-1) and updates the shader property |
| SetYCrop | float ycrop | Sets the Y crop factor (clamped 0-1) and updates the shader property |
To use the UIImageCrop effect:
Example code for runtime cropping:
using UnityEngine.UI.Extensions;
public class ImageCropExample : MonoBehaviour
{
public UIImageCrop imageCrop;
void Start()
{
// Ensure material is initialized
imageCrop.SetMaterial();
}
void CropHorizontally(float amount)
{
// Crop from 0 (no crop) to 1 (fully cropped)
imageCrop.SetXCrop(amount);
}
void AnimateCrop()
{
// Animate cropping over time
StartCoroutine(AnimateCropCoroutine());
}
IEnumerator AnimateCropCoroutine()
{
float t = 0;
while (t < 1)
{
imageCrop.SetXCrop(t);
imageCrop.SetYCrop(t);
t += Time.deltaTime;
yield return null;
}
}
}
Note: This component must be attached to a GameObject with a MaskableGraphic component (Image, RawImage, Text, etc.). If no graphic is found, an error will be logged.
Video demonstration to be added
Credits: 00christian00