Exemple #1
0
    /// <summary>
    /// Extract sprites from the atlas, adding them to the list.
    /// </summary>

    static public void ExtractSprites(UIAtlas atlas, List <SpriteEntry> finalSprites)
    {
        ShowProgress(0f);

        TextureAlphaSpliter.UseRgbaTex(atlas);
        // Make the atlas texture readable
        Texture2D tex = NGUIEditorTools.ImportTexture(atlas.texture, true, true, false);

        if (tex != null)
        {
            Color32[]           pixels  = null;
            int                 width   = tex.width;
            int                 height  = tex.height;
            List <UISpriteData> sprites = atlas.spriteList;
            float               count   = sprites.Count;
            int                 index   = 0;

            foreach (UISpriteData es in sprites)
            {
                ShowProgress((index++) / count);

                bool found = false;

                foreach (SpriteEntry fs in finalSprites)
                {
                    if (es.name == fs.name)
                    {
                        fs.CopyBorderFrom(es);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    if (pixels == null)
                    {
                        pixels = tex.GetPixels32();
                    }
                    SpriteEntry sprite = ExtractSprite(es, pixels, width, height);
                    if (sprite != null)
                    {
                        finalSprites.Add(sprite);
                    }
                }
            }
        }

        // The atlas no longer needs to be readable
        NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
        ShowProgress(1f);
    }
Exemple #2
0
    /// <summary>
    /// Combine all sprites into a single texture and save it to disk.
    /// </summary>

    static public bool UpdateTexture(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the texture for the atlas
        Texture2D tex     = atlas.texture as Texture2D;
        bool      success = TextureAlphaSpliter.UseRgbaTex(atlas);

        if (success)
        {
            tex = atlas.texture as Texture2D;
        }
        //bool isRGB = tex != null && tex.name.Contains("(rgb)");
        //if (isRGB)
        //{
        //    var srcTex = TextureAlphaSpliter.GetUIRgbaTex(tex);
        //    if (srcTex != null) {
        //    tex = srcTex;
        //    atlas.spriteMaterial.SetTexture("_MainTex", tex);
        //    }
        //    else
        //    {
        //        Debug.LogErrorFormat("找不到图集={0}的原图(rgba)", atlas.name);
        //    }
        //}
        string oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        string newPath = NGUIEditorTools.GetSaveableTexturePath(atlas);

        // Clear the read-only flag in texture file attributes
        if (System.IO.File.Exists(newPath))
        {
#if !UNITY_4_1 && !UNITY_4_0 && !UNITY_3_5
            if (!AssetDatabase.IsOpenForEdit(newPath))
            {
                Debug.LogError(newPath + " is not editable. Did you forget to do a check out?");
                return(false);
            }
#endif
            System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
            newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
            System.IO.File.SetAttributes(newPath, newPathAttrs);
        }

        bool newTexture = (tex == null || oldPath != newPath);

        if (newTexture)
        {
            // Create a new texture for the atlas
            tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        }
        else
        {
            // Make the atlas readable so we can save it
            tex = NGUIEditorTools.ImportTexture(oldPath, true, false, false);
        }

        // Pack the sprites into this texture
        if (PackTextures(tex, sprites))
        {
            byte[] bytes = tex.EncodeToPNG();
            System.IO.File.WriteAllBytes(newPath, bytes);
            bytes = null;

            // Load the texture we just saved as a Texture2D
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            tex = NGUIEditorTools.ImportTexture(newPath, false, true, !atlas.premultipliedAlpha);

            // Update the atlas texture
            if (newTexture)
            {
                if (tex == null)
                {
                    Debug.LogError("Failed to load the created atlas saved as " + newPath);
                }
                else
                {
                    atlas.spriteMaterial.mainTexture = tex;
                }
                ReleaseSprites(sprites);

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            }
            return(true);
        }
        else
        {
            if (!newTexture)
            {
                NGUIEditorTools.ImportTexture(oldPath, false, true, !atlas.premultipliedAlpha);
            }

            //Debug.LogError("Operation canceled: The selected sprites can't fit into the atlas.\n" +
            //	"Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead.");

            EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can't fit into the atlas.\n" +
                                        "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead", "OK");
            return(false);
        }
    }