You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, your scripts are very usefule but there is a bug on the garbageRect list in AssetPacker.cs. Rectangle indexed are not corresponding. Here the corrected script :
usingDaVikingCode.RectanglePacking;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingUnityEngine;usingUnityEngine.Events;namespaceDaVikingCode.AssetPacker{publicclassAssetPacker:MonoBehaviour{publicUnityEventOnProcessCompleted;publicfloatpixelsPerUnit=100.0f;publicbooluseCache=false;publicstringcacheName="";publicintcacheVersion=1;publicbooldeletePreviousCacheVersion=true;protectedDictionary<string,Sprite>mSprites=newDictionary<string,Sprite>();protectedList<TextureToPack>itemsToRaster=newList<TextureToPack>();protectedboolallow4096Textures=false;publicvoidAddTextureToPack(stringfile,stringcustomID=null){itemsToRaster.Add(newTextureToPack(file,customID!=null?customID:Path.GetFileNameWithoutExtension(file)));}publicvoidAddTexturesToPack(string[]files){foreach(stringfileinfiles)AddTextureToPack(file);}publicvoidProcess(boolallow4096Textures=false){this.allow4096Textures=allow4096Textures;if(useCache){if(cacheName=="")thrownewException("No cache name specified");stringpath=Application.streamingAssetsPath+"/AssetPacker/"+cacheName+"/"+cacheVersion+"/";boolcacheExist=Directory.Exists(path);if(!cacheExist)StartCoroutine(createPack(path));elseStartCoroutine(loadPack(path));}elseStartCoroutine(createPack());}protectedIEnumeratorcreatePack(stringsavePath=""){if(savePath!=""){if(deletePreviousCacheVersion&&Directory.Exists(Application.streamingAssetsPath+"/AssetPacker/"+cacheName+"/"))foreach(stringdirPathinDirectory.GetDirectories(Application.streamingAssetsPath+"/AssetPacker/"+cacheName+"/","*",SearchOption.AllDirectories))Directory.Delete(dirPath,true);Directory.CreateDirectory(savePath);}List<Texture2D>textures=newList<Texture2D>();List<string>images=newList<string>();foreach(TextureToPackitemToRasterinitemsToRaster){WWWloader=newWWW("file:///"+itemToRaster.file);yieldreturnloader;textures.Add(loader.texture);images.Add(itemToRaster.id);}inttextureSize=allow4096Textures?4096:2048;List<Rect>rectangles=newList<Rect>();for(inti=0;i<textures.Count;i++){if(textures[i].width>textureSize||textures[i].height>textureSize){Debug.Log("A texture size is bigger than the sprite sheet size!");}else{rectangles.Add(newRect(0,0,textures[i].width,textures[i].height));}}constintpadding=1;intnumSpriteSheet=0;while(rectangles.Count>0){Texture2Dtexture=newTexture2D(textureSize,textureSize,TextureFormat.ARGB32,false);Color32[]fillColor=texture.GetPixels32();for(inti=0;i<fillColor.Length;++i)fillColor[i]=Color.clear;RectanglePackerpacker=newRectanglePacker(texture.width,texture.height,padding);for(inti=0;i<rectangles.Count;i++){packer.insertRectangle((int)rectangles[i].width,(int)rectangles[i].height,i);}packer.packRectangles();if(packer.rectangleCount>0){texture.SetPixels32(fillColor);IntegerRectanglerect=newIntegerRectangle();List<TextureAsset>textureAssets=newList<TextureAsset>();List<int>garbageRect=newList<int>();//List<Rect> garbageRect = new List<Rect>();List<Texture2D>garabeTextures=newList<Texture2D>();List<string>garbageImages=newList<string>();for(intj=0;j<packer.rectangleCount;j++){rect=packer.getRectangle(j,rect);intindex=packer.getRectangleId(j);texture.SetPixels32(rect.x,rect.y,rect.width,rect.height,textures[index].GetPixels32());TextureAssettextureAsset=newTextureAsset();textureAsset.x=rect.x;textureAsset.y=rect.y;textureAsset.width=rect.width;textureAsset.height=rect.height;textureAsset.name=images[index];textureAssets.Add(textureAsset);garbageRect.Add(index);//garbageRect.Add(rectangles[index]);garabeTextures.Add(textures[index]);garbageImages.Add(images[index]);}garbageRect.Sort();garbageRect.Reverse();for(intk=0;k<garbageRect.Count;k++){rectangles.RemoveAt(garbageRect[k]);}//foreach (Rect gRect in garbageRect)//rectangles.Remove(gRect);foreach(Texture2DgTexingarabeTextures)textures.Remove(gTex);foreach(stringgImgingarbageImages)images.Remove(gImg);texture.Apply();if(savePath!=""){File.WriteAllBytes(savePath+"/data"+numSpriteSheet+".png",texture.EncodeToPNG());File.WriteAllText(savePath+"/data"+numSpriteSheet+".json",JsonUtility.ToJson(newTextureAssets(textureAssets.ToArray())));++numSpriteSheet;}foreach(TextureAssettextureAssetintextureAssets)mSprites.Add(textureAsset.name,Sprite.Create(texture,newRect(textureAsset.x,textureAsset.y,textureAsset.width,textureAsset.height),Vector2.zero,pixelsPerUnit,0,SpriteMeshType.FullRect));}}OnProcessCompleted.Invoke();}protectedIEnumeratorloadPack(stringsavePath){intnumFiles=Directory.GetFiles(savePath).Length;int_div=2;if(Application.platform==RuntimePlatform.WindowsEditor){_div=4;}for(inti=0;i<numFiles/_div;++i){WWWloaderTexture=newWWW("file:///"+savePath+"/data"+i+".png");yieldreturnloaderTexture;WWWloaderJSON=newWWW("file:///"+savePath+"/data"+i+".json");yieldreturnloaderJSON;TextureAssetstextureAssets=JsonUtility.FromJson<TextureAssets>(loaderJSON.text);Texture2Dt=loaderTexture.texture;// prevent creating a new Texture2D each time.foreach(TextureAssettextureAssetintextureAssets.assets)mSprites.Add(textureAsset.name,Sprite.Create(t,newRect(textureAsset.x,textureAsset.y,textureAsset.width,textureAsset.height),Vector2.zero,pixelsPerUnit,0,SpriteMeshType.FullRect));}yieldreturnnull;OnProcessCompleted.Invoke();}publicvoidDispose(){foreach(varassetinmSprites)Destroy(asset.Value.texture);mSprites.Clear();}voidDestroy(){Dispose();}publicSpriteGetSprite(stringid){Spritesprite=null;mSprites.TryGetValue(id,outsprite);returnsprite;}publicSprite[]GetSprites(stringprefix){List<string>spriteNames=newList<string>();foreach(varassetinmSprites)if(asset.Key.StartsWith(prefix))spriteNames.Add(asset.Key);spriteNames.Sort(StringComparer.Ordinal);List<Sprite>sprites=newList<Sprite>();Spritesprite;for(inti=0;i<spriteNames.Count;++i){mSprites.TryGetValue(spriteNames[i],outsprite);sprites.Add(sprite);}returnsprites.ToArray();}}}
The text was updated successfully, but these errors were encountered:
Hey, what was the bug with garbage rect? I'm currently using this - but haven't encountered a bug or issue with it. Just wondering what you are fixing here. Thanks!
Hello, your scripts are very usefule but there is a bug on the garbageRect list in AssetPacker.cs. Rectangle indexed are not corresponding. Here the corrected script :
The text was updated successfully, but these errors were encountered: