Http://blog.csdn.net/akof1314/article/details/38845933
A png/TGA gallery is provided, which is imported to unity and placed in the "assets/resources/UI" directory. (The UI folder can be replaced with another one, it is important to go to the "assets/resources/" Path). The default setting is as follows:
Change the Texture type"
Sprite"," Sprite mode "changed"
Multiple"," Format "changed"
Truecolor", Click" Apply. Then, click "Sprite Editor" to open the sprite editor. Click "slice" in the upper left corner to bring up the slice settings. Click the "slice" button again to automatically cut the image, as shown in: After modifying the incomplete cut, click the "Apply" button in the upper right corner to save. You can see that the gallery under the project view has been split into many small images, as shown in: Next, because you need to read and write images, you need to change the attributes of the images, otherwise, the following message is displayed:
- Unityexception: texture 'testuis 'is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the texture import settings.
Change the Image Texture type"
Advanced", Tick the" read/write enabled "attribute, as shown in: Create a script file, the Code is as follows:
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 |
|
Using unityengine; Using unityeditor;
Public class testsavesprite { [Menuitem ("tools/export Genie")] Static void savesprite () { String resourcespath = "assets/resources /"; Foreach (Object OBJ in selection. Objects) { String selectionpath = assetdatabase. getassetpath (OBJ );
// The upper level must be assets/resources /" If (selectionpath. startswith (resourcespath )) { String selectionext = system. Io. Path. getextension (selectionpath ); If (selectionext. Length = 0) { Continue; }
// Obtain the path "UI/testui" from the path "assets/resources/UI/testui.png" String loadpath = selectionpath. Remove (selectionpath. Length-selectionext. Length ); Loadpath = loadpath. substring (resourcespath. Length );
// Load all resources under this file Sprite [] sprites = resources. loadall <sprite> (loadpath ); If (sprites. length> 0) { // Create an export folder String outpath = application. datapath + "/outsprite/" + loadpath; System. Io. Directory. createdirectory (outpath );
Foreach (sprite Sprite in sprites) { // Create a separate texture Texture2d Tex = new texture2d (INT) Sprite. rect. Width, (INT) Sprite. rect. Height, Sprite. texture. format, false ); Tex. setpixels (sprite. texture. getpixels (INT) Sprite. rect. xmin, (INT) Sprite. rect. ymin, (INT) Sprite. rect. Width, (INT) Sprite. rect. Height )); Tex. Apply ();
// Write to PNG file System. Io. file. writeallbytes (outpath + "/" + Sprite. Name + ". PNG", Tex. encodetopng ()); } Debug. Log ("savesprite to" + outpath ); } } } Debug. Log ("savesprite finished "); } } |
In the unity editor, you can see that the export wizard item is added to the Tools menu, select the gallery, and click the export wizard menu item to export the subgraph. As shown in:
Unity export slice genie