Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Sprite GenerateOutline(Sprite sprite)
- {
- Vector2Int originSize = new Vector2Int((int)sprite.rect.width, (int)sprite.rect.height);
- Vector2Int originPivot = new Vector2Int((int)sprite.rect.x, (int)sprite.rect.y);
- Texture2D origin = sprite.texture;
- Vector2Int outlineSize = (originSize + Vector2Int.one) * 2;
- Texture2D outline = new Texture2D(outlineSize.x, outlineSize.y)
- {
- filterMode = FilterMode.Point
- };
- Color[] colors = outline.GetPixels();
- for (int i = 0; i < colors.Length; i++)
- {
- colors[i] = Color.clear;
- }
- outline.SetPixels(colors);
- for (int x = 0; x < originSize.x; x++)
- {
- for (int y = 0; y < originSize.y; y++)
- {
- Color color = origin.GetPixel(originPivot.x + x, originPivot.y + y);
- for (int cX = 0; cX < 2; cX++)
- {
- for (int cY = 0; cY < 2; cY++)
- {
- outline.SetPixel(x * 2 + cX + 1, y * 2 + cY + 1, color);
- }
- }
- }
- }
- colors = outline.GetPixels();
- Vector2Int[] neighbours = new Vector2Int[] { Vector2Int.up, Vector2Int.right, Vector2Int.down, Vector2Int.left };
- for (int x = 1; x < outlineSize.x - 1; x++)
- {
- for (int y = 1; y < outlineSize.y - 1; y++)
- {
- if (colors[x + y * outlineSize.y].a != 0f)
- {
- for (int n = 0; n < 4; n++)
- {
- Vector2Int position = new Vector2Int(x, y) + neighbours[n];
- if (colors[position.x + position.y * outlineSize.y].a == 0f)
- {
- outline.SetPixel(position.x, position.y, Color.black);
- }
- }
- }
- }
- }
- outline.Apply();
- return Sprite.Create(outline, new Rect(0f, 0f, outlineSize.x, outlineSize.y), Vector2.one / 2f, 16f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement