private static List <SpRect> CompileSources(List <SpSource> sources)
    {
        var rects = new List <SpRect>();

        for (var i = sources.Count - 1; i >= 0; i--)
        {
            var source        = sources[i];
            var sourceTexture = source.Texture;

            if (sourceTexture != null)
            {
                var sourcePath     = source.Path;
                var sourcePixels   = default(SpPixels);
                var sourceSprites  = source.Sprites;
                var sourceImporter = SpHelper.GetAssetImporter <TextureImporter>(sourcePath);

                if (sourceImporter != null)
                {
                    // Make the texture temporarily readable, or directly read pixels
                    if (sourceImporter.isReadable == false)
                    {
                        sourceImporter.isReadable = true; SpHelper.ReimportAsset(sourcePath);
                        {
                            sourcePixels = new SpPixels(sourceTexture);
                        }
                        sourceImporter.isReadable = false; SpHelper.ReimportAsset(sourcePath);
                    }
                    else
                    {
                        sourcePixels = new SpPixels(sourceTexture);
                    }

                    // Add sprites or whole texture
                    if (sourceSprites.Count > 0)
                    {
                        for (var j = 0; j < sourceSprites.Count; j++)
                        {
                            var sourceSprite = sourceSprites[j];

                            CompileRect(rects, source, sourcePixels.GetSubset(sourceSprite.rect), sourceSprite.name, sourceSprite);
                        }
                    }
                    else
                    {
                        CompileRect(rects, source, sourcePixels, sourceTexture.name);
                    }

                    continue;
                }
            }

            sources.RemoveAt(i);
        }

        rects.Sort((a, b) => Mathf.Max(b.W, b.H) - Mathf.Max(a.W, a.H));

        return(rects);
    }
Exemple #2
0
    private void UpdateTextureAsset(SpriteMetaData[] metaDatas, Texture2D tempTexture)
    {
        if (tempTexture == null)
        {
            throw new System.Exception("TempTexture is null");
        }

        var path     = default(string);
        var importer = default(TextureImporter);

        // Try and find the path of an existing
        if (string.IsNullOrEmpty(Identifier) == false)
        {
            path = AssetDatabase.GUIDToAssetPath(Identifier);
        }

        // Create asset texture for the first time?
        if (string.IsNullOrEmpty(path) == true)
        {
            path     = AssetDatabase.GetAssetPath(this);
            path     = path.Substring(0, path.Length - ".asset".Length);
            path     = AssetDatabase.GenerateUniqueAssetPath(path + ".png");
            importer = SpHelper.SaveTextureAsset(tempTexture, path, false);

            importer.maxTextureSize     = 8192;
            importer.textureCompression = TextureImporterCompression.Uncompressed;
        }
        // Update existing asset texture?
        else
        {
            importer = SpHelper.SaveTextureAsset(tempTexture, path, true);
        }

        // Update the atlas settings
        importer.textureType      = TextureImporterType.Sprite;
        importer.spriteImportMode = SpriteImportMode.Multiple;
        importer.spritesheet      = metaDatas;

        EditorUtility.SetDirty(importer);

        // Apply new settings
        SpHelper.ReimportAsset(path);

        // Update settings
        Texture    = AssetDatabase.LoadMainAssetAtPath(path) as Texture2D;
        Width      = Texture.width;
        Height     = Texture.height;
        Identifier = AssetDatabase.AssetPathToGUID(path);

        // Find all packed sprites
        Sprites.Clear();

        foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(path))
        {
            var sprite = asset as Sprite;

            if (sprite != null)
            {
                Sprites.Add(sprite);
            }
        }

        // Destroy temp texture
        DestroyImmediate(tempTexture);

        // Unmark dirty
        Dirty = false;

        Sources.ForEach(s => { s.Flag = SpFlag.None; s.Dirty = false; });
    }