Ejemplo n.º 1
0
    /// <summary>
    /// Replace the sprites within the atlas.
    /// </summary>

    static public void ReplaceSprites(INGUIAtlas atlas, List <SpriteEntry> sprites)
    {
        if (atlas == null)
        {
            return;
        }

        // Get the list of sprites we'll be updating
        List <UISpriteData> kept = new List <UISpriteData>();
        var spriteList           = atlas.spriteList;

        // Run through all the textures we added and add them as sprites to the atlas
        for (int i = 0; i < sprites.Count; ++i)
        {
            var se     = sprites[i];
            var sprite = AddSprite(spriteList, se);
            kept.Add(sprite);
        }

        // Remove unused sprites
        for (int i = spriteList.Count; i > 0;)
        {
            var sp = spriteList[--i];
            if (!kept.Contains(sp))
            {
                spriteList.RemoveAt(i);
            }
        }

        // Sort the sprites so that they are alphabetical within the atlas
        atlas.SortAlphabetically();
        atlas.MarkAsChanged();
    }
Ejemplo n.º 2
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();
    }
Ejemplo n.º 3
0
    // Token: 0x060006FD RID: 1789 RVA: 0x000394C8 File Offset: 0x000376C8
    public void MarkAsChanged()
    {
        INGUIAtlas replacement = this.replacement;

        if (replacement != null)
        {
            replacement.MarkAsChanged();
        }
        UISprite[] array = NGUITools.FindActive <UISprite>();
        int        i     = 0;
        int        num   = array.Length;

        while (i < num)
        {
            UISprite uisprite = array[i];
            if (NGUITools.CheckIfRelated(this, uisprite.atlas))
            {
                INGUIAtlas atlas = uisprite.atlas;
                uisprite.atlas = null;
                uisprite.atlas = atlas;
            }
            i++;
        }
        NGUIFont[] array2 = Resources.FindObjectsOfTypeAll <NGUIFont>();
        int        j      = 0;
        int        num2   = array2.Length;

        while (j < num2)
        {
            NGUIFont nguifont = array2[j];
            if (nguifont.atlas != null && NGUITools.CheckIfRelated(this, nguifont.atlas))
            {
                INGUIAtlas atlas2 = nguifont.atlas;
                nguifont.atlas = null;
                nguifont.atlas = atlas2;
            }
            j++;
        }
        UIFont[] array3 = Resources.FindObjectsOfTypeAll <UIFont>();
        int      k      = 0;
        int      num3   = array3.Length;

        while (k < num3)
        {
            UIFont uifont = array3[k];
            if (NGUITools.CheckIfRelated(this, uifont.atlas))
            {
                INGUIAtlas atlas3 = uifont.atlas;
                uifont.atlas = null;
                uifont.atlas = atlas3;
            }
            k++;
        }
        UILabel[] array4 = NGUITools.FindActive <UILabel>();
        int       l      = 0;
        int       num4   = array4.Length;

        while (l < num4)
        {
            UILabel uilabel = array4[l];
            if (uilabel.atlas != null && NGUITools.CheckIfRelated(this, uilabel.atlas))
            {
                INGUIAtlas atlas4     = uilabel.atlas;
                INGUIFont  bitmapFont = uilabel.bitmapFont;
                uilabel.bitmapFont = null;
                uilabel.bitmapFont = bitmapFont;
            }
            l++;
        }
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Draw the inspector widget.
    /// </summary>

    public override void OnInspectorGUI()
    {
        NGUIEditorTools.SetLabelWidth(80f);

        if (mAtlas == null)
        {
            EditorGUILayout.HelpBox("Invalid asset. Please re-create it.", MessageType.Error, true);
            return;
        }

        UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;

        GUILayout.Space(6f);

        if (mAtlas.replacement != null)
        {
            mType        = AtlasType.Reference;
            mReplacement = mAtlas.replacement;
        }

        GUILayout.BeginHorizontal();
        AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);

        NGUIEditorTools.DrawPadding();
        GUILayout.EndHorizontal();

        if (mType != after)
        {
            if (after == AtlasType.Normal)
            {
                mType = AtlasType.Normal;
                OnSelectAtlas(null);
            }
            else
            {
                mType = AtlasType.Reference;
            }
        }

        if (mType == AtlasType.Reference)
        {
            ComponentSelector.Draw(mAtlas.replacement, OnSelectAtlas, true);

            GUILayout.Space(6f);
            EditorGUILayout.HelpBox("You can have one atlas simply point to " +
                                    "another one. This is useful if you want to be " +
                                    "able to quickly replace the contents of one " +
                                    "atlas with another one, for example for " +
                                    "swapping an SD atlas with an HD one, or " +
                                    "replacing an English atlas with a Chinese " +
                                    "one. All the sprites referencing this atlas " +
                                    "will update their references to the new one.", MessageType.Info);

            if (mReplacement != (mAtlas as INGUIAtlas) && mAtlas.replacement != mReplacement)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas as Object);
                mAtlas.replacement = mReplacement;
                NGUITools.SetDirty(mAtlas as Object);
            }
            return;
        }

        if (mAtlas is UIAtlas)
        {
            EditorGUILayout.HelpBox("Legacy atlas type should be upgraded in order to maintain compatibility with Unity 2018 and newer.", MessageType.Warning, true);

            if (GUILayout.Button("Upgrade"))
            {
                var path = EditorUtility.SaveFilePanelInProject("Save As", (mAtlas as Object).name + ".asset", "asset", "Save atlas as...", NGUISettings.currentPath);

                if (!string.IsNullOrEmpty(path))
                {
                    NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
                    var asset = ScriptableObject.CreateInstance <NGUIAtlas>();
                    asset.spriteList     = mAtlas.spriteList;
                    asset.spriteMaterial = mAtlas.spriteMaterial;

                    var atlasName = path.Replace(".asset", "");
                    atlasName  = atlasName.Substring(path.LastIndexOfAny(new char[] { '/', '\\' }) + 1);
                    asset.name = atlasName;

                    AssetDatabase.CreateAsset(asset, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

                    asset = AssetDatabase.LoadAssetAtPath <NGUIAtlas>(path);
                    NGUISettings.atlas     = asset;
                    Selection.activeObject = NGUISettings.atlas as Object;

                    if (asset != null)
                    {
                        mAtlas.replacement = asset;
                        mAtlas.MarkAsChanged();
                    }
                }
            }
        }

        //GUILayout.Space(6f);
        Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;

        if (mAtlas.spriteMaterial != mat)
        {
            NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas as Object);
            mAtlas.spriteMaterial = mat;

            // Ensure that this atlas has valid import settings
            if (mAtlas.texture != null)
            {
                NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
            }

            mAtlas.MarkAsChanged();
        }

        if (mat != null)
        {
            TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;

            if (ta != null)
            {
                // Ensure that this atlas has valid import settings
                if (mAtlas.texture != null)
                {
                    NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
                }

                NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas as Object);
                NGUIJson.LoadSpriteData(mAtlas, ta);
                if (sprite != null)
                {
                    sprite = mAtlas.GetSprite(sprite.name);
                }
                mAtlas.MarkAsChanged();
            }

            float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));

            if (pixelSize != mAtlas.pixelSize)
            {
                NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas as Object);
                mAtlas.pixelSize = pixelSize;
            }
        }

        if (mAtlas.spriteMaterial != null)
        {
            Color blueColor  = new Color(0f, 0.7f, 1f, 1f);
            Color greenColor = new Color(0.4f, 1f, 0f, 1f);

            if (sprite == null && mAtlas.spriteList.Count > 0)
            {
                string spriteName = NGUISettings.selectedSprite;
                if (!string.IsNullOrEmpty(spriteName))
                {
                    sprite = mAtlas.GetSprite(spriteName);
                }
                if (sprite == null)
                {
                    sprite = mAtlas.spriteList[0];
                }
            }

            if (sprite != null)
            {
                if (sprite == null)
                {
                    return;
                }

                Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;

                if (tex != null)
                {
                    if (!NGUIEditorTools.DrawHeader("Sprite Details"))
                    {
                        return;
                    }

                    NGUIEditorTools.BeginContents();

                    GUILayout.Space(3f);
                    NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
                    GUILayout.Space(6f);

                    GUI.changed = false;

                    GUI.backgroundColor = greenColor;
                    NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
                    NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = blueColor;
                    NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
                    NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                    EditorGUILayout.Separator();
                    GUI.backgroundColor = Color.white;
                    NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
                    NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);

                    if (GUI.changed)
                    {
                        NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas as Object);

                        sprite.x      = sizeA.x;
                        sprite.y      = sizeA.y;
                        sprite.width  = sizeB.x;
                        sprite.height = sizeB.y;

                        sprite.paddingLeft   = padA.x;
                        sprite.paddingRight  = padA.y;
                        sprite.paddingBottom = padB.x;
                        sprite.paddingTop    = padB.y;

                        sprite.borderLeft   = borderA.x;
                        sprite.borderRight  = borderA.y;
                        sprite.borderBottom = borderB.x;
                        sprite.borderTop    = borderB.y;

                        MarkSpriteAsDirty();
                    }

                    GUILayout.Space(3f);

                    GUILayout.BeginHorizontal();

                    if (GUILayout.Button("Duplicate"))
                    {
                        UIAtlasMaker.SpriteEntry se = UIAtlasMaker.DuplicateSprite(mAtlas, sprite.name);
                        if (se != null)
                        {
                            NGUISettings.selectedSprite = se.name;
                        }
                    }

                    if (GUILayout.Button("Save As..."))
                    {
                        string path = EditorUtility.SaveFilePanel("Save As",
                                                                  NGUISettings.currentPath, sprite.name + ".png", "png");

                        if (!string.IsNullOrEmpty(path))
                        {
                            NGUISettings.currentPath = System.IO.Path.GetDirectoryName(path);
                            var se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);

                            if (se != null)
                            {
                                var bytes = se.tex.EncodeToPNG();
                                File.WriteAllBytes(path, bytes);
                                //AssetDatabase.ImportAsset(path);
                                if (se.temporaryTexture)
                                {
                                    DestroyImmediate(se.tex);
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.DrawHeader("Modify"))
                {
                    NGUIEditorTools.BeginContents();

                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginVertical();

                    NGUISettings.backgroundColor = EditorGUILayout.ColorField("Background", NGUISettings.backgroundColor);

                    if (GUILayout.Button("Add a Shadow"))
                    {
                        AddShadow(sprite);
                    }
                    if (GUILayout.Button("Add a Soft Outline"))
                    {
                        AddOutline(sprite);
                    }

                    if (GUILayout.Button("Add a Transparent Border"))
                    {
                        AddTransparentBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Clamped Border"))
                    {
                        AddClampedBorder(sprite);
                    }
                    if (GUILayout.Button("Add a Tiled Border"))
                    {
                        AddTiledBorder(sprite);
                    }
                    EditorGUI.BeginDisabledGroup(!sprite.hasBorder);
                    if (GUILayout.Button("Crop Border"))
                    {
                        CropBorder(sprite);
                    }
                    EditorGUI.EndDisabledGroup();

                    EditorGUILayout.EndVertical();
                    GUILayout.Space(20f);
                    EditorGUILayout.EndHorizontal();

                    NGUIEditorTools.EndContents();
                }

                if (NGUIEditorTools.previousSelection != null)
                {
                    GUILayout.Space(3f);
                    GUI.backgroundColor = Color.green;

                    if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
                    {
                        NGUIEditorTools.SelectPrevious();
                    }
                    GUI.backgroundColor = Color.white;
                }
            }
        }
    }