/// <summary>
        /// Assigns new diced sprite data.
        /// </summary>
        public void SetDicedSprite(DicedSprite newDicedSprite)
        {
            #if UNITY_EDITOR
            // Reset sprite after it's data was modified (usually when rebuilding atlas).
            if (DicedSprite)
            {
                DicedSprite.OnModified -= SetDicedSprite;
            }
            if (newDicedSprite)
            {
                newDicedSprite.OnModified += SetDicedSprite;
            }
            #endif

            dicedSprite = newDicedSprite;

            if (!DicedSprite)
            {
                if (Mesh.vertexCount > 0)
                {
                    Mesh.Clear();
                }
                return;
            }

            DicedSprite.FillMesh(Mesh);
            SetMaterialMainTex(DicedSprite.AtlasTexture);
        }
        void ReferenceChildAtlas()
        {
            var path = AssetDatabase.GetAssetPath(TargetAtlas);

            //Debug.Log(path);
            UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
            for (int i = 0; i < assets.Length; i++)
            {
                if (assets[i] is DicedSprite)
                {
                    DicedSprite item = assets[i] as DicedSprite;
                    if (item.AtlasTexture == null)
                    {
                        item.AtlasTextureSet = atlasTexturesProperty.GetArrayElementAtIndex(0).objectReferenceValue as Texture2D;
                    }
                }
            }

            AssetDatabase.SaveAssets();
        }
        void RemoveNullRefAssets()
        {
            var path = AssetDatabase.GetAssetPath(TargetAtlas);

            //Debug.Log(path);
            UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
            for (int i = 0; i < assets.Length; i++)
            {
                if (assets[i] is DicedSprite)
                {
                    DicedSprite item = assets[i] as DicedSprite;
                    if (item.AtlasTexture == null)
                    {
                        var subAssetPath = AssetDatabase.GetAssetPath(assets[i]);
                        //Debug.Log(assets[i].name + " , : " + subAssetPath);
                        UnityEngine.Object.DestroyImmediate(assets[i], true);
                    }
                }
            }

            AssetDatabase.SaveAssets();
        }
        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);
            }
        }