Unity 2D gallery processing, and cut out a small image, unity2d gallery Cutting

Source: Internet
Author: User

Unity 2D gallery processing, and cut out a small image, unity2d gallery Cutting

(Reprinted please indicate the source: http://blog.csdn.net/hongyouwei/article/details/45011315)

When we look for resources, we sometimes encounter a situation where we put a bunch of images into a png image. When we were playing a 2D game, we often need to cut the small images in the image, so the problem arises. How can we cut it? Now we provide a small image (note, generally, the image width and height required in Unity should be an integer multiple of 4, for example, 128*128:

Now we will import the image to Unity. If you select the image, you will see the Inspector interface above.

Then, set Texture Type to Advanced, for example:



Select Read/Write Enabled and Sprite Mode select Multiple, as shown in. Click Sprite Editor to go to the image cutting interface:


Click Slice in the upper left corner. In the upper right corner, select Grid as the Type. You can also select Automatic. Because I want to get three grids, the pixels here are X: 150 Y: 200. Click Slice directly to go to the following page, select each grid, and adjust it so that it can just side with its border, for example:



Click Apply for the preceding menu bar to obtain a cut Gallery. Now this gallery can be dragged out and used directly. But if you want to get a picture in it quickly, I will introduce you to a simple editor extension, I can see someone using it in the post bar and share it with you.

First, create a folder named Editor in Assets in the Unity Project window, and then create a script in it. The folder name is ImageSlicer, the next step is to open the script for encoding. Here I paste the Code directly, and then you can select the image, enter the Assets Directory, and select ImageSlider/Process to Sprites, then you can get a folder with a small image in it. Now you can paste the code and cut the result image:

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; // get the rotated object string rootPath = Path. getDirectoryName (AssetDatabase. getAssetPath (image); // obtain the path name string path = rootPath + "/" + I Mage. name + ". PNG "; // image path name TextureImporter texImp = AssetImporter. getAtPath (path) as TextureImporter; // obtain the image entry AssetDatabase. createFolder (rootPath, image. name); // create the folder foreach (SpriteMetaData metaData in texImp. spritesheet) // traverse the small gallery {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. rec T. y; y <metaData. rect. y + metaData. rect. height; y ++) // y axis pixel {for (int x = (int) metaData. rect. x; x <metaData. rect. x + metaData. rect. width; x ++) myimage. setPixel (x-(int) metaData. rect. x, y-(int) metaData. rect. y, image. getPixel (x, y);} // convert the texture to the 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 the resource window interface AssetDatabase. refresh ();}}}

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.