Unity picture Segmentation saves Spirte on-premises

Source: Internet
Author: User

If you get a full picture, you want to use the Ngui sprite after splitting it! The requirements that can be solved below.

Steps:

1. Split a picture using Unity's Spirte

2. Use the code to convert the split 2DSpirte cost to PNG images, then import unity using Atlas texture O (∩_∩) o~

Precautions:

1. Switch the image to the advanced type Read/write enabled hook, otherwise it will throw an exception

Directly on the code:
[MenuItem ("tools/Export Wizard")]    Static voidSavesprite () {//each map type advanced Read/write enabled to make file read        stringResourcespath =@"assets/resources/"; foreach(Object objinchselection.objects) {stringSelectionpath =Assetdatabase.getassetpath (obj); //must be the most superior is "assets/resources/"            if(Selectionpath.startswith (Resourcespath)) {//get the file after the charge. PNG                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 [] spritelist = resources.loadall<sprite>(LoadPath); if(Spritelist.length >0)                {                    //Create an Export folder                    stringOutpath = Application.datapath +"/outsprite/"+LoadPath;                                        System.IO.Directory.CreateDirectory (Outpath); foreach(varSpriteinchspritelist) {texture2d 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 ("save picture Finished!"+Outpath); }            }        }    }

Unity picture Segmentation saves Spirte on-premises

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.