-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteSplicer.cs
57 lines (53 loc) · 2.71 KB
/
SpriteSplicer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using UnityEngine;
namespace Assets.Scripts.Libraries.UnityHelpers
{
public static class SpriteSplicer
{
/// <summary>
/// Splices a sprite into multiple sprites, returns a new empty gameobject that contains the spliced sprites as children.
/// </summary>
/// <param name="sprite">The sprite to be sliced.</param>
/// <param name="splices">The amount of slices.</param>
/// <param name="position">Optional, sets the position of the parent container.</param>
/// <param name="scale">Optional, sets the scale of the parent container.</param>
/// <param name="rotation">Optional, sets the rotation of the parent container.</param>
/// <returns>GameObject containing sprite slices as child gameobjects.</returns>
public static GameObject Splice(SpriteRenderer renderer, int splices, Vector3? position = null, Vector3? scale = null, Quaternion? rotation = null)
{
var sprite = renderer.sprite;
var texture = sprite.texture;
var spliceParent = new GameObject("Splices");
var spliceSizeX = texture.width / splices;
var spliceSizeY = texture.height / splices;
float split = 1f / splices;
float prevValueX = -(split * (splices / 2f)) + (split / 2f);
for (int i = 0; i < splices; i++)
{
float prevValueY = -(split * (splices / 2f)) + (split / 2f);
if (i != 0)
prevValueX += split;
for (int j = 0; j < splices; j++)
{
if (j != 0)
prevValueY += split;
Sprite newSprite = Sprite.Create(texture, new Rect(i * spliceSizeX, j * spliceSizeY, spliceSizeX, spliceSizeY), new Vector2(0.5f, 0.5f), sprite.pixelsPerUnit);
GameObject n = new();
SpriteRenderer sr = n.AddComponent<SpriteRenderer>();
sr.sprite = newSprite;
sr.sortingOrder = renderer.sortingOrder;
n.transform.parent = spliceParent.transform;
n.transform.localRotation = Quaternion.identity;
n.transform.localPosition = new Vector3(prevValueX, prevValueY, 0);
}
}
// Set transform settings
if (position != null)
spliceParent.transform.position = position.Value;
if (scale != null)
spliceParent.transform.localScale = scale.Value;
if (rotation != null)
spliceParent.transform.rotation = rotation.Value;
return spliceParent;
}
}
}