コード例 #1
0
        /// <summary>
        /// Get cached material for GuiAtlas.
        /// </summary>
        /// <param name="atlas">GuiAtlas instance.</param>
        public Material GetMaterial(GuiAtlas atlas)
        {
            if (atlas == null)
            {
                return(null);
            }
            Material mtrl;

            if (!_mtrlCache.ContainsKey(atlas))
            {
                mtrl             = new Material(Shader.Find(atlas.AlphaTexture != null ? StandardShaderName : OpaqueShaderName));
                mtrl.mainTexture = atlas.ColorTexture;
                mtrl.SetTexture("_AlphaTex", atlas.AlphaTexture);
                mtrl.hideFlags    = HideFlags.DontSave | HideFlags.HideInInspector;
                _mtrlCache[atlas] = mtrl;
            }
            else
            {
                mtrl = _mtrlCache[atlas];
            }
            UpdateMaterial(mtrl);
            return(mtrl);
        }
コード例 #2
0
        public static string BakeAtlas(GuiAtlas atlas, string fileName)
        {
            if (!AssetDatabase.Contains (atlas)) {
                return "Atlas should be saved as asset";
            }
            if (string.IsNullOrEmpty (fileName)) {
                return "Sprite folder not selected";
            }

            // cleanup.
            try {
                if (atlas.ColorTexture != null && AssetDatabase.Contains (atlas.ColorTexture)) {
                    var t = atlas.ColorTexture;
                    atlas.ColorTexture = null;
                    AssetDatabase.DeleteAsset (AssetDatabase.GetAssetPath (t));
                }
            } catch {
            }
            try {
                if (atlas.AlphaTexture != null && AssetDatabase.Contains (atlas.AlphaTexture)) {
                    var t = atlas.AlphaTexture;
                    atlas.AlphaTexture = null;
                    AssetDatabase.DeleteAsset (AssetDatabase.GetAssetPath (t));
                }
            } catch {
            }

            AssetDatabase.Refresh ();

            var sprites = new List<Texture2D> ();

            foreach (var spriteFileName in Directory.GetFiles (fileName, "*.png", SearchOption.TopDirectoryOnly)) {
                var spriteTex = new Texture2D (2, 2);
                spriteTex.LoadImage (File.ReadAllBytes (spriteFileName));
                spriteTex.Apply ();
                spriteTex.name = Path.GetFileNameWithoutExtension (spriteFileName);
                sprites.Add (spriteTex);
            }

            var atlasTex = new Texture2D (2, 2, TextureFormat.ARGB32, false);
            atlasTex.hideFlags = HideFlags.HideAndDontSave;

            Rect[] rects;

            if (sprites.Count == 1) {
                // special case, one texture
                rects = new Rect[] { new Rect (0, 0, 1f, 1f) };
                atlasTex.LoadImage (sprites[0].EncodeToPNG ());
                atlasTex.Apply ();
            } else {
                rects = atlasTex.PackTextures (sprites.ToArray (), 2, 2048);
            }

            var tex = new Texture2D (atlasTex.width, atlasTex.height, TextureFormat.RGB24, false);
            tex.hideFlags = HideFlags.HideAndDontSave;
            var srcColors = atlasTex.GetPixels32 ();
            DestroyImmediate (atlasTex);
            atlasTex = null;

            var dstAlphas = new Color32[srcColors.Length];
            Color32 c;
            for (int i = 0; i < srcColors.Length; i++) {
                c = srcColors[i];
                dstAlphas[i] = new Color32 (c.a, c.a, c.a, 255);
            }
            tex.SetPixels32 (srcColors);
            var colorData = tex.EncodeToPNG ();
            tex.SetPixels32 (dstAlphas);
            var alphaData = tex.EncodeToPNG ();
            DestroyImmediate (tex);
            tex = null;

            try {
                var srcAtlasPath = AssetDatabase.GetAssetPath (atlas);
                EditorUtility.DisplayProgressBar ("Save and import atlas data", "Color data processing...", 1f);
                var atlasPath = Path.ChangeExtension (srcAtlasPath, "color.png");
                File.WriteAllBytes (Path.Combine (Path.Combine (Application.dataPath, ".."), atlasPath), colorData);

                EditorUtility.DisplayProgressBar ("Save and import atlas data", "Alpha data processing...", 1f);
                var alphaAtlasPath = Path.ChangeExtension (srcAtlasPath, "alpha.png");
                File.WriteAllBytes (Path.Combine (Path.Combine (Application.dataPath, ".."), alphaAtlasPath), alphaData);

                EditorUtility.DisplayProgressBar ("Save and import atlas data", "Import processed data...", 1f);
                AssetDatabase.Refresh ();
                atlasTex = FixAtlasImport (atlasPath);
                atlas.ColorTexture = atlasTex;
                atlas.AlphaTexture = FixAtlasImport (alphaAtlasPath);

                var atlasSprites = new List<GuiSpriteData> (sprites.Count);
                float atlasWidth = atlasTex.width;
                float atlasHeight = atlasTex.height;
                for (int i = 0, iMax = sprites.Count; i < iMax; i++) {
                    var sprData = new GuiSpriteData ();
                    var sprName = sprites[i].name;

                    // slicing
                    var match = _slicedMask.Match (sprName.ToLowerInvariant ());
                    if (match.Success) {
                        sprName = sprName.Replace (match.Value, string.Empty);
                        sprData.BorderL = int.Parse (match.Groups["left"].Value) / atlasWidth;
                        sprData.BorderT = int.Parse (match.Groups["top"].Value) / atlasHeight;
                        sprData.BorderR = int.Parse (match.Groups["right"].Value) / atlasWidth;
                        sprData.BorderB = int.Parse (match.Groups["bottom"].Value) / atlasHeight;
                    } else {
                        sprData.BorderL = 0;
                        sprData.BorderT = 0;
                        sprData.BorderR = 0;
                        sprData.BorderB = 0;
                    }

                    sprData.Name = sprName;
                    sprData.CornerX = rects[i].x;
                    sprData.CornerY = rects[i].y;
                    sprData.CornerW = rects[i].width;
                    sprData.CornerH = rects[i].height;
                    atlasSprites.Add (sprData);
                }

                atlas.Sprites = atlasSprites.ToArray ();

                EditorUtility.SetDirty (atlas);

                AssetDatabase.SaveAssets ();
                AssetDatabase.Refresh ();

                atlas.ResetCache ();

                foreach (var panel in FindObjectsOfType<GuiPanel> ()) {
                    panel.ResetMaterialCache ();
                }
                foreach (var vis in FindObjectsOfType<GuiWidget> ()) {
                    vis.SetDirty (GuiDirtyType.Geometry);
                    EditorUtility.SetDirty (vis);
                }
            } finally {
                EditorUtility.ClearProgressBar ();

                for (var i = sprites.Count - 1; i >= 0; i--) {
                    DestroyImmediate (sprites[i]);
                    sprites.RemoveAt (i);
                }
            }

            return null;
        }
コード例 #3
0
 public static string BakeAtlas(GuiAtlas atlas)
 {
     var fileName = EditorUtility.OpenFolderPanel ("Select sprites folder", Application.dataPath, string.Empty);
     return BakeAtlas (atlas, fileName);
 }
コード例 #4
0
        public static string BakeAtlas(GuiAtlas atlas, string fileName)
        {
            if (!AssetDatabase.Contains(atlas))
            {
                return("Atlas should be saved as asset");
            }
            if (string.IsNullOrEmpty(fileName))
            {
                return("Sprite folder not selected");
            }

            // cleanup.
            try {
                if (atlas.ColorTexture != null && AssetDatabase.Contains(atlas.ColorTexture))
                {
                    var t = atlas.ColorTexture;
                    atlas.ColorTexture = null;
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(t));
                }
            } catch {
            }
            try {
                if (atlas.AlphaTexture != null && AssetDatabase.Contains(atlas.AlphaTexture))
                {
                    var t = atlas.AlphaTexture;
                    atlas.AlphaTexture = null;
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(t));
                }
            } catch {
            }

            AssetDatabase.Refresh();

            var sprites = new List <Texture2D> ();

            foreach (var spriteFileName in Directory.GetFiles(fileName, "*.png", SearchOption.TopDirectoryOnly))
            {
                var spriteTex = new Texture2D(2, 2);
                spriteTex.LoadImage(File.ReadAllBytes(spriteFileName));
                spriteTex.Apply();
                spriteTex.name = Path.GetFileNameWithoutExtension(spriteFileName);
                sprites.Add(spriteTex);
            }

            var atlasTex = new Texture2D(2, 2, TextureFormat.ARGB32, false);

            atlasTex.hideFlags = HideFlags.HideAndDontSave;

            Rect[] rects;

            if (sprites.Count == 1)
            {
                // special case, one texture
                rects = new Rect[] { new Rect(0, 0, 1f, 1f) };
                atlasTex.LoadImage(sprites[0].EncodeToPNG());
                atlasTex.Apply();
            }
            else
            {
                rects = atlasTex.PackTextures(sprites.ToArray(), 2, 2048);
            }

            var tex = new Texture2D(atlasTex.width, atlasTex.height, TextureFormat.RGB24, false);

            tex.hideFlags = HideFlags.HideAndDontSave;
            var srcColors = atlasTex.GetPixels32();

            DestroyImmediate(atlasTex);
            atlasTex = null;

            var     dstAlphas = new Color32[srcColors.Length];
            Color32 c;

            for (int i = 0; i < srcColors.Length; i++)
            {
                c            = srcColors[i];
                dstAlphas[i] = new Color32(c.a, c.a, c.a, 255);
            }
            tex.SetPixels32(srcColors);
            var colorData = tex.EncodeToPNG();

            tex.SetPixels32(dstAlphas);
            var alphaData = tex.EncodeToPNG();

            DestroyImmediate(tex);
            tex = null;

            try {
                var srcAtlasPath = AssetDatabase.GetAssetPath(atlas);
                EditorUtility.DisplayProgressBar("Save and import atlas data", "Color data processing...", 1f);
                var atlasPath = Path.ChangeExtension(srcAtlasPath, "color.png");
                File.WriteAllBytes(Path.Combine(Path.Combine(Application.dataPath, ".."), atlasPath), colorData);

                EditorUtility.DisplayProgressBar("Save and import atlas data", "Alpha data processing...", 1f);
                var alphaAtlasPath = Path.ChangeExtension(srcAtlasPath, "alpha.png");
                File.WriteAllBytes(Path.Combine(Path.Combine(Application.dataPath, ".."), alphaAtlasPath), alphaData);

                EditorUtility.DisplayProgressBar("Save and import atlas data", "Import processed data...", 1f);
                AssetDatabase.Refresh();
                atlasTex           = FixAtlasImport(atlasPath);
                atlas.ColorTexture = atlasTex;
                atlas.AlphaTexture = FixAtlasImport(alphaAtlasPath);

                var   atlasSprites = new List <GuiSpriteData> (sprites.Count);
                float atlasWidth   = atlasTex.width;
                float atlasHeight  = atlasTex.height;
                for (int i = 0, iMax = sprites.Count; i < iMax; i++)
                {
                    var sprData = new GuiSpriteData();
                    var sprName = sprites[i].name;
                    // slicing
                    var match = _slicedMask.Match(sprName.ToLowerInvariant());
                    if (match.Success)
                    {
                        sprName         = sprName.Replace(match.Value, string.Empty);
                        sprData.BorderL = int.Parse(match.Groups["left"].Value) / atlasWidth;
                        sprData.BorderT = int.Parse(match.Groups["top"].Value) / atlasHeight;
                        sprData.BorderR = int.Parse(match.Groups["right"].Value) / atlasWidth;
                        sprData.BorderB = int.Parse(match.Groups["bottom"].Value) / atlasHeight;
                    }
                    else
                    {
                        sprData.BorderL = 0;
                        sprData.BorderT = 0;
                        sprData.BorderR = 0;
                        sprData.BorderB = 0;
                    }

                    sprData.Name    = sprName;
                    sprData.CornerX = rects[i].x;
                    sprData.CornerY = rects[i].y;
                    sprData.CornerW = rects[i].width;
                    sprData.CornerH = rects[i].height;
                    atlasSprites.Add(sprData);
                }

                atlas.Sprites = atlasSprites.ToArray();

                EditorUtility.SetDirty(atlas);

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();

                atlas.ResetCache();

                foreach (var panel in FindObjectsOfType <GuiPanel>())
                {
                    panel.ResetMaterialCache();
                }
                foreach (var vis in FindObjectsOfType <GuiWidget>())
                {
                    vis.SetDirty(GuiDirtyType.Geometry);
                    EditorUtility.SetDirty(vis);
                }
            } finally {
                EditorUtility.ClearProgressBar();

                for (var i = sprites.Count - 1; i >= 0; i--)
                {
                    DestroyImmediate(sprites[i]);
                    sprites.RemoveAt(i);
                }
            }

            return(null);
        }
コード例 #5
0
        public static string BakeAtlas(GuiAtlas atlas)
        {
            var fileName = EditorUtility.OpenFolderPanel("Select sprites folder", Application.dataPath, string.Empty);

            return(BakeAtlas(atlas, fileName));
        }