示例#1
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; });
    }