This is the 20th day of my participation in the August Text Challenge.More challenges in August

An overview of the

In the development of the project, sometimes you need to cut a Sprite image resource found on the Internet, want to use a part of it, or cut the picture as a puzzle. At this time we need to use the image cutting function, so how to achieve it? Let’s take a look.

Implementation effect

The implementation process

This article uses the Unity version Unity2019.4.17. The images to be cut will be placed in Unity’s Resources folder, so that you can call \ when writing the program later

Now we will begin the content study. go! go! go!

To do image cutting, first install the 2D Sprite Package into Unity editor and go to Windows->Package Manager. You can find it in the top few minutes and click Install in the lower right corner to Install it

Two, set up pictures

The types of images imported into Unity are generally the sameDefaultType, so we’re going to change it toSprite (2D and UI)The following

After changing the image type, change the image mode toMultipleThen you can cut the picture

Note: The important step is to make sure that the image is set to Read/Write mode. In Advanced, Read/Write Enabled must be checked.

Don’t forget to click Applay

Once you’ve set the image type and format, the next step is to cut the image invisible. Click the Sprite Editor button to cut the image

When you open the cut screen you’ll see one in the upper left cornerSlice (Slice)Button, click the button, select the cutting type in the pop-up small window. There are three cutting types:

  • Automatic: Automatic cutting
  • Grid By Cell Size: Cut according to Size
  • Grid By Cell Count: Cuts By number

Here we choose to cut according to size, secondLet’s take a look at the following interface. Set the length, width, height and interval of each part of the cutting and click after settingSliceFor image cuttingIn the following figure, the arrow points to the cutting line, and you can also see the size of each piece and other parameters. After cutting, you can drag the wire frame with the mouse to customize the cutting size

Finally, click Apply to save, in the upper right corner

After saving, we can see in Unity that the image we imported has been broken up into several different pieces, but we still need to separate it into separate images

Write code to save the image as a separate image

Create a new script, in this case using [MenuItem(“Tools/ export Sprite “)] to write the code in the editor extension. Select. Objects to get the selected image, then traverse, and then get the path to the selected file

Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
Copy the code

Get all the resources under this file (Sprite). Then create an output (save) folder

 string outPath = Application.dataPath + "/outSprite/" + loadPath;
 System.IO.Directory.CreateDirectory(outPath);
Copy the code

After that, walk through all sprites, create a single texture, and save the image in Png format

Foreach (Sprite in sprites) {// Create a single Texture2D Tex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, sprite.texture.format, false); Tex. SetPixels (Sprite. Texture. GetPixels ((int) Sprite. The rect. XMin, (int) Sprite. The rect. YMin, (int)sprite.rect.width, (int)sprite.rect.height)); tex.Apply(); / / write into PNG File System. IO. File. WriteAllBytes (outPath + "/" + Sprite. The name + "PNG", Tex., EncodeToPNG ()); }Copy the code

At this point all images are saved into a separate folder, as shown below

Problems encountered

1. The error is as followsSolution: The problem is that the image is not readable, open the image readable mode can be:AdvancedUnder theRead/Write Enabled

2. The following two errors are reported:

The specific reason is not very clear, probably should be the image format is wrong, the image format can be changed to RGBA 32 bit

Source code sharing

GitHub address: Click here to skip to download

Write in the last

All the shared content is the author used in the daily development process of a variety of small function points, sharing is also a way to review, if there is a bad place to write, please give more advice. Welcome to learn from each other and make progress. This piece article is written here first, hope to be able to help you