Example #1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void AddSelected(exAtlasInfo.Element _el)
    {
        if (selectedElements.IndexOf(_el) == -1)
        {
            selectedElements.Add(_el);
        }
    }
Example #2
0
    // ------------------------------------------------------------------
    /// \param _tex the raw texture you want to add
    /// \param _trim if trim the texture
    /// \return the new element
    /// add the element by raw texture
    // ------------------------------------------------------------------

    public Element AddElement(Texture2D _tex, bool _trim)
    {
        if (exTextureHelper.IsValidForAtlas(_tex) == false)
        {
            exTextureHelper.ImportTextureForAtlas(_tex);
        }

        //
        exAtlasInfo.Element el = new exAtlasInfo.Element();
        if (_trim)
        {
            el.trimRect = exTextureHelper.GetTrimTextureRect(_tex);
        }
        else
        {
            el.trimRect = new Rect(0, 0, _tex.width, _tex.height);
        }

        el.rotated   = false;
        el.trim      = _trim;
        el.atlasInfo = this;
        el.texture   = _tex;
        el.coord[0]  = 0;
        el.coord[1]  = 0;
        elements.Add(el);

        // get sprite animation clip by textureGUID, add them to rebuildAnimClipGUIDs
        AddSpriteAnimClipForRebuilding(el);

        //
        needRebuild = true;
        EditorUtility.SetDirty(this);

        return(el);
    }
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void CalculatePreviewScale()
    {
        // get the max width and max height
        float maxWidth  = -1.0f;
        float maxHeight = -1.0f;

        foreach (exSpriteAnimClip.FrameInfo frameInfo in curEdit.frameInfos)
        {
            float fiWidth  = 0.0f;
            float fiHeight = 0.0f;

            //
            exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(frameInfo.textureGUID);
            if (elInfo != null)
            {
                exAtlasInfo         atlasInfo = exEditorHelper.LoadAssetFromGUID <exAtlasInfo>(elInfo.guidAtlasInfo);
                exAtlasInfo.Element el        = atlasInfo.elements[elInfo.indexInAtlasInfo];
                // fiWidth = el.trimRect.width;
                // fiHeight = el.trimRect.height;
                fiWidth  = el.texture.width;
                fiHeight = el.texture.height;
            }
            else
            {
                string    texturePath = AssetDatabase.GUIDToAssetPath(frameInfo.textureGUID);
                Texture2D tex2D       = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
                fiWidth  = tex2D.width;
                fiHeight = tex2D.height;
            }

            //
            if (maxWidth <= fiWidth)
            {
                maxWidth = fiWidth;
            }
            if (maxHeight <= fiHeight)
            {
                maxHeight = fiHeight;
            }
        }

        // get the preview scale
        previewScale = 1.0f;
        float viewWidth  = curEdit.editorPreviewSize;
        float viewHeight = curEdit.editorPreviewSize;

        if (maxWidth > viewWidth && maxHeight > viewHeight)
        {
            previewScale = Mathf.Min(viewWidth / maxWidth, viewHeight / maxHeight);
        }
        else if (maxWidth > viewWidth)
        {
            previewScale = viewWidth / maxWidth;
        }
        else if (maxHeight > viewHeight)
        {
            previewScale = viewHeight / maxHeight;
        }
    }
Example #4
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void ToggleSelected(exAtlasInfo.Element _el)
    {
        int i = selectedElements.IndexOf(_el);

        if (i != -1)
        {
            selectedElements.RemoveAt(i);
        }
        else
        {
            selectedElements.Add(_el);
        }
    }
Example #5
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    static ElementInfo AddElementInfo(exAtlasInfo.Element _el, int _index)
    {
        Init();

        if (_el.isFontElement)
        {
            return(null);
        }

        string textureGUID = exEditorHelper.AssetToGUID(_el.texture);

        return(AddElementInfo(textureGUID,
                              exEditorHelper.AssetToGUID(_el.atlasInfo.atlas),
                              exEditorHelper.AssetToGUID(_el.atlasInfo),
                              _index));
    }
Example #6
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    protected Element AddFontElement(exBitmapFont _srcFontInfo, exBitmapFont _destFontInfo, exBitmapFont.CharInfo _charInfo)
    {
        exAtlasInfo.Element el = new exAtlasInfo.Element();
        el.isFontElement = true;

        el.srcFontInfo  = _srcFontInfo;
        el.destFontInfo = _destFontInfo;
        el.charInfo     = _charInfo;

        el.trimRect  = new Rect(_charInfo.x, _charInfo.y, _charInfo.width, _charInfo.height);
        el.rotated   = false;
        el.trim      = true;
        el.atlasInfo = this;
        el.texture   = _srcFontInfo.pageInfos[0].texture;
        el.coord[0]  = 0;
        el.coord[1]  = 0;

        exBitmapFont.CharInfo destCharInfo = el.destFontInfo.GetCharInfo(el.charInfo.id);
        if (destCharInfo != null)
        {
            destCharInfo.id       = el.charInfo.id;
            destCharInfo.x        = el.charInfo.x;
            destCharInfo.y        = el.charInfo.y;
            destCharInfo.width    = el.charInfo.width;
            destCharInfo.height   = el.charInfo.height;
            destCharInfo.xoffset  = el.charInfo.xoffset;
            destCharInfo.yoffset  = el.charInfo.yoffset;
            destCharInfo.xadvance = el.charInfo.xadvance;
            destCharInfo.page     = el.charInfo.page;
            destCharInfo.uv0      = el.charInfo.uv0;
        }
        else
        {
            Debug.LogError("can't not find char info with ID " + el.charInfo.id);
        }

        elements.Add(el);

        needRebuild = true;
        EditorUtility.SetDirty(this);

        return(el);
    }
Example #7
0
    // ------------------------------------------------------------------
    /// \param _fontInfo the font info you want to remove
    /// Find and remove the font info from the atlas
    // ------------------------------------------------------------------

    public void RemoveBitmapFont(exBitmapFont _fontInfo)
    {
        for (int i = 0; i < elements.Count; ++i)
        {
            exAtlasInfo.Element el = elements[i];
            if (el.isFontElement == false)
            {
                continue;
            }

            if (el.destFontInfo == _fontInfo)
            {
                RemoveElement(el);
                --i;
            }
        }
        bitmapFonts.Remove(_fontInfo);
        EditorUtility.SetDirty(this);
    }
Example #8
0
    ///////////////////////////////////////////////////////////////////////////////
    // functions
    ///////////////////////////////////////////////////////////////////////////////

    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public void UpdateElement(Texture2D _tex, bool _trim, bool _noImport = true)
    {
        //
        if (exTextureHelper.IsValidForAtlas(_tex) == false)
        {
            if (_noImport)
            {
                Debug.LogError("Invalid texture settings for atlas, texture name " + _tex.name);
                return;
            }
            exTextureHelper.ImportTextureForAtlas(_tex);
        }

        //
        exAtlasInfo.Element el = null;
        for (int i = 0; i < elements.Count; ++i)
        {
            el = elements[i];
            if (el.texture == _tex)
            {
                break;
            }
        }

        //
        if (el == null)
        {
            Debug.LogError("can't find element by texture " + _tex.name);
            return;
        }

        //
        if (_trim)
        {
            el.trimRect = exTextureHelper.GetTrimTextureRect(_tex);
        }
        else
        {
            el.trimRect = new Rect(0, 0, _tex.width, _tex.height);
        }
        el.trim = _trim;
    }
Example #9
0
    // ------------------------------------------------------------------
    /// \param _el the element in atlas info
    /// \param _index the index of the element
    /// update the index of the element info in atlas db
    // ------------------------------------------------------------------

    public static void UpdateElementInfo(exAtlasInfo.Element _el, int _index)
    {
        Init();

        string      textureGUID = exEditorHelper.AssetToGUID(_el.texture);
        ElementInfo elInfo      = GetElementInfo(textureGUID);

        if (elInfo == null)
        {
            elInfo = AddElementInfo(_el, _index);
            if (elInfo != null)
            {
                db.elementInfos.Add(elInfo);
                EditorUtility.SetDirty(db);
            }
        }
        else
        {
            elInfo.indexInAtlas     = _index;
            elInfo.indexInAtlasInfo = _index;
            EditorUtility.SetDirty(db);
        }
    }
Example #10
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void AtlasElementField(Rect _atlasRect, exAtlasInfo _atlasInfo, exAtlasInfo.Element _el)
    {
        Color oldBGColor = GUI.backgroundColor;
        Rect  srcRect;
        Rect  rect = new Rect(_el.coord[0] * _atlasInfo.scale,
                              _el.coord[1] * _atlasInfo.scale,
                              _el.Width() * _atlasInfo.scale,
                              _el.Height() * _atlasInfo.scale);
        bool selected = selectedElements.IndexOf(_el) != -1;

        // ========================================================
        // draw the sprite background
        // ========================================================

        GUI.BeginGroup(_atlasRect);
        GUI.color = _el.atlasInfo.elementBgColor;
        GUI.DrawTexture(rect, exEditorHelper.WhiteTexture());
        GUI.color = oldBGColor;
        GUI.EndGroup();


        // ========================================================
        // draw the texture
        // ========================================================

        // process rotate
        Matrix4x4 oldMat = GUI.matrix;

        if (_el.rotated)
        {
            GUIUtility.RotateAroundPivot(90.0f, new Vector2(_atlasRect.x, _atlasRect.y));
            GUI.matrix = GUI.matrix * Matrix4x4.TRS(new Vector3(0.0f, -_atlasRect.width, 0.0f), Quaternion.identity, Vector3.one);

            // NOTE: clipping is done before rotating, if we rotate, we have to change the clip
            GUI.BeginGroup(_atlasRect);
            srcRect = new Rect(_el.coord[1],
                               _atlasRect.width - _el.coord[0] - _el.trimRect.height,
                               _el.trimRect.width,
                               _el.trimRect.height);
        }
        else
        {
            GUI.BeginGroup(_atlasRect);
            srcRect = new Rect(_el.coord[0],
                               _el.coord[1],
                               _el.trimRect.width,
                               _el.trimRect.height);
        }
        srcRect = new Rect(srcRect.x * _atlasInfo.scale,
                           srcRect.y * _atlasInfo.scale,
                           srcRect.width * _atlasInfo.scale,
                           srcRect.height * _atlasInfo.scale);

        // draw texture
        if (_el.trim)
        {
            Rect rect2 = new Rect(-_el.trimRect.x * _atlasInfo.scale,
                                  -_el.trimRect.y * _atlasInfo.scale,
                                  _el.texture.width * _atlasInfo.scale,
                                  _el.texture.height * _atlasInfo.scale);
            GUI.BeginGroup(srcRect);
            GUI.DrawTexture(rect2, _el.texture);
            GUI.EndGroup();
        }
        else
        {
            GUI.DrawTexture(srcRect, _el.texture);
        }

        // recover from rotate
        if (_el.rotated)
        {
            GUI.matrix = oldMat;
        }
        GUI.EndGroup();

        // ========================================================
        // draw selected border
        // ========================================================

        GUI.BeginGroup(_atlasRect);
        if (selected)
        {
            GUI.backgroundColor = _el.atlasInfo.elementSelectColor;
            GUI.Box(rect, GUIContent.none, exEditorHelper.RectBorderStyle());
            GUI.backgroundColor = oldBGColor;
        }
        GUI.EndGroup();

        // ========================================================
        // process mouse event
        Event e = Event.current;

        // ========================================================

        GUI.BeginGroup(_atlasRect);
        if (e.type == EventType.MouseDown && e.button == 0 && e.clickCount == 1)
        {
            if (rect.Contains(e.mousePosition))
            {
                GUIUtility.keyboardControl = -1; // remove any keyboard control
                if (e.command || e.control)
                {
                    ToggleSelected(_el);
                }
                else
                {
                    inDraggingElementState = true;
                    if (selected == false)
                    {
                        if (e.command == false && e.control == false)
                        {
                            selectedElements.Clear();
                            AddSelected(_el);
                        }
                    }
                }

                e.Use();
                Repaint();
            }
        }
        GUI.EndGroup();
    }
Example #11
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void PreviewField(Rect _rect)
    {
        // ========================================================
        // preview
        // ========================================================

        int       borderSize      = 1;
        int       boxWidth        = (int)_rect.width + 2 * borderSize;  // box border
        int       boxHeight       = (int)_rect.height + 2 * borderSize; // box border
        Texture2D texCheckerboard = exEditorHelper.CheckerboardTexture();

        GUI.BeginGroup(_rect);
        int col = Mathf.CeilToInt(_rect.width / texCheckerboard.width);
        int row = Mathf.CeilToInt(_rect.height / texCheckerboard.height);

        for (int j = 0; j < row; ++j)
        {
            for (int i = 0; i < col; ++i)
            {
                Rect size = new Rect(i * texCheckerboard.width,
                                     j * texCheckerboard.height,
                                     texCheckerboard.width,
                                     texCheckerboard.height);
                GUI.DrawTexture(size, texCheckerboard);
            }
        }
        GUI.EndGroup();

        Color oldBGColor = GUI.backgroundColor;

        GUI.backgroundColor = Color.black;
        GUI.Box(new Rect(_rect.x - borderSize, _rect.y - borderSize, boxWidth, boxHeight),
                GUIContent.none,
                exEditorHelper.RectBorderStyle());
        GUI.backgroundColor = oldBGColor;

        // ========================================================
        // draw frame
        // ========================================================

        if (curEdit != null)
        {
            // draw the preview
            exSpriteAnimClip.FrameInfo fi = curEdit.GetFrameInfoBySeconds(curSeconds, curEdit.wrapMode);
            if (fi != null)
            {
                exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(fi.textureGUID);

                if (elInfo != null)
                {
                    exAtlasInfo         atlasInfo = exEditorHelper.LoadAssetFromGUID <exAtlasInfo>(elInfo.guidAtlasInfo);
                    exAtlasInfo.Element el        = atlasInfo.elements[elInfo.indexInAtlasInfo];

                    if (el.texture != null)
                    {
                        float width   = el.texture.width;
                        float height  = el.texture.height;
                        float offsetX = (width - el.trimRect.width) * 0.5f - el.trimRect.x;
                        float offsetY = (height - el.trimRect.height) * 0.5f - el.trimRect.y;

                        // DELME {
                        // Rect frameRect = new Rect( -el.trimRect.x * previewScale,
                        //                            -el.trimRect.y * previewScale,
                        //                            width * previewScale,
                        //                            height * previewScale );
                        // } DELME end
                        Rect rect2 = new Rect((_rect.width - el.trimRect.width * previewScale) * 0.5f - offsetX * previewScale,
                                              (_rect.height - el.trimRect.height * previewScale) * 0.5f - offsetY * previewScale,
                                              el.trimRect.width * previewScale,
                                              el.trimRect.height * previewScale);


                        // DISABLE: because we can't make sure all texture have same size, so ScaleToFit may play large texture {
                        // GUI.BeginGroup( _rect );
                        //     GUI.DrawTexture( new Rect( 0.0f, 0.0f, _rect.width, _rect.height ),
                        //                      el.texture,
                        //                      ScaleMode.ScaleToFit );
                        // GUI.EndGroup();
                        // } DISABLE end

                        GUI.BeginGroup(_rect);
                        // DELME {
                        // draw background
                        // Color old = GUI.color;
                        // GUI.color = new Color( 1.0f, 0.0f, 0.85f, 0.2f );
                        //     GUI.DrawTexture( rect2, exEditorHelper.WhiteTexture() );
                        // GUI.color = old;

                        // // draw texture
                        // GUI.BeginGroup( rect2 );
                        //     GUI.BeginGroup( new Rect( (rect2.width - el.trimRect.width * previewScale) * 0.5f,
                        //                               (rect2.height - el.trimRect.height * previewScale) * 0.5f,
                        //                               el.trimRect.width * previewScale,
                        //                               el.trimRect.height * previewScale ) );
                        //         GUI.DrawTexture( frameRect, el.texture );
                        //     GUI.EndGroup();
                        // GUI.EndGroup();
                        // } DELME end

                        Graphics.DrawTexture(rect2,
                                             el.texture,
                                             new Rect(el.trimRect.x / el.texture.width,
                                                      (el.texture.height - el.trimRect.y - el.trimRect.height) / el.texture.height,
                                                      el.trimRect.width / el.texture.width,
                                                      el.trimRect.height / el.texture.height),
                                             0, 0, 0, 0,
                                             Color.white / 2.0f);

                        // DELME {
                        // draw border
                        // Color oldBGColor = GUI.backgroundColor;
                        // GUI.backgroundColor = Color.black;
                        //     GUI.Box ( rect2, GUIContent.none, exEditorHelper.RectBorderStyle() );
                        // GUI.backgroundColor = oldBGColor;
                        // } DELME end
                        GUI.EndGroup();
                    }
                }
                else
                {
                    string    texturePath = AssetDatabase.GUIDToAssetPath(fi.textureGUID);
                    Texture2D tex2D       = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
                    if (tex2D != null)
                    {
                        Rect size = new Rect(0.0f,
                                             0.0f,
                                             tex2D.width * previewScale,
                                             tex2D.height * previewScale);
                        Rect rect2 = new Rect((_rect.width - size.width) * 0.5f,
                                              (_rect.height - size.height) * 0.5f,
                                              size.width,
                                              size.height);

                        GUI.BeginGroup(_rect);
                        GUI.BeginGroup(rect2);
                        GUI.DrawTexture(size, tex2D);
                        GUI.EndGroup();
                        GUI.EndGroup();
                    }
                }
            }
        }

        GUILayoutUtility.GetRect(_rect.width, _rect.height);
    }
Example #12
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------
    protected Element AddFontElement( exBitmapFont _srcFontInfo, exBitmapFont _destFontInfo, exBitmapFont.CharInfo _charInfo )
    {
        exAtlasInfo.Element el = new exAtlasInfo.Element();
        el.isFontElement = true;

        el.srcFontInfo = _srcFontInfo;
        el.destFontInfo = _destFontInfo;
        el.charInfo = _charInfo;

        el.trimRect = new Rect( _charInfo.x, _charInfo.y, _charInfo.width, _charInfo.height );
        el.rotated = false;
        el.trim = true;
        el.atlasInfo = this;
        el.texture = _srcFontInfo.pageInfos[0].texture;
        el.coord[0] = 0;
        el.coord[1] = 0;

        exBitmapFont.CharInfo destCharInfo = el.destFontInfo.GetCharInfo(el.charInfo.id);
        if ( destCharInfo != null ) {
            destCharInfo.id = el.charInfo.id;
            destCharInfo.x = el.charInfo.x;
            destCharInfo.y = el.charInfo.y;
            destCharInfo.width = el.charInfo.width;
            destCharInfo.height = el.charInfo.height;
            destCharInfo.xoffset = el.charInfo.xoffset;
            destCharInfo.yoffset = el.charInfo.yoffset;
            destCharInfo.xadvance = el.charInfo.xadvance;
            destCharInfo.page = el.charInfo.page;
            destCharInfo.uv0 = el.charInfo.uv0;
        }
        else {
            Debug.LogError ( "can't not find char info with ID " + el.charInfo.id );
        }

        elements.Add(el);

        needRebuild = true;
        EditorUtility.SetDirty(this);

        return el;
    }
Example #13
0
    // ------------------------------------------------------------------
    /// \param _tex the raw texture you want to add
    /// \param _trim if trim the texture
    /// \return the new element
    /// add the element by raw texture 
    // ------------------------------------------------------------------
    public Element AddElement( Texture2D _tex, bool _trim )
    {
        if ( exTextureHelper.IsValidForAtlas (_tex) == false )
            exTextureHelper.ImportTextureForAtlas(_tex);

        //
        exAtlasInfo.Element el = new exAtlasInfo.Element();
        if ( _trim ) {
            el.trimRect = exTextureHelper.GetTrimTextureRect(_tex);
        }
        else {
            el.trimRect = new Rect( 0, 0, _tex.width, _tex.height );
        }

        el.rotated = false;
        el.trim = _trim;
        el.atlasInfo = this;
        el.texture = _tex;
        el.coord[0] = 0;
        el.coord[1] = 0;
        elements.Add(el);

        // get sprite animation clip by textureGUID, add them to rebuildAnimClipGUIDs
        AddSpriteAnimClipForRebuilding(el);

        //
        needRebuild = true;
        EditorUtility.SetDirty(this);

        return el;
    }
Example #14
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    void FrameInfoField(Rect _rect, exSpriteAnimClip.FrameInfo _fi)
    {
        bool selected = selectedFrameInfos.IndexOf(_fi) != -1;

        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(_fi.textureGUID);

        // ========================================================
        // draw background
        // ========================================================

        Color old = GUI.color;

        if (selected)
        {
            GUI.color = new Color(0.2f, 0.85f, 0.0f, 0.2f);
        }
        else if (elInfo == null)
        {
            GUI.color = new Color(1.0f, 0.0f, 0.0f, 0.2f);
        }
        else
        {
            GUI.color = new Color(1.0f, 0.0f, 0.85f, 0.2f);
        }
        GUI.DrawTexture(_rect, exEditorHelper.WhiteTexture());
        GUI.color = old;

        // ========================================================
        // draw texture
        // ========================================================

        if (elInfo != null)
        {
            exAtlasInfo         atlasInfo = exEditorHelper.LoadAssetFromGUID <exAtlasInfo>(elInfo.guidAtlasInfo);
            exAtlasInfo.Element el        = atlasInfo.elements[elInfo.indexInAtlasInfo];

            if (el.texture != null)
            {
                float width  = el.texture.width;
                float height = el.texture.height;

                // get the scale
                float scale = 1.0f;
                if (width > _rect.width && height > _rect.height)
                {
                    scale = Mathf.Min(_rect.width / width,
                                      _rect.height / height);
                }
                else if (width > _rect.width)
                {
                    scale = _rect.width / width;
                }
                else if (height > _rect.height)
                {
                    scale = _rect.height / height;
                }

                // draw
                Rect size = new Rect(-el.trimRect.x * scale,
                                     -el.trimRect.y * scale,
                                     width * scale,
                                     height * scale);
                GUI.BeginGroup(_rect);
                GUI.BeginGroup(new Rect((_rect.width - el.trimRect.width * scale) * 0.5f,
                                        (_rect.height - el.trimRect.height * scale) * 0.5f,
                                        el.trimRect.width * scale,
                                        el.trimRect.height * scale));
                GUI.DrawTexture(size, el.texture);
                GUI.EndGroup();
                GUI.EndGroup();
            }
        }
        else
        {
            Texture2D tex2D = exEditorHelper.LoadAssetFromGUID <Texture2D>(_fi.textureGUID);
            if (tex2D != null)
            {
                float width  = tex2D.width;
                float height = tex2D.height;

                // get the scale
                float scale = 1.0f;
                if (width > _rect.width && height > _rect.height)
                {
                    scale = Mathf.Min(_rect.width / width,
                                      _rect.height / height);
                }
                else if (width > _rect.width)
                {
                    scale = _rect.width / width;
                }
                else if (height > _rect.height)
                {
                    scale = _rect.height / height;
                }

                //
                Rect size  = new Rect(0.0f, 0.0f, width * scale, height * scale);
                Rect rect2 = new Rect((_rect.width - size.width) * 0.5f,
                                      (_rect.height - size.height) * 0.5f,
                                      size.width,
                                      size.height);

                //
                GUI.BeginGroup(_rect);
                GUI.BeginGroup(rect2);
                GUI.DrawTexture(size, tex2D);
                GUI.EndGroup();
                GUI.EndGroup();
            }
        }

        // ========================================================
        // draw border
        // ========================================================

        Color oldBGColor = GUI.backgroundColor;

        GUI.backgroundColor = Color.black;
        GUI.Box(_rect, GUIContent.none, exEditorHelper.RectBorderStyle());
        GUI.backgroundColor = oldBGColor;
    }
Example #15
0
    // ------------------------------------------------------------------
    /// \param _atlasInfo the atlas info
    /// \param _noImport if true, ex2D will not import the texture to fit for atlas
    /// build the atlas info to atlas
    // ------------------------------------------------------------------

    public static void Build(exAtlasInfo _atlasInfo, bool _noImport = false)
    {
        exAtlas   atlas    = _atlasInfo.atlas;
        Texture2D texture  = _atlasInfo.texture;
        Material  material = _atlasInfo.material;

        // check if the atlas info is valid for build
        if (atlas == null)
        {
            Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the atlas is missing!");
            return;
        }
        if (texture == null)
        {
            Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the texture is missing!");
            return;
        }
        if (material == null)
        {
            Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the material is missing!");
            return;
        }

        //
        if (_atlasInfo.needLayout)
        {
            _atlasInfo.LayoutElements();
            _atlasInfo.needLayout = false;
        }

        // create temp texture
        Color32 buildColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);

        if (_atlasInfo.useBuildColor)
        {
            buildColor = new Color(_atlasInfo.buildColor.r,
                                   _atlasInfo.buildColor.g,
                                   _atlasInfo.buildColor.b,
                                   0.0f);
        }

        string path = AssetDatabase.GetAssetPath(texture);

        TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter;

        // TextureImporterSettings textureImporterSettings = new TextureImporterSettings();
        // importer.ReadTextureSettings(textureImporterSettings);
        // textureImporterSettings.readable = true;
        // importer.SetTextureSettings(textureImporterSettings);
        importer.wrapMode   = TextureWrapMode.Clamp;
        importer.isReadable = true;
        AssetDatabase.ImportAsset(path);

        Color32[] colors = new Color32[_atlasInfo.width * _atlasInfo.height];
        for (int i = 0; i < _atlasInfo.width * _atlasInfo.height; ++i)
        {
            colors[i] = buildColor;
        }
        texture.SetPixels32(colors);

        try {
            EditorUtility.DisplayProgressBar("Building Atlas " + _atlasInfo.name, "Building Atlas...", 0.1f);

            // build atlas texture
            _atlasInfo.elements.Sort(exAtlasInfo.CompareByName);
            FillAtlasTexture(texture, _atlasInfo, _noImport);
            EditorUtility.DisplayProgressBar("Building Atlas " + _atlasInfo.name,
                                             "Import Atlas",
                                             0.9f);

            // write to disk
            byte[] pngData = texture.EncodeToPNG();
            if (pngData != null)
            {
                File.WriteAllBytes(path, pngData);
            }

            // now we finish atlas texture filling, we should turn off Read/Write settings, that will save memory a lot!
            TextureImporter importSettings = TextureImporter.GetAtPath(path) as TextureImporter;
            importSettings.wrapMode   = TextureWrapMode.Clamp;
            importSettings.isReadable = _atlasInfo.readable;
            AssetDatabase.ImportAsset(path);

            //
            atlas.elements = new exAtlas.Element[_atlasInfo.elements.Count];
            for (int i = 0; i < _atlasInfo.elements.Count; ++i)
            {
                exAtlasInfo.Element el  = _atlasInfo.elements[i];
                exAtlas.Element     el2 = new exAtlas.Element();

                int   coord_x = el.coord[0];
                int   coord_y = el.atlasInfo.height - el.coord[1] - (int)el.Height();
                float xStart  = (float)coord_x / (float)el.atlasInfo.width;
                float yStart  = (float)coord_y / (float)el.atlasInfo.height;
                float xEnd    = (float)(coord_x + el.Width()) / (float)el.atlasInfo.width;
                float yEnd    = (float)(coord_y + el.Height()) / (float)el.atlasInfo.height;
                el2.name           = el.texture.name;
                el2.coords         = new Rect(xStart, yStart, xEnd - xStart, yEnd - yStart);
                el2.rotated        = el.rotated;
                el2.originalWidth  = el.texture.width;
                el2.originalHeight = el.texture.height;
                el2.trimRect       = el.trimRect;
                atlas.elements[i]  = el2;

                // update the index in exAtlasDB
                if (el.isFontElement == false)
                {
                    exAtlasDB.UpdateElementInfo(el, i);
                }
            }
            atlas.texture  = texture;
            atlas.material = material;
            EditorUtility.SetDirty(atlas);
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }

        // save the needRebuild setting
        _atlasInfo.needRebuild = false;
        EditorUtility.SetDirty(_atlasInfo);
    }