返回 Skills 目錄
unity-technologies/skills已通過檢查

SKILL DETAIL

sprite-segment-3x3grid

unity-technologies/skills/sprite-segment-3x3grid

Analyze Sprite textures and output a 3x3 grid representation based on color matching. Segments a Sprite into a 3x3 grid, identifies the majority color of the center cell, and outputs a text pattern showing which cells match the center color. Use when analyzing sprite patterns, documenting sprite structure, or describing sprite color distribution.

安裝量 · 99查看來源

Installation

npx skills add https://github.com/unity-technologies/skills --skill sprite-segment-3x3grid

技能檔案

SKILL.md

最近同步 · 2026年9月13日

scripts/SpriteGridAnalysis.cs
// Code template for analyzing Sprite textures as a 3x3 color grid.
// Segments a Sprite into a 3x3 grid and outputs a text pattern based on
// color matching with the center cell.

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SpriteGridAnalysis
{
    public static class SpriteGridAnalysis
    {
        /// <summary>
        /// Analyzes a sprite and returns a 3x3 grid pattern based on color matching.
        /// </summary>
        /// <param name="sprite">The sprite to analyze</param>
        /// <param name="matchThreshold">Percentage of pixels that must match center color (0.5 = 50%, 0.75 = 75%, 0.9 = 90%)</param>
        /// <param name="colorTolerance">RGB tolerance for considering two colors as matching</param>
        public static string AnalyzeSpriteGrid(Sprite sprite, float matchThreshold = 0.5f, float colorTolerance = 0.04f)
        {
            var texture = sprite.texture;
            var rect = sprite.rect;

            int cellWidth = (int)(rect.width / 3);
            int cellHeight = (int)(rect.height / 3);

            // Get center cell bounds (row 1, col 1)
            int centerStartX = (int)rect.x + cellWidth;
            int centerStartY = (int)rect.y + cellHeight;
            Color centerColor = GetMajorityColor(texture, centerStartX, centerStartY, cellWidth, cellHeight);

            // Build output by checking each cell against center color
            var symbols = new string[9];
            for (int row = 0; row < 3; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    int cellIndex = row * 3 + col;

                    if (cellIndex == 4)
                    {
                        symbols[cellIndex] = "*";
                        continue;
                    }

                    int startX = (int)rect.x + col * cellWidth;
                    int startY = (int)rect.y + (2 - row) * cellHeight; // Flip Y for top-to-bottom
                    float matchPercentage = GetColorMatchPercentage(texture, startX, startY, cellWidth, cellHeight, centerColor, colorTolerance);

                    symbols[cellIndex] = matchPercentage >= matchThreshold ? "." : "X";
                }
            }

            // Format: "A B C / D E F / G H I"
            return $"{symbols[0]} {symbols[1]} {symbols[2]} / {symbols[3]} {symbols[4]} {symbols[5]} / {symbols[6]} {symbols[7]} {symbols[8]}";
        }

        /// <summary>
        /// Gets the majority (most frequent) color in a cell region.
        /// </summary>
        static Color GetMajorityColor(Texture2D texture, int startX, int startY, int width, int height)
        {
            var colorCounts = new Dictionary<Color32, int>();

            for (int y = startY; y < startY + height; y++)
            {
                for (int x = startX; x < startX + width; x++)
                {
                    Color32 pixel = texture.GetPixel(x, y);
                    if (!colorCounts.ContainsKey(pixel))
                        colorCounts[pixel] = 0;
                    colorCounts[pixel]++;
                }
            }

            return colorCounts.OrderByDescending(kvp => kvp.Value).First().Key;
        }

        /// <summary>
        /// Calculates what percentage of pixels in a cell match the target color.
        /// </summary>
        static float GetColorMatchPercentage(Texture2D texture, int startX, int startY, int width, int height, Color targetColor, float colorTolerance)
        {
            int totalPixels = 0;
            int matchingPixels = 0;

            for (int y = startY; y < startY + height; y++)
            {
                for (int x = startX; x < startX + width; x++)
                {
                    totalPixels++;
                    Color pixel = texture.GetPixel(x, y);
                    if (ColorsMatch(pixel, targetColor, colorTolerance))
                        matchingPixels++;
                }
            }

            return totalPixels > 0 ? (float)matchingPixels / totalPixels : 0f;
        }

        static bool ColorsMatch(Color a, Color b, float tolerance)
        {
            return Mathf.Abs(a.r - b.r) < tolerance &&
                   Mathf.Abs(a.g - b.g) < tolerance &&
                   Mathf.Abs(a.b - b.b) < tolerance;
        }
    }
}

// Usage Examples:
//
// // Default 50% threshold
// string pattern1 = SpriteGridAnalysis.AnalyzeSpriteGrid(mySprite);
//
// // Strict 90% threshold
// string pattern2 = SpriteGridAnalysis.AnalyzeSpriteGrid(mySprite, matchThreshold: 0.9f);
//
// // 75% threshold with tighter color tolerance
// string pattern3 = SpriteGridAnalysis.AnalyzeSpriteGrid(mySprite, matchThreshold: 0.75f, colorTolerance: 0.02f);
SKILL.md
---
name: sprite-segment-3x3grid
description: Analyze Sprite textures and output a 3x3 grid representation based on color matching. Segments a Sprite into a 3x3 grid, identifies the majority color of the center cell, and outputs a text pattern showing which cells match the center color. Use when analyzing sprite patterns, documenting sprite structure, or describing sprite color distribution.
---
# Sprite Color Grid Analysis

## Purpose

This skill analyzes Sprite textures by segmenting them into a 3x3 grid and outputting a text representation based on color matching with the center cell.

## Parameters

### Match Threshold

The **match threshold** determines what percentage of pixels in a non-center cell must match the center's majority color for that cell to be considered a "match" (`.`).

| Threshold | Description | Use Case |
|-----------|-------------|----------|
| **50%** | At least half the cell's pixels match center color | Lenient matching for sprites with mixed regions |
| **75%** | At least three-quarters match center color | Moderate matching for mostly solid regions |
| **90%** | Nearly all pixels must match center color | Strict matching for very uniform sprites |

**Default**: 75%

## Algorithm

1. **Segment the Sprite**: Divide the Sprite's texture region into a 3x3 grid (9 cells total)
2. **Identify Center Color**: Calculate the majority (most frequent) color in the center cell (position [1,1])
3. **Compare Each Cell**: For each of the 8 surrounding cells, calculate what percentage of pixels match the center's majority color. If the percentage meets or exceeds the **match threshold**, the cell is a match (`.`); otherwise it is not (`X`)
4. **Generate Output**: Create a text representation using the legend below

## Output Legend

| Symbol | Meaning |
|--------|---------|
| `X` | Cell does NOT meet the match threshold (insufficient pixels match center color) |
| `.` | Cell MEETS the match threshold (enough pixels match center color) |
| `*` | The center cell itself (always position [1,1] in the grid) |

## Output Format

The output is a single line with three groups of three characters, separated by ` / `. Each group represents one row of the grid.

**Order**: Top to Bottom rows, Left to Right within each row.

**Format**: `[TopRow] / [MiddleRow] / [BottomRow]`

Each row contains 3 characters separated by spaces: `[Left] [Center] [Right]`

### Grid Position Mapping

```
Grid Layout:        Output Order:
[0,0] [1,0] [2,0]   1  2  3   -> First group
[0,1] [1,1] [2,1]   4  5  6   -> Second group (5 is always *)
[0,2] [1,2] [2,2]   7  8  9   -> Third group
```

### Examples

**Example 1**: Center matches top-left and bottom-right only
```
. X X / X * X / X X .
```
Grid interpretation:
```
.  X  X
X  *  X
X  X  .
```

**Example 2**: Center matches all surrounding cells
```
. . . / . * . / . . .
```

**Example 3**: Center matches none of the surrounding cells
```
X X X / X * X / X X X
```

**Example 4**: Center matches bottom row only
```
X X X / X * X / . . .
```

## Implementation Guidance

### Calculating Center's Majority Color

For the center cell:
1. Sample all pixels within the cell's bounds
2. Group pixels by color (consider using a color tolerance threshold for similar colors)
3. The majority color is the color with the highest pixel count
4. For tie-breaking, use the first color encountered

### Matching Non-Center Cells

For each of the 8 surrounding cells:
1. Count the total number of pixels in the cell
2. Count how many pixels match the center's majority color (using color tolerance)
3. Calculate the match percentage: `matchingPixels / totalPixels`
4. If match percentage >= threshold, cell is a match (`.`); otherwise (`X`)

### Color Tolerance

When comparing pixel colors:
- Consider using a tolerance threshold (e.g., RGB distance < 10) for "matching"
- Alternatively, for sprites with limited palettes, exact matching may be appropriate
- Account for alpha channel if relevant to the use case

### Sprite Bounds

Use the Sprite's `rect` property to determine the pixel region to analyze:
- `sprite.rect.x`, `sprite.rect.y` for the origin
- `sprite.rect.width`, `sprite.rect.height` for dimensions
- Divide width and height by 3 to get cell dimensions

## References

Code Template: "scripts/SpriteGridAnalysis.cs"

## Use Cases

- **Documentation**: Describe sprite patterns in a compact text format
- **Testing**: Verify expected sprite structure in automated tests
- **Analysis**: Quickly identify sprites with similar color distributions
- **Debugging**: Understand why sprites look different than expected
- **Asset Cataloging**: Generate searchable metadata for sprite assets

## Notes

- This analysis works best with sprites that have distinct color regions
- For sprites with gradients or many colors, consider increasing color tolerance
- The Y-axis is flipped from Unity's texture coordinates to produce top-to-bottom output
- Transparent pixels can be treated as a distinct "color" or ignored based on use case