public void Batch(CompletionCallback completion) { this.completion = completion; Debug.Assert(modelList.Count > 0); if (modelList.Count > 1) { int assetRootIndex = studio.path.directoryPath.IndexOf("Assets"); string dirPath = studio.path.directoryPath.Substring(assetRootIndex); batchingFolderPath = Path.Combine(dirPath, PathHelper.MakeDateTimeString()); Directory.CreateDirectory(batchingFolderPath); } ModelIndex = 0; EditorApplication.update -= UpdateState; EditorApplication.update += UpdateState; EditorUtility.DisplayProgressBar("Progress...", "Ready...", 0.0f); }
protected void BuildFolderPathAndCreate(string fileName) { string folderName = ""; if (parentFolderPath.Length > 0) { if (sIndex.Length > 0) { folderName += sIndex + "_"; } folderName += fileName; } else { int assetRootIndex = studio.path.directoryPath.IndexOf("Assets"); parentFolderPath = studio.path.directoryPath.Substring(assetRootIndex); folderName += fileName + "_" + PathHelper.MakeDateTimeString(); } folderPath = Path.Combine(parentFolderPath, folderName); Directory.CreateDirectory(folderPath); }
private static void Combine(bool onlySpriteName) { if (Selection.objects.Length < 2) { return; } List <Sprite> sprites = new List <Sprite>(); List <Texture2D> textures = new List <Texture2D>(); foreach (Object obj in Selection.objects) { if (!EditorUtility.IsPersistent(obj)) { continue; } string assetPath = AssetDatabase.GetAssetPath(obj); TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath); if (importer == null) { continue; } importer.isReadable = true; AssetDatabase.ImportAsset(assetPath); Texture2D atlasTex = obj as Texture2D; if (atlasTex == null) { continue; } Color32[] srcPixels = atlasTex.GetPixels32(); for (int metai = 0; metai < importer.spritesheet.Length; ++metai) { SpriteMetaData metaData = importer.spritesheet[metai]; int spriteWidth = (int)metaData.rect.width; int spriteHeight = (int)metaData.rect.height; Color32[] destPixels = new Color32[spriteWidth * spriteHeight]; for (int destIndex = 0; destIndex < spriteWidth * spriteHeight; destIndex++) { int x = destIndex % spriteWidth; int y = destIndex / spriteWidth; int srcIndex = ((int)metaData.rect.y + y) * atlasTex.width + ((int)metaData.rect.x + x); if (srcIndex < srcPixels.Length) { destPixels[destIndex] = srcPixels[srcIndex]; } } Texture2D spriteTex = new Texture2D(spriteWidth, spriteHeight, TextureFormat.ARGB32, false); spriteTex.SetPixels32(destPixels); spriteTex.Apply(); string spriteName = onlySpriteName ? metaData.name : atlasTex.name + "_" + metaData.name; Sprite sprite = new Sprite(spriteTex, spriteName, metaData.pivot); sprites.Add(sprite); textures.Add(spriteTex); } } Texture2D newAtlasTex = new Texture2D(8192, 8192, TextureFormat.ARGB32, false); Rect[] texRects = newAtlasTex.PackTextures(textures.ToArray(), 2, 8192); for (int i = 0; i < sprites.Count; i++) { Texture2D tex = sprites[i].tex; float newX = texRects[i].x * newAtlasTex.width; float newY = texRects[i].y * newAtlasTex.height; texRects[i] = new Rect(newX, newY, (float)tex.width, (float)tex.height); } string filePath = AssetDatabase.GetAssetPath(Selection.objects[0]); string dirPath = filePath.Remove(filePath.LastIndexOf('/')); string fileName = "CombinedSpritesheet_" + PathHelper.MakeDateTimeString(); filePath = TextureHelper.SaveTexture(dirPath, fileName, newAtlasTex); AssetDatabase.ImportAsset(filePath); TextureImporter texImporter = (TextureImporter)AssetImporter.GetAtPath(filePath); if (texImporter != null) { texImporter.textureType = TextureImporterType.Sprite; texImporter.spriteImportMode = SpriteImportMode.Multiple; texImporter.maxTextureSize = 4096; int texCount = sprites.Count; SpriteMetaData[] metaData = new SpriteMetaData[texCount]; for (int i = 0; i < texCount; i++) { metaData[i].name = sprites[i].name; metaData[i].rect = texRects[i]; metaData[i].alignment = (int)SpriteAlignment.Custom; metaData[i].pivot = sprites[i].pivot; } texImporter.spritesheet = metaData; AssetDatabase.ImportAsset(filePath); } foreach (Object obj in Selection.objects) { if (!EditorUtility.IsPersistent(obj)) { continue; } string assetPath = AssetDatabase.GetAssetPath(obj); TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath); if (importer == null) { continue; } importer.isReadable = false; AssetDatabase.ImportAsset(assetPath); } }