Exemplo n.º 1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public override void OnInspectorGUI()
    {
        // ========================================================
        // Base GUI
        // ========================================================

        base.OnInspectorGUI();
        GUILayout.Space(20);

        // ========================================================
        // init values
        // ========================================================

        //
        bool needRebuild = false;

        // ========================================================
        // exGUIBorder object filed
        // ========================================================

        bool borderChanged = false;

        EditorGUI.indentLevel = 1;

        GUILayout.BeginHorizontal();
        GUI.enabled = !inAnimMode;
        exGUIBorder newGUIBorder = (exGUIBorder)EditorGUILayout.ObjectField("GUI Border"
                                                                            , editSpriteBorder.guiBorder
                                                                            , typeof(exGUIBorder)
                                                                            , false
                                                                            );

        if (newGUIBorder != editSpriteBorder.guiBorder)
        {
            borderChanged = true;
        }

        if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
        {
            exGUIBorderEditor editor = exGUIBorderEditor.NewWindow();
            editor.Edit(editSpriteBorder.guiBorder);
        }
        GUI.enabled = true;
        GUILayout.EndHorizontal();
        GUILayout.Space(5);

        // get edit texture
        Texture2D editTexture = null;

        if (newGUIBorder)
        {
            editTexture = exEditorHelper.LoadAssetFromGUID <Texture2D>(newGUIBorder.textureGUID);

            // ========================================================
            // border preview
            // ========================================================

            Rect lastRect = GUILayoutUtility.GetLastRect();
            GUILayout.BeginHorizontal();
            GUILayout.Space(30);
            lastRect = GUILayoutUtility.GetLastRect();
            Rect previewRect = new Rect(lastRect.xMax,
                                        lastRect.yMax,
                                        Mathf.Max(100, newGUIBorder.border.horizontal),
                                        Mathf.Max(100, newGUIBorder.border.vertical));
            exGUIBorderEditor.TexturePreviewField(previewRect, newGUIBorder, editTexture);

            GUILayout.Space(10);
            lastRect    = GUILayoutUtility.GetLastRect();
            previewRect = new Rect(lastRect.x,
                                   lastRect.yMax,
                                   Mathf.Max(100, newGUIBorder.border.horizontal),
                                   Mathf.Max(100, newGUIBorder.border.vertical));
            exGUIBorderEditor.BorderPreviewField(previewRect, newGUIBorder, editTexture);

            if (GUILayout.Button("Select...", GUILayout.Width(60), GUILayout.Height(15)))
            {
                EditorGUIUtility.PingObject(editTexture);
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(5);
        }

        // ========================================================
        // get atlas element info from atlas database
        // ========================================================

        exAtlas editAtlas = null;
        int     editIndex = -1;

        exAtlasDB.ElementInfo elInfo = null;
        if (newGUIBorder)
        {
            elInfo = exAtlasDB.GetElementInfo(newGUIBorder.textureGUID);
        }

        if (elInfo != null)
        {
            editAtlas = exEditorHelper.LoadAssetFromGUID <exAtlas>(elInfo.guidAtlas);
            editIndex = elInfo.indexInAtlas;
        }
        bool useAtlas = editAtlas != null && editIndex != -1;

        // get atlas and index from textureGUID
        if (!EditorApplication.isPlaying)
        {
            // if we use atlas, check if the atlas,index changes
            if (useAtlas)
            {
                if (editAtlas != editSpriteBorder.atlas ||
                    editIndex != editSpriteBorder.index)
                {
                    borderChanged = true;
                }
            }
            // if we don't use atlas and current edit target use atlas, clear it.
            else
            {
                if (editSpriteBorder.useAtlas)
                {
                    borderChanged = true;
                }
            }

            // check if we are first time assignment
            if (useAtlas || editTexture != null)
            {
                if (isPrefab == false && editSpriteBorder.meshFilter.sharedMesh == null)
                {
                    needRebuild = true;
                }
            }
        }

        // set border
        if (borderChanged)
        {
            //
            if (newGUIBorder == null)
            {
                editSpriteBorder.Clear();
            }
            else
            {
                editSpriteBorder.SetBorder(newGUIBorder, editAtlas, editIndex);
                if (editSpriteBorder.useAtlas == false)
                {
                    editSpriteBorder.GetComponent <Renderer>().sharedMaterial = exEditorHelper.GetDefaultMaterial(editTexture, editTexture.name);
                    editSpriteBorder.updateFlags |= exPlane.UpdateFlags.UV;
                }
            }

            GUI.changed = true;
        }

        // ========================================================
        // color
        // ========================================================

        editSpriteBorder.color = EditorGUILayout.ColorField("Color", editSpriteBorder.color);

        // ========================================================
        // atlas & index
        // ========================================================

        GUILayout.BeginHorizontal();
        GUI.enabled = false;
        EditorGUILayout.ObjectField("Atlas"
                                    , editSpriteBorder.atlas
                                    , typeof(exAtlas)
                                    , false
                                    );
        GUI.enabled = true;

        GUI.enabled = !inAnimMode;
        if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
        {
            exAtlasEditor editor = exAtlasEditor.NewWindow();
            editor.Edit(editSpriteBorder.atlas);
        }
        GUI.enabled = true;
        GUILayout.EndHorizontal();

        GUI.enabled = false;
        EditorGUILayout.IntField("Index", editSpriteBorder.index);
        GUI.enabled = true;

        // ========================================================
        // width & height
        // ========================================================

        GUI.enabled = !inAnimMode;
        // width
        float newWidth = EditorGUILayout.FloatField("Width", editSpriteBorder.width);

        if (newWidth != editSpriteBorder.width)
        {
            if (newWidth < 1.0f)
            {
                newWidth = 1.0f;
            }
            editSpriteBorder.width = newWidth;
        }

        // height
        float newHeight = EditorGUILayout.FloatField("Height", editSpriteBorder.height);

        if (newHeight != editSpriteBorder.height)
        {
            if (newHeight < 1.0f)
            {
                newHeight = 1.0f;
            }
            editSpriteBorder.height = newHeight;
        }
        GUI.enabled = true;

        // ========================================================
        // Rebuild button
        // ========================================================

        GUI.enabled = !inAnimMode;
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Rebuild...", GUILayout.Height(20)))
        {
            needRebuild = true;
        }
        GUILayout.EndHorizontal();
        GUI.enabled = true;
        GUILayout.Space(5);

        // if dirty, build it.
        if (!EditorApplication.isPlaying && !AnimationUtility.InAnimationMode())
        {
            if (needRebuild)
            {
                EditorUtility.ClearProgressBar();
                editSpriteBorder.Build(editTexture);
            }
            else if (GUI.changed)
            {
                if (editSpriteBorder.meshFilter.sharedMesh != null)
                {
                    editSpriteBorder.UpdateMesh(editSpriteBorder.meshFilter.sharedMesh);
                }
                EditorUtility.SetDirty(editSpriteBorder);
            }
        }
    }
Exemplo n.º 2
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public static void Rebuild(this exSpriteBorder _spriteBorder)
    {
        Texture2D texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(_spriteBorder.guiBorder.textureGUID);

        _spriteBorder.Build(texture);
    }
Exemplo n.º 3
0
    // ------------------------------------------------------------------
    /// \param _guiBorderList the list of gui-borders
    /// update sprite borders in current scene and in prefab by gui-border list
    // ------------------------------------------------------------------

    public static void UpdateSpriteBorders(List <exGUIBorder> _guiBorderList)
    {
        if (_guiBorderList.Count == 0)
        {
            return;
        }

        try {
            EditorUtility.DisplayProgressBar("Update Scene Sprites With Changed GUIBorders...", "Scanning...", 0.0f);
            // exSpriteBase[] sprites = GameObject.FindObjectsOfType(typeof(exSpriteBase)) as exSpriteBase[];
            exSpriteBase[] sprites = Resources.FindObjectsOfTypeAll(typeof(exSpriteBase)) as exSpriteBase[];
            for (int i = 0; i < sprites.Length; ++i)
            {
                exSpriteBase spBase = sprites[i];

#if !(EX2D_EVALUATE)
                // ========================================================
                // exSpriteBorder
                // ========================================================

                if (spBase is exSpriteBorder)
                {
                    exSpriteBorder spBorder    = spBase as exSpriteBorder;
                    bool           needRebuild = false;

                    // find if the spBorder's atalsInfo need rebuild
                    foreach (exGUIBorder guiBorder in _guiBorderList)
                    {
                        if (spBorder.guiBorder == guiBorder)
                        {
                            needRebuild = true;
                            break;
                        }
                    }

                    //
                    if (needRebuild)
                    {
                        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID);
                        exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo);
#if UNITY_3_4
                        bool isPrefab = (EditorUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#else
                        bool isPrefab = (PrefabUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#endif
                        if (isPrefab == false)
                        {
                            Texture2D texture = null;
                            if (spBorder.useAtlas == false)
                            {
                                texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID);
                            }
                            spBorder.Build(texture);
                        }
                        EditorUtility.SetDirty(spBorder);
                    }
                }
#endif // !(EX2D_EVALUATE)
            }
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }
    }
Exemplo n.º 4
0
    // ------------------------------------------------------------------
    /// \param _sprites the list of sprites to rebuild
    /// rebuild the listed sprites
    // ------------------------------------------------------------------

    public static void RebuildSprites(exSpriteBase[] _sprites)
    {
        try {
            EditorUtility.DisplayProgressBar("Rebuild Scene Sprites...",
                                             "Rebuild Scene Sprites...",
                                             0.5f);

            for (int i = 0; i < _sprites.Length; ++i)
            {
                exSpriteBase spBase = _sprites[i];
                // DISABLE: it is too slow {
                // float progress = (float)i/(float)_sprites.Length;
                // EditorUtility.DisplayProgressBar( "Rebuild Scene Sprites...",
                //                                   "Build Sprite " + spBase.gameObject.name, progress );
                // } DISABLE end

                // if sprite
                if (spBase is exSprite)
                {
                    exSprite sp = spBase as exSprite;
                    exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(sp.textureGUID);
                    exSpriteEditor.UpdateAtlas(sp, elInfo);

                    Texture2D texture = null;
                    if (sp.useAtlas == false)
                    {
                        texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(sp.textureGUID);
                    }
                    sp.Build(texture);
                }

#if !(EX2D_EVALUATE)
                // if sprite font
                if (spBase is exSpriteFont)
                {
                    exSpriteFont spFont = spBase as exSpriteFont;
                    spFont.Build();
                }

                // if sprite border
                if (spBase is exSpriteBorder)
                {
                    exSpriteBorder spBorder = spBase as exSpriteBorder;

                    Texture2D texture = null;
                    if (spBorder.guiBorder)
                    {
                        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID);
                        exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo);

                        if (spBorder.useAtlas == false)
                        {
                            texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID);
                        }
                    }
                    spBorder.Build(texture);
                }
#endif // EX2D_EVALUATE
            }
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }
    }
Exemplo n.º 5
0
    // ------------------------------------------------------------------
    /// \param _atlasInfoGUIDs the list of atlas info guid
    /// update sprites in current scene and in prefab by atlas info list
    // ------------------------------------------------------------------

    public static void UpdateSprites(List <string> _atlasInfoGUIDs)
    {
        if (_atlasInfoGUIDs.Count == 0)
        {
            return;
        }

        try {
            EditorUtility.DisplayProgressBar("Update Scene Sprites With Changed Atlas...", "Scanning...", 0.0f);
            // exSpriteBase[] sprites = GameObject.FindObjectsOfType(typeof(exSpriteBase)) as exSpriteBase[];
            exSpriteBase[] sprites = Resources.FindObjectsOfTypeAll(typeof(exSpriteBase)) as exSpriteBase[];
            for (int i = 0; i < sprites.Length; ++i)
            {
                exSpriteBase spBase = sprites[i];

                // ========================================================
                // exSprite
                // ========================================================

                if (spBase is exSprite)
                {
                    exSprite sp = spBase as exSprite;
                    exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(sp.textureGUID);
                    bool needRebuild             = false;

                    // NOTE: we test sp.index is -1 or not instead of test atlas is null, because it is possible we delete an atlas and it will always be null
                    if (elInfo != null)
                    {
                        if (sp.index == -1)
                        {
                            needRebuild = true;
                        }
                        else
                        {
                            // find if the sp's atalsInfo need rebuild
                            foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                            {
                                if (elInfo.guidAtlasInfo == guidAtlasInfo)
                                {
                                    needRebuild = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (sp.index != -1)
                        {
                            needRebuild = true;
                        }
                    }

                    //
                    if (needRebuild)
                    {
                        exSpriteEditor.UpdateAtlas(sp, elInfo);
#if UNITY_3_4
                        bool isPrefab = (EditorUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#else
                        bool isPrefab = (PrefabUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#endif
                        if (isPrefab == false)
                        {
                            Texture2D texture = null;
                            if (sp.index == -1)
                            {
                                texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(sp.textureGUID);
                            }
                            sp.Build(texture);
                        }
                        EditorUtility.SetDirty(sp);
                    }
                }

#if !(EX2D_EVALUATE)
                // ========================================================
                // exSpriteFont
                // ========================================================

                if (spBase is exSpriteFont)
                {
                    exSpriteFont spFont = spBase as exSpriteFont;

                    //
                    bool needRebuild = false;
                    if (spFont.fontInfo == null)
                    {
                        needRebuild = true;
                    }
                    else
                    {
                        foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                        {
                            exAtlasInfo atlasInfo = exEditorHelper.LoadAssetFromGUID <exAtlasInfo>(guidAtlasInfo);
                            // NOTE: it is possible we process this in delete stage
                            if (atlasInfo == null)
                            {
                                continue;
                            }

                            foreach (exBitmapFont bmfont in atlasInfo.bitmapFonts)
                            {
                                if (spFont.fontInfo == bmfont)
                                {
                                    needRebuild = true;
                                    break;
                                }
                            }
                        }
                    }

                    //
                    if (needRebuild)
                    {
                        spFont.Build();
                    }
                }

                // ========================================================
                // exSpriteBorder
                // ========================================================

                if (spBase is exSpriteBorder)
                {
                    exSpriteBorder spBorder = spBase as exSpriteBorder;
                    if (spBorder.guiBorder != null)
                    {
                        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID);
                        bool needRebuild             = false;

                        // NOTE: we test spBorder.index is -1 or not instead of test atlas is null, because it is possible we delete an atlas and it will always be null
                        if (elInfo != null)
                        {
                            if (spBorder.index == -1)
                            {
                                needRebuild = true;
                            }
                            else
                            {
                                // find if the spBorder's atalsInfo need rebuild
                                foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                                {
                                    if (elInfo.guidAtlasInfo == guidAtlasInfo)
                                    {
                                        needRebuild = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (spBorder.index != -1)
                            {
                                needRebuild = true;
                            }
                        }

                        //
                        if (needRebuild)
                        {
                            exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo);
#if UNITY_3_4
                            bool isPrefab = (EditorUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#else
                            bool isPrefab = (PrefabUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#endif
                            if (isPrefab == false)
                            {
                                Texture2D texture = null;
                                if (spBorder.index == -1)
                                {
                                    texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID);
                                }
                                spBorder.Build(texture);
                            }
                            EditorUtility.SetDirty(spBorder);
                        }
                    }
                }
#endif // !(EX2D_EVALUATE)

                // DISABLE: it is too slow {
                // float progress = (float)i/(float)sprites.Length;
                // EditorUtility.DisplayProgressBar( "Update Scene Sprites...",
                //                                   "Update Sprite " + spBase.gameObject.name, progress );
                // } DISABLE end
            }
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }
    }