Suppose you have a png/tga atlas, import it into unity, place the directory "Assets/resources/ui" (the UI folder can be replaced by something else, it's important to be in the "assets/resources/" path), and the default setting is as follows:
In order to use Unity's own sprite cutting, change the texture type to "Sprite", "Sprite Mode" to "multiple", "Format" to "Truecolor", Click on the "Apply" button to apply.
Then, click on "Sprite Editor" to open the wizard, click on the "Slice" button in the upper left corner, pop up the slice settings, click on the "Slice" button, the image will be automatically cut, as shown in:
After making corrections to the incomplete cut, click on the "Apply" button in the top right corner to save. You can see this Atlas under Project view, which has been split into a number of small plots, as shown in:
Next, because you want to read and write to the picture, to change the properties of the picture can be done, otherwise you will be prompted as follows:
- Unityexception:texture ' Testui ' is not readable, the Texture memory can isn't accessed from scripts. You can make the texture readable in the texture Import Settings.
Change the picture texture type to "
Advanced, tick the Read/write Enabled property as shown in: Create a script file with the following code:
usingUnityengine;usingUnityeditor; Public classtestsavesprite{[MenuItem ("tools/Export Wizard")] Static voidSavesprite () {stringResourcespath ="assets/resources/"; foreach(Object objinchselection.objects) {stringSelectionpath =Assetdatabase.getassetpath (obj); //must be the most superior is "assets/resources/" if(Selectionpath.startswith (Resourcespath)) {stringSelectionext =System.IO.Path.GetExtension (Selectionpath); if(Selectionext.length = =0) { Continue; } //Get Path "Ui/testui" from Path "Assets/resources/ui/testui.png " stringLoadPath = Selectionpath.remove (Selectionpath.length-selectionext.length); LoadPath=loadpath.substring (resourcespath.length); //load all resources under this filesprite[] Sprites = resources.loadall<sprite>(LoadPath); if(Sprites. Length >0) { //Create an Export folder stringOutpath = Application.datapath +"/outsprite/"+LoadPath; System.IO.Directory.CreateDirectory (Outpath); foreach(Sprite Spriteinchsprites) { //Create a separate textureTexture2d Tex =NewTexture2d ((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 into a PNG fileSystem.IO.File.WriteAllBytes (Outpath +"/"+ Sprite.name +". PNG", Tex. Encodetopng ()); } Debug.Log ("Savesprite to"+Outpath); }}} Debug.Log ("Savesprite finished"); }}
In the Unity Editor you will see the "Export Sprite" item under the Tools menu, select the Atlas, and then click the "Export Sprite" menu item to export the sub-chart successfully. As shown in the following:
Go Unity output post-cut picture