コード例 #1
0
ファイル: UIAtlasMaker.cs プロジェクト: Bmak/PalantirZone
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static public void UpdateAtlas(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(atlas, sprites))
            {
                // Replace the sprites within the atlas
                ReplaceSprites(atlas, sprites);
            }

            // Release the temporary textures
            ReleaseSprites(sprites);
            EditorUtility.ClearProgressBar();
            return;
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }

        atlas.MarkAsChanged();
        Selection.activeGameObject = (NGUISettings.atlas != null) ? NGUISettings.atlas.gameObject : null;
        EditorUtility.ClearProgressBar();
    }
コード例 #2
0
ファイル: UIAtlasMaker.cs プロジェクト: icywind/DiceGame
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static void UpdateAtlas(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            UpdateTexture(atlas, sprites);

            // Replace the sprites within the atlas
            ReplaceSprites(atlas, sprites);

            // Release the temporary textures
            ReleaseSprites(sprites);
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }
        EditorUtility.SetDirty(atlas.gameObject);
    }
コード例 #3
0
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static void UpdateAtlas(NGUIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(atlas, sprites))
            {
                // Replace the sprites within the atlas
                ReplaceSprites(atlas, sprites);

                // Release the temporary textures
                ReleaseSprites(sprites);
            }
            else
            {
                return;
            }
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }
        atlas.MarkAsDirty();

        Debug.Log("The atlas has been updated. Don't forget to save the scene to write the changes!");
    }
コード例 #4
0
    /// <summary>
    /// Combine all sprites into a single texture and save it to disk.
    /// </summary>

    static Texture2D UpdateTexture(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the texture for the atlas
        Texture2D tex     = atlas.texture as Texture2D;
        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))
        {
            System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
            newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
            System.IO.File.SetAttributes(newPath, newPathAttrs);
        }

        if (tex == null || oldPath != newPath)
        {
            // Create a new texture for the atlas
            tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);

            // Pack the sprites into this texture
            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.Refresh();
            tex = NGUIEditorTools.ImportTexture(newPath, false, true);

            // Update the atlas texture
            if (tex == null)
            {
                Debug.LogError("Failed to load the created atlas saved as " + newPath);
            }
            else
            {
                atlas.spriteMaterial.mainTexture = tex;
            }
        }
        else
        {
            // Make the atlas readable so we can save it
            tex = NGUIEditorTools.ImportTexture(oldPath, true, false);

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

            // Re-import the newly created texture, turning off the 'readable' flag
            AssetDatabase.Refresh();
            tex = NGUIEditorTools.ImportTexture(newPath, false, false);
        }
        AssetDatabase.SaveAssets();
        Resources.UnloadUnusedAssets();
        return(tex);
    }
コード例 #5
0
ファイル: UIAtlasMaker.cs プロジェクト: iuvei/hzmj_client
    /// <summary>
    /// Combine all sprites into a single texture and save it to disk.
    /// </summary>

    static Texture2D UpdateTexture(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the texture for the atlas
        Texture2D tex     = atlas.texture as Texture2D;
        string    oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        string    newPath = NGUIEditorTools.GetSaveableTexturePath(atlas);

        if (!newPath.Contains("_t.png"))
        {
            newPath  = newPath.Substring(0, newPath.Length - 4);
            newPath += "_t.png";
        }
        if (tex == null || oldPath != newPath)
        {
            // Create a new texture for the atlas
            tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);

            // Pack the sprites into this texture
            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.Refresh();
            tex = NGUIEditorTools.ImportTexture(newPath, false, true);

            // Update the atlas texture
            if (tex == null)
            {
                Debug.LogError("Failed to load the created atlas saved as " + newPath);
            }
            else
            {
                atlas.spriteMaterial.mainTexture = tex;
            }
        }
        else
        {
            // Make the atlas readable so we can save it
            tex = NGUIEditorTools.ImportTexture(oldPath, true, false);

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

            // Re-import the newly created texture, turning off the 'readable' flag
            AssetDatabase.Refresh();
            tex = NGUIEditorTools.ImportTexture(newPath, false, false);
        }
        return(tex);
    }
コード例 #6
0
ファイル: UIAtlasMaker.cs プロジェクト: justinlm/Unity_APP
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static public void UpdateAtlas(UIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(atlas, sprites))
            {
                // Replace the sprites within the atlas
                ReplaceSprites(atlas, sprites);
            }

            // Release the temporary textures
            ReleaseSprites(sprites);
            EditorUtility.ClearProgressBar();
            //process through seperating rgb and a
            Texture rgb, alpha;
            TextureAlphaSpliter.SplitAlpha(atlas.texture, false, out rgb, out alpha);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            Debug.Log("分离为rgb+alpha 贴图");
            //update mat
            var rgba = atlas.texture;
            var mat  = atlas.spriteMaterial;
            mat.shader = Shader.Find("Unlit/Transparent Colored Alpha");
            mat.SetTexture("_AlphaTex", alpha);
            mat.SetTexture("_MainTex", rgb);
            Debug.Log("重设material");
            //save src pic
            var src = AssetDatabase.GetAssetPath(rgba);
            TextureAlphaSpliter.SaveUIRgbaTexture(src);
            Debug.Log("保存rgba");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

            return;
        }
        else
        {
            atlas.spriteList.Clear();
            string path = NGUIEditorTools.GetSaveableTexturePath(atlas);
            atlas.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
        }

        atlas.MarkAsChanged();
        Selection.activeGameObject = (NGUISettings.atlas != null) ? NGUISettings.atlas.gameObject : null;
        EditorUtility.ClearProgressBar();
    }
コード例 #7
0
ファイル: UIAtlasMaker.cs プロジェクト: willFuRoy/Unity
    /// <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;
        string    oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        string    newPath = NGUIEditorTools.GetSaveableTexturePath(atlas);

        // 分离贴图,删除原来的贴图,重新创建
        if (NGUISettings.atlasSplit)
        {
            if (!string.IsNullOrEmpty(oldPath))
            {
                AssetDatabase.DeleteAsset(oldPath);
            }

            oldPath = "";
        }

        // 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);
        }
    }
コード例 #8
0
    /// <summary>
    /// Update the sprite atlas, keeping only the sprites that are on the specified list.
    /// </summary>

    static public void UpdateAtlas(INGUIAtlas obj, List <SpriteEntry> sprites)
    {
#if UNITY_2018_3_OR_NEWER
        // Contributed by B9 from https://discord.gg/tasharen
        if (obj is UIAtlas)         // Prefab-based atlas
        {
            Debug.LogWarning("Updating a legacy atlas: issues may occur. Please update the atlas to a new format that uses Scriptable Objects rather than Prefabs.", obj as Object);
            var atlas = (obj as UIAtlas);

            if (!PrefabUtility.IsPartOfPrefabAsset(atlas.gameObject))
            {
                Debug.LogWarning("Atlas is not sourced from prefab asset, ignoring the request to update it");
                return;
            }

            var assetPath = AssetDatabase.GetAssetPath(atlas.gameObject);

            if (string.IsNullOrEmpty(assetPath))
            {
                Debug.LogWarning("Atlas asset path could not be found, aborting");
                return;
            }

            var assetRoot = PrefabUtility.LoadPrefabContents(assetPath);
            var atlasTemp = assetRoot.GetComponent <UIAtlas>();

            if (atlasTemp == null)
            {
                Debug.LogWarning("Atlas component could not be found in the loaded prefab, aborting");
                PrefabUtility.UnloadPrefabContents(assetRoot);
                return;
            }

            if (sprites.Count > 0)
            {
                // Combine all sprites into a single texture and save it
                if (UpdateTexture(atlasTemp, sprites))
                {
                    // Replace the sprites within the atlas
                    ReplaceSprites(atlasTemp, sprites);
                }

                // Release the temporary textures
                ReleaseSprites(sprites);
            }
            else
            {
                atlasTemp.spriteList.Clear();
                var texturePath = NGUIEditorTools.GetSaveableTexturePath(atlasTemp);
                atlasTemp.spriteMaterial.mainTexture = null;
                if (!string.IsNullOrEmpty(texturePath))
                {
                    AssetDatabase.DeleteAsset(texturePath);
                }
            }

            PrefabUtility.SaveAsPrefabAsset(assetRoot, assetPath);
            Selection.activeObject = NGUISettings.atlas as Object;
            EditorUtility.ClearProgressBar();

            PrefabUtility.UnloadPrefabContents(assetRoot);
            AssetDatabase.Refresh();

            var assetUpdated = (GameObject)AssetDatabase.LoadMainAssetAtPath(assetPath);
            var newAtlas     = assetUpdated.GetComponent <UIAtlas>();
            NGUISettings.atlas = newAtlas;
            newAtlas.MarkAsChanged();

            var panels = NGUITools.FindActive <UIPanel>();

            foreach (var panel in panels)
            {
                if (!panel.enabled)
                {
                    continue;
                }
                panel.enabled = false;
                panel.enabled = true;
            }
            EditorUtility.CollectDependencies(panels);
            return;
        }
#endif
        if (sprites.Count > 0)
        {
            // Combine all sprites into a single texture and save it
            if (UpdateTexture(obj, sprites))
            {
                ReplaceSprites(obj, sprites);
            }
            ReleaseSprites(sprites);
        }
        else
        {
            obj.spriteList.Clear();
            var path = NGUIEditorTools.GetSaveableTexturePath(obj);
            obj.spriteMaterial.mainTexture = null;
            if (!string.IsNullOrEmpty(path))
            {
                AssetDatabase.DeleteAsset(path);
            }
            obj.MarkAsChanged();
        }

        EditorUtility.ClearProgressBar();
    }
コード例 #9
0
    /// <summary>
    /// Combine all sprites into a single texture and save it to disk.
    /// </summary>

    static public bool UpdateTexture(INGUIAtlas atlas, List <SpriteEntry> sprites)
    {
        // Get the texture for the atlas
        var tex     = atlasTexture;
        var oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        var newPath = NGUIEditorTools.GetSaveableTexturePath(atlas as Object, atlasTexture);

        // Clear the read-only flag in texture file attributes
        if (System.IO.File.Exists(newPath))
        {
            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, !premultipliedAlpha);

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

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            }
            return(true);
        }
        else
        {
            if (!newTexture)
            {
                NGUIEditorTools.ImportTexture(oldPath, false, true, !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);
        }
    }
コード例 #10
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;
        Texture2D alphaTex  = null;
        string    oldPath   = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
        string    newPath   = NGUIEditorTools.GetSaveableTexturePath(atlas);
        string    alphaPath = newPath.Replace(".png", "`alpha.png");

        // Clear the read-only flag in texture file attributes
        if (System.IO.File.Exists(newPath))
        {
            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
            if (tex.format.ToString().EndsWith("32"))
            {
                tex = NGUIEditorTools.ImportTexture(oldPath, true, false, false);
            }
            else
            {
                tex = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
            }
        }

        bool alphaSplit = NGUISettings.atlasAlphaSplit && !atlas.premultipliedAlpha;

        if (!alphaSplit)
        {
            alphaTex = NGUIEditorTools.ImportTexture(alphaPath, true, true, false);
            if (alphaTex != null)
            {
                tex = AlphaMerge(tex, alphaTex);

                byte[] bytes = tex.EncodeToPNG();
                System.IO.File.WriteAllBytes(newPath, bytes);
                bytes = null;

                AssetDatabase.SaveAssets();
                AssetDatabase.DeleteAsset(alphaPath);
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
                tex = NGUIEditorTools.ImportTexture(newPath, true, true, true);
            }
        }

        // Pack the sprites into this texture
        if (PackTextures(tex, sprites))
        {
            atlas.InitSize(tex);
            if (alphaSplit)
            {
                alphaTex = AlphaSplit(ref tex);

                byte[] alphaBytes = alphaTex.EncodeToPNG();
                System.IO.File.WriteAllBytes(alphaPath, alphaBytes);
            }

            byte[] bytes = tex.EncodeToPNG();
            System.IO.File.WriteAllBytes(newPath, bytes);

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

            // 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;
                    if (alphaTex != null)
                    {
                        atlas.spriteMaterial.SetTexture("_AlphaTex", alphaTex);
                    }
                }
                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);
        }
    }