Unity ShoeBox Extract Sprites to Unity format, shoeboxsprites

Source: Internet
Author: User
Tags transparent image

Unity ShoeBox Extract Sprites to Unity format, shoeboxsprites

Cutting a transparent image


ShoeBox Extract SpritesFunction. Drag the image to the Extract Sprites icon to automatically cut the image, as shown in:
Click SettingsYou can set it in detail. Here, set Clusters Merge Sub Items to false without merging subitems, change File Name to slice‑0000.png, and click ApplyYou can. Then press SaveYou can export the cut image. As shown in:
Generate a text document that records the submap coordinates of each cut. Add the following script in the Editor directory of Unity: C # Code
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Using System. Collections. Generic;
Using System. Reflection;
Using UnityEditor;
Using UnityEngine;
Using System. IO;

/// <Summary>
/// Process ShoeBox-cut images to Sprites
/// </Summary>
Public class ShoeBoxExtractSpritesImport
{
[MenuItem ("Assets/ShoeBox/Process to Sprites")]
Static void ProcessExtractSpritesToSprites ()
{
TextAsset txt = Selection. activeObject as TextAsset;
If (txt = null)
{
Debug. LogError ("Must be. txt file! ");
Return;
}
String assetPath = AssetDatabase. GetAssetPath (txt );
If (Path. GetExtension (assetPath )! = ". Txt ")
{
Debug. LogError ("Must be. txt file! ");
Return;
}

String rootPath = Path. GetDirectoryName (assetPath );
String path = rootPath + "/" + Path. GetFileNameWithoutExtension (assetPath );
TextureImporter texImp = AssetImporter. GetAtPath (path) as TextureImporter;
If (texImp = null)
{
Return;
}

Object [] args = new object [2] {0, 0 };
MethodInfo mi = typeof (TextureImporter). GetMethod ("GetWidthAndHeight", BindingFlags. NonPublic | BindingFlags. Instance );
Mi. Invoke (texImp, args );

Var height = (int) args [1];
List <SpriteMetaData> sprites = GetSpriteMetaDatas (rootPath, txt. text, height );

TexImp. spritesheet = sprites. ToArray ();
TexImp. textureType = TextureImporterType. Sprite;
TexImp. spriteImportMode = SpriteImportMode. Multiple;

AssetDatabase. DeleteAsset (assetPath );
AssetDatabase. ImportAsset (path );
}

Static List <SpriteMetaData> GetSpriteMetaDatas (string path, string text, int totalHeight)
{
List <SpriteMetaData> sprites = new List <SpriteMetaData> ();
Var sliceName = "slice ";
Var arrayTxt = text. Split (',');
Var pngIndex = 0;
Foreach (var txt in arrayTxt)
{
Var arrayPoint = txt. Split ('');

Int x = int. Parse (arrayPoint [arrayPoint. Length-2]);
Int y = int. Parse (arrayPoint [arrayPoint. Length-1]. TrimEnd ('.'));

PngIndex ++;
Var pngName = sliceName + pngIndex. ToString (). PadLeft (2, '0') + ". png ";
Var pngPath = path + "/" + pngName;
TextureImporter importer = AssetImporter. GetAtPath (pngPath) as TextureImporter;

If (importer! = Null)
{
Object [] args = new object [2] {0, 0 };
MethodInfo mi = typeof (TextureImporter). GetMethod ("GetWidthAndHeight", BindingFlags. NonPublic | BindingFlags. Instance );
Mi. Invoke (importer, args );

Var width = (int) args [0];
Var height = (int) args [1];

SpriteMetaData smd = new SpriteMetaData ();
Smd. rect = new Rect (x, totalHeight-y-height, width, height );
Smd. alignment = 0;
Smd. name = pngName. Remove (pngName. Length-4 );
Smd. Outputs = new Vector2 (0.5f, 0.5f );
Sprites. Add (smd );

AssetDatabase. DeleteAsset (pngPath );
}
}
Return sprites;
}
}
In the Project view, right-click the text document and choose ShoeBox> Process to Sprites from the shortcut menu. The Sprites format of Unity is automatically imported, as shown in:
The Sprite Editor of Unity also provides the image cutting function. However, when a complex image set is encountered, the cutting process is chaotic and the ShoeBox accuracy rate is relatively high.
ShoeBox address: http://renderhjs.net/shoebox/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.