Class SpriteUtil
Provides utility methods for procedurally generating sprites and textures at runtime, as well as safe cleanup helpers for both play-mode and edit-mode contexts.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class SpriteUtil
Remarks
All generation methods create new UnityEngine.Texture2D instances flagged with UnityEngine.HideFlags.HideAndDontSave, meaning they are excluded from the scene hierarchy and will not be serialized. Because Unity's garbage collector does not automatically free GPU-backed objects, every texture and sprite returned by these methods must be explicitly destroyed when no longer needed. Use DestroySprite(Sprite) and DestroyTexture(Texture2D) to perform correct cleanup in both play mode and edit mode.
The rounded-rectangle and circle sprites use sub-pixel distance comparisons to produce a one-pixel anti-aliased fringe at shape edges. The resulting sprites are suitable for use with Unity UI (Image component in Simple or Sliced mode).
Methods
CreateCircleSprite(int)
Creates a white filled circle UnityEngine.Sprite with a soft anti-aliased
edge. The backing texture is square with dimensions radius * 2 + 2
pixels - the full circle diameter plus a one-pixel anti-aliasing fringe on
each side. The texture is marked with UnityEngine.HideFlags.HideAndDontSave.
Declaration
public static Sprite CreateCircleSprite(int radius)
Parameters
| Type | Name | Description |
|---|---|---|
| int | radius | Circle radius in pixels. Values less than 1 are clamped to 1. The resulting
texture will be |
Returns
| Type | Description |
|---|---|
| Sprite | A new UnityEngine.Sprite containing an anti-aliased white filled circle. The sprite pivot is centred at (0.5, 0.5) with 100 pixels per unit and uses UnityEngine.SpriteMeshType.FullRect (no mesh trimming). |
Remarks
Anti-aliasing is produced by computing the signed Euclidean distance from each pixel centre to the circle boundary and blending the alpha channel across the outermost pixel ring. The texture uses UnityEngine.FilterMode.Bilinear and UnityEngine.TextureFormat.RGBA32.
Important: The returned sprite and its backing texture must be manually destroyed when no longer needed to prevent GPU memory leaks. Use DestroySprite(Sprite) for proper cleanup in both play mode and edit mode.
See Also
CreateGradientTexture(Color, Color, bool)
Creates a minimal 2×2 UnityEngine.Texture2D representing a linear gradient between two colors. Because the texture uses UnityEngine.FilterMode.Bilinear and UnityEngine.TextureWrapMode.Clamp, the GPU smoothly interpolates the gradient across the full extent of any UI element or mesh the texture is applied to, regardless of its screen size.
Declaration
public static Texture2D CreateGradientTexture(Color startColor, Color endColor, bool horizontal = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Color | startColor | The color at the start of the gradient: the bottom row for a vertical gradient, or the left column for a horizontal gradient. |
| Color | endColor | The color at the end of the gradient: the top row for a vertical gradient, or the right column for a horizontal gradient. |
| bool | horizontal |
|
Returns
| Type | Description |
|---|---|
| Texture2D | A new 2×2 UnityEngine.Texture2D with UnityEngine.TextureFormat.RGBA32, UnityEngine.FilterMode.Bilinear, and UnityEngine.TextureWrapMode.Clamp representing the specified gradient. |
Remarks
Using a 2×2 texture instead of a full-resolution gradient minimises memory usage while achieving the same visual result for linear ramps. For non-linear or multi-stop gradients, a higher-resolution texture would be required.
Important: The returned texture must be manually destroyed when no longer needed to prevent GPU memory leaks. Use DestroyTexture(Texture2D) for proper cleanup in both play mode and edit mode.
See Also
CreateRoundedRectSprite(int)
Creates a white UnityEngine.Sprite with anti-aliased rounded corners,
auto-sizing the texture to the minimum dimensions required to represent the
corner radius. The texture size is computed as cornerRadius * 2 + 2
pixels in each axis - two full corner regions plus a one-pixel 9-slice centre
strip and a one-pixel anti-aliasing fringe on each side.
Declaration
public static Sprite CreateRoundedRectSprite(int cornerRadius)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cornerRadius | Corner radius in pixels. Values less than 1 are clamped to 1. |
Returns
| Type | Description |
|---|---|
| Sprite | A new square UnityEngine.Sprite sized to |
Remarks
This convenience overload is equivalent to calling
CreateRoundedRectSprite(int, int, int) with
width = cornerRadius * 2 + 2, height = cornerRadius * 2 + 2,
and the specified cornerRadius.
Important: The returned sprite and its backing texture must be manually destroyed when no longer needed to prevent GPU memory leaks. Use DestroySprite(Sprite) for proper cleanup in both play mode and edit mode.
See Also
CreateRoundedRectSprite(int, int, int)
Creates a white UnityEngine.Sprite backed by a procedurally generated UnityEngine.Texture2D with soft anti-aliased rounded corners, configured for 9-slice scaling. The 9-slice border is set uniformly to the effective corner radius so that all four corners scale independently when the sprite is stretched.
Declaration
public static Sprite CreateRoundedRectSprite(int width, int height, int cornerRadius)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | Texture width in pixels. Values less than 1 are clamped to 1. |
| int | height | Texture height in pixels. Values less than 1 are clamped to 1. |
| int | cornerRadius | Corner radius in pixels. Values greater than half the smaller dimension are clamped to prevent corners from overlapping. Negative values are clamped to 0, producing a sharp-cornered rectangle. |
Returns
| Type | Description |
|---|---|
| Sprite | A new UnityEngine.Sprite whose texture is |
Remarks
Anti-aliasing is achieved by computing the signed distance from each corner pixel to the arc centre and blending the alpha channel across a one-pixel fringe. The texture uses UnityEngine.FilterMode.Bilinear and UnityEngine.TextureFormat.RGBA32.
Important: The returned sprite and its backing texture must be manually destroyed when no longer needed to prevent GPU memory leaks. Use DestroySprite(Sprite) for proper cleanup in both play mode and edit mode.
See Also
CreateSolidColorTexture(int, int, Color)
Creates a UnityEngine.Texture2D filled uniformly with the specified color. The texture uses point filtering (no blending) and is marked with UnityEngine.HideFlags.HideAndDontSave.
Declaration
public static Texture2D CreateSolidColorTexture(int width, int height, Color color)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | Texture width in pixels. Values less than 1 are clamped to 1. |
| int | height | Texture height in pixels. Values less than 1 are clamped to 1. |
| Color | color | The fill color in linear or gamma color space, matching the project's color space setting. All pixels are set to this color. |
Returns
| Type | Description |
|---|---|
| Texture2D | A new UnityEngine.Texture2D of the specified dimensions with all pixels set
to |
Remarks
Solid color textures are useful as backgrounds, masks, or tinted UI elements where a plain color block is needed without requiring a sprite asset. The texture uses UnityEngine.FilterMode.Point so it renders crisply when sized exactly to the display region.
Important: The returned texture must be manually destroyed when no longer needed to prevent GPU memory leaks. Use DestroyTexture(Texture2D) for proper cleanup in both play mode and edit mode.
See Also
DestroySprite(Sprite)
Destroys a UnityEngine.Sprite and its associated backing UnityEngine.Texture2D,
using Destroy(Object, float) in play mode and DestroyImmediate(Object, bool)
in edit mode. Safe to call with null - no action is taken if
sprite is null.
Declaration
public static void DestroySprite(Sprite sprite)
Parameters
| Type | Name | Description |
|---|---|---|
| Sprite | sprite | The sprite to destroy, typically one returned by CreateRoundedRectSprite(int, int, int)
or CreateCircleSprite(int). May be |
Remarks
Always prefer this method over calling Destroy(Object, float) directly on procedurally generated sprites, because the backing texture is a separate UnityEngine.Object that must be explicitly destroyed to free GPU memory. Forgetting to destroy the texture is a common source of VRAM leaks in edit-mode tooling.
See Also
DestroyTexture(Texture2D)
Destroys a UnityEngine.Texture2D, using Destroy(Object, float) in play
mode and DestroyImmediate(Object, bool) in edit mode to ensure immediate
GPU memory release. Safe to call with null - no action is taken if
texture is null.
Declaration
public static void DestroyTexture(Texture2D texture)
Parameters
| Type | Name | Description |
|---|---|---|
| Texture2D | texture | The texture to destroy. May be |
Remarks
Use this method to clean up textures returned by CreateSolidColorTexture(int, int, Color) or CreateGradientTexture(Color, Color, bool). For sprite cleanup (which includes the backing texture), use DestroySprite(Sprite) instead.