Unity's 2D Atlas processing, and cutting out a small picture

Source: Internet
Author: User

(Reproduced Please specify Source: http://blog.csdn.net/hongyouwei/article/details/45011315)

When we look for resources, sometimes encounter the kind of put a bunch of pictures into a PNG picture inside the situation, in doing 2D games, we often need to be inside a block of small picture cutting out to use, then the problem comes, how to cut it, now we provide a small picture, to show you:

So now we're going to import the picture into unity, select the image and you'll see the inspector interface above.

Then, select the texture type as advanced, such as:



Select the Read/write enabled, and then the sprite mode selects multiple, such as. Then click on the Sprite Editor to enter the graphical clipping interface:


Click on the upper left corner of the slice, in the upper right of the figure above I here type Select Grid, you can also choose automatic because I want to get three grid, so I am here the pixel is x:150 y:200, other regardless, directly click Slice, enter the following interface, Then select each grid and make adjustments so that it fits exactly with its boundary, such as:



Then click Apply on the top of the menu to get a cut-off atlas. Now this atlas, can be directly dragged out of the use, but if you want to quickly get inside a picture of Zhang, next to introduce a simple editor extension, which I inadvertently, in the bar to see someone use, come out and share with everyone.

First, in the Unity Project window under the assets Create a folder name editor, and then create a script inside, name casually, I named here: Imageslicer, the next is to open the script to encode, here I directly paste the code, Then you can select the image, enter the assets directory, select Imageslider/process to Sprites, and then you can get a folder, there is a small picture of a picture, and now paste the code and cut the results of the picture:

Using unityengine;using system.collections;using unityeditor;using system.io;using System.Collections.Generic; public static Class Imageslicer{[menuitem ("Assets/imageslicer/process to Sprites")]static void Processtosprite () { Texture2d image = Selection.activeobject as texture2d;//gets the rotated object string rootpath = Path.getdirectoryname ( Assetdatabase.getassetpath (image));//Gets the path name string path = RootPath + "/" + Image.name + ". PNG ";//Picture path name Textureimporter teximp = Assetimporter.getatpath (path) as textureimporter;// Get Picture Entry Assetdatabase.createfolder (RootPath, image.name);//Create folder foreach (Spritemetadata metaData in Teximp.spritesheet)//Traverse small Atlas {texture2d myimage = new Texture2d ((int) metaData.rect.width, (int) metaData.rect.height); /abc_0: (x:2.00, y:400.00, width:103.00, height:112.00) for (int y = (int) metadata.rect.y; y < Metadata.rect.y + metaData . rect.height; y++)//y axis pixels {for (int x = (int) metadata.rect.x; x < Metadata.rect.x + metaData.rect.width; + +) myimage. SetPixel (x-(int) metadata.rect.x, y-(int) Metadata.rect.y, image. GetPixel (x, y));} Convert textures to encodetopng compatible format if (Myimage.format! = Textureformat.argb32 && Myimage.format! = textureformat.rgb24) { Texture2d newtexture = new Texture2d (myimage.width, myimage.height); Newtexture.setpixels (MyImage. Getpixels (0), 0); myimage = Newtexture;} var pngdata = myimage. Encodetopng ();//assetdatabase.createasset (myimage, RootPath + "/" + Image.name + "/" + Metadata.name + ". PNG "); File.writeallbytes (RootPath + "/" + Image.name + "/" + Metadata.name + ". PNG ", pngdata);//Refresh Resource Window interface Assetdatabase.refresh ();}}}

Unity's 2D Atlas processing, and cutting out a small picture

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.