Ejemplo n.º 1
0
        public byte[] GetSpriteFromAtlas(UISpriteData es)
        {
            if (m_tet2dAtlasImage == null)
            {
                return(null);
            }
            m_iWidthMin  = Mathf.Clamp(es.x, 0, m_iAtlasImageWidth);
            m_iHeightMin = Mathf.Clamp(es.y, 0, m_iAtlasImageHeight);
            m_iWidthMax  = Mathf.Min(m_iWidthMin + es.width, m_iAtlasImageWidth - 1);
            m_iHeightMax = Mathf.Min(m_iHeightMin + es.height, m_iAtlasImageHeight - 1);
            m_iNewWidth  = Mathf.Clamp(es.width, 0, m_iAtlasImageWidth);
            m_iNewHeight = Mathf.Clamp(es.height, 0, m_iAtlasImageHeight);

            if (m_iNewWidth == 0 || m_iNewHeight == 0)
            {
                return(null);
            }
            m_c32arrNewPixels = new Color32[m_iNewWidth * m_iNewHeight];

            for (int y = 0; y < m_iNewHeight; ++y)
            {
                m_iCopyHeight = m_iHeightMin + y;
                if (m_iCopyHeight > m_iHeightMax)
                {
                    m_iCopyHeight = m_iHeightMax;
                }
                for (int x = 0; x < m_iNewWidth; ++x)
                {
                    m_iCopyWidth = m_iWidthMin + x;
                    if (m_iCopyWidth > m_iWidthMax)
                    {
                        m_iCopyWidth = m_iWidthMax;
                    }
                    int newIndex = (m_iNewHeight - 1 - y) * m_iNewWidth + x;
                    int oldIndex = (m_iAtlasImageHeight - 1 - m_iCopyHeight) * m_iAtlasImageWidth + m_iCopyWidth;

                    m_c32arrNewPixels[newIndex] = m_carrAtlasImage[oldIndex];
                }
            }

            m_spriteEntry = new UIAtlasMaker.SpriteEntry();
            m_spriteEntry.CopyFrom(es);
            m_spriteEntry.SetRect(0, 0, m_iNewWidth, m_iNewHeight);
            m_spriteEntry.temporaryTexture = true;
            m_spriteEntry.tex = new Texture2D(m_iNewWidth, m_iNewHeight);
            m_spriteEntry.tex.SetPixels32(m_c32arrNewPixels);
            m_spriteEntry.tex.Apply();
            m_c32arrNewPixels = null;
            return(m_spriteEntry.tex.EncodeToPNG());
        }
Ejemplo n.º 2
0
    /// <summary>
    /// 从图集纹理中分解特定的sprite
    /// </summary>
    SpriteEntry ExtractSprite(UISpriteData es, Color32[] oldPixels, int oldWidth, int oldHeight)
    {
        int xmin      = Mathf.Clamp(es.x, 0, oldWidth);
        int ymin      = Mathf.Clamp(es.y, 0, oldHeight);
        int xmax      = Mathf.Min(xmin + es.width, oldWidth - 1);
        int ymax      = Mathf.Min(ymin + es.height, oldHeight - 1);
        int newWidth  = Mathf.Clamp(es.width, 0, oldWidth);
        int newHeight = Mathf.Clamp(es.height, 0, oldHeight);

        if (newWidth == 0 || newHeight == 0)
        {
            return(null);
        }

        Color32[] newPixels = new Color32[newWidth * newHeight];

        for (int y = 0; y < newHeight; ++y)
        {
            int cy = ymin + y;
            if (cy > ymax)
            {
                cy = ymax;
            }

            for (int x = 0; x < newWidth; ++x)
            {
                int cx = xmin + x;
                if (cx > xmax)
                {
                    cx = xmax;
                }

                int newIndex = (newHeight - 1 - y) * newWidth + x;
                int oldIndex = (oldHeight - 1 - cy) * oldWidth + cx;

                newPixels[newIndex] = oldPixels[oldIndex];
            }
        }

        // Create a new sprite
        SpriteEntry sprite = new SpriteEntry();

        sprite.CopyFrom(es);
        sprite.SetRect(0, 0, newWidth, newHeight);
        sprite.SetTexture(newPixels, newWidth, newHeight);
        return(sprite);
    }