private void CreateDicedSprites(Dictionary <string, List <DicedUnit> > dicedUnits) { DisplayProgressBar("Generating diced sprites data...", 1f); // Generate diced sprites using diced units. var newDicedSprites = dicedUnits.Select(nameToUnits => DicedSprite.CreateInstance(nameToUnits.Key, nameToUnits.Value.First().AtlasTexture, nameToUnits.Value, defaultPivotProperty.vector2Value, keepOriginalPivotProperty.boolValue)).ToList(); // Save generated sprites. if (!decoupleSpriteDataProperty.boolValue) { // Delete generated sprites folder (in case it was previously created). if (!string.IsNullOrWhiteSpace(generatedSpritesFolderGuidProperty.stringValue)) { var folderPath = AssetDatabase.GUIDToAssetPath(generatedSpritesFolderGuidProperty.stringValue); if (AssetDatabase.IsValidFolder(folderPath)) { AssetDatabase.DeleteAsset(folderPath); } } // Update rebuilded sprites to preserve references and delete stale ones. var spritesToAdd = new List <DicedSprite>(newDicedSprites); for (int i = dicedSpritesProperty.arraySize - 1; i >= 0; i--) { var oldSprite = dicedSpritesProperty.GetArrayElementAtIndex(i).objectReferenceValue as DicedSprite; if (!oldSprite) { continue; } var newSprite = spritesToAdd.Find(sprite => sprite.Name == oldSprite.Name); if (newSprite) { newSprite.AtlasAsset = TargetAtlas; //Sora.add EditorUtility.CopySerialized(newSprite, oldSprite); spritesToAdd.Remove(newSprite); } else { DestroyImmediate(oldSprite, true); } } foreach (var spriteToAdd in spritesToAdd) { spriteToAdd.AtlasAsset = TargetAtlas; //Sora.add AssetDatabase.AddObjectToAsset(spriteToAdd, target); } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); dicedSpritesProperty.SetListValues(spritesToAdd, false); } else { // Delete sprites stored in atlas asset (in case they were previously added). for (int i = dicedSpritesProperty.arraySize - 1; i >= 0; i--) { DestroyImmediate(dicedSpritesProperty.GetArrayElementAtIndex(i).objectReferenceValue, true); } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); var folderPath = AssetDatabase.GetAssetPath(target).GetBeforeLast("/") + "/" + target.name; var dicedSpritesFolder = new FolderAssetHelper(folderPath); var savedDicedSprites = dicedSpritesFolder.SetContainedAssets(newDicedSprites); generatedSpritesFolderGuidProperty.stringValue = AssetDatabase.AssetPathToGUID(folderPath); dicedSpritesProperty.SetListValues(savedDicedSprites); } }
private static void SaveGeneratedSprites(List <Sprite> sprites, bool decoupleSpriteData, SerializedProperty generatedSpritesFolderGuidProperty, SerializedProperty spritesProperty, UnityEngine.Object target) { if (!decoupleSpriteData) { // Delete generated sprites folder (in case it was previously created). if (!string.IsNullOrWhiteSpace(generatedSpritesFolderGuidProperty.stringValue)) { var folderPath = AssetDatabase.GUIDToAssetPath(generatedSpritesFolderGuidProperty.stringValue); if (AssetDatabase.IsValidFolder(folderPath)) { AssetDatabase.DeleteAsset(folderPath); } } // Update rebuilt sprites to preserve references and delete stale ones. var spritesToAdd = new List <Sprite>(sprites); for (int i = spritesProperty.arraySize - 1; i >= 0; i--) { var oldSprite = spritesProperty.GetArrayElementAtIndex(i).objectReferenceValue as Sprite; if (!oldSprite) { continue; } var newSprite = spritesToAdd.Find(sprite => sprite.name == oldSprite.name); if (newSprite) { EditorUtility.CopySerialized(newSprite, oldSprite); spritesToAdd.Remove(newSprite); } else { DestroyImmediate(oldSprite, true); } } foreach (var spriteToAdd in spritesToAdd) { AssetDatabase.AddObjectToAsset(spriteToAdd, target); } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); spritesProperty.SetListValues(spritesToAdd, false); } else { // Delete sprites stored in atlas asset (in case they were previously added). for (int i = spritesProperty.arraySize - 1; i >= 0; i--) { DestroyImmediate(spritesProperty.GetArrayElementAtIndex(i).objectReferenceValue, true); } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); var folderPath = AssetDatabase.GetAssetPath(target).GetBeforeLast("/") + "/" + target.name; var dicedSpritesFolder = new FolderAssetHelper(folderPath); var savedDicedSprites = dicedSpritesFolder.SetContainedAssets(sprites); generatedSpritesFolderGuidProperty.stringValue = AssetDatabase.AssetPathToGUID(folderPath); spritesProperty.SetListValues(savedDicedSprites); } }