UGUI Image扩展组件

柔情痞子 提交于 2019-12-03 14:26:07

功能:

优化对称图片及九宫格图片内存(对称图片只用一半图片,九宫格只用左下部分四分之一图片)。

核心代码:

1、九宫格翻转代码:

/// <summary>
/// 赋值九宫vert顶点数据
/// </summary>
/// <param name="toFill"></param>
/// <param name="rect"></param>
/// <param name="padding"></param>
/// <param name="adjustedBorders"></param>
/// <param name="dir">0-3代表4个方向,分别为0:左下部分;1:右下部分;2:右上部分;3:左上部分</param>
private void Generate9SlicedSprite(VertexHelper toFill, ref Rect rect, ref Vector4 padding, ref Vector4 adjustedBorders, int dir)
{
    s_VertScratch[3] = new Vector2((rect.width - padding.z - padding.x) / 2, (rect.height - padding.w - padding.y) / 2);
    s_VertScratch[2] = s_VertScratch[3];
    switch (dir)
    {
        case 0://左下部分
            s_VertScratch[0] = new Vector2(padding.x, padding.y);
            s_VertScratch[1] = new Vector2(adjustedBorders.x, adjustedBorders.y);
            break;
        case 1://右下部分
            s_VertScratch[0] = new Vector2(rect.width - padding.z, padding.y);
            s_VertScratch[1] = new Vector2(rect.width - adjustedBorders.x, adjustedBorders.y);
            break;
        case 2://右上部分
            s_VertScratch[0] = new Vector2(rect.width - padding.z, rect.height - padding.w);
            s_VertScratch[1] = new Vector2(rect.width - adjustedBorders.x, rect.height - adjustedBorders.y);
            break;
        case 3://左上部分
            s_VertScratch[0] = new Vector2(padding.x, rect.height - padding.w);
            s_VertScratch[1] = new Vector2(adjustedBorders.x, rect.height - adjustedBorders.y);
            break;
    }

    for (int i = 0; i < 4; ++i)
    {
        s_VertScratch[i].x += rect.x;
        s_VertScratch[i].y += rect.y;
    }


    for (int x = 0; x < 3; ++x)
    {
        int x2 = x + 1;

        for (int y = 0; y < 3; ++y)
        {
            if (!fillCenter && x == 1 && y == 1)
                continue;

            int y2 = y + 1;


            AddQuad(toFill,
                new Vector2(s_VertScratch[x].x, s_VertScratch[y].y),
                new Vector2(s_VertScratch[x2].x, s_VertScratch[y2].y),
                color,
                new Vector2(s_UVScratch[x].x, s_UVScratch[y].y),
                new Vector2(s_UVScratch[x2].x, s_UVScratch[y2].y));
        }
    }
}

对九宫格图片顶点数据进行进行重新计算赋值,将九宫格uv对应到相应的顶点上。

2、对称图片翻转代码

/// <summary>
/// Generate vertices for a simple Image.
/// </summary>
void GenerateSimpleSprite(VertexHelper vh, bool lPreserveAspect, bool bHorizontal)
{
    Vector4 v = GetDrawingDimensions(lPreserveAspect);
    var uv = (overrideSprite != null) ? Sprites.DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;

    var color32 = color;
    vh.Clear();
    if (bHorizontal)//水平翻转
    {
        vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(uv.x, uv.y));
        vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(uv.x, uv.w));
        vh.AddVert(new Vector3((v.z + v.x) / 2, v.w), color32, new Vector2(uv.z, uv.w));
        vh.AddVert(new Vector3((v.z + v.x) / 2, v.y), color32, new Vector2(uv.z, uv.y));
        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(2, 3, 0);
        vh.AddVert(new Vector3((v.z + v.x) / 2, v.y), color32, new Vector2(uv.z, uv.y));
        vh.AddVert(new Vector3((v.z + v.x) / 2, v.w), color32, new Vector2(uv.z, uv.w));
        vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(uv.x, uv.w));
        vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(uv.x, uv.y));
        vh.AddTriangle(4, 5, 6);
        vh.AddTriangle(6, 7, 4);
    }
    else//竖直翻转
    {
        vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(uv.x, uv.y));
        vh.AddVert(new Vector3(v.x, (v.w + v.y) / 2), color32, new Vector2(uv.x, uv.w));
        vh.AddVert(new Vector3(v.z, (v.w + v.y) / 2), color32, new Vector2(uv.z, uv.w));
        vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(uv.z, uv.y));
        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(2, 3, 0);
        vh.AddVert(new Vector3(v.x, (v.w + v.y) / 2), color32, new Vector2(uv.x, uv.w));
        vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(uv.x, uv.y));
        vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(uv.z, uv.y));
        vh.AddVert(new Vector3(v.z, (v.w + v.y) / 2), color32, new Vector2(uv.z, uv.w));
        vh.AddTriangle(4, 5, 6);
        vh.AddTriangle(6, 7, 4);
    }
}

重新计算对称图片进行verts,uv,实现对称图片翻转。

优缺点:

优点:

节省图片内存

缺点:

增加Tris和verts数据,一张运用ImageEx组件的九宫格的图片比正常九宫格图片多出54个Tris,108个verts,相当于多出三个九宫格图片(对称图片只多出2个Tris,4个verts,可以忽略不计)。增加面数和顶点,目前还未想到好的解决办法,但是UI界面九宫图一般都只有个位数,也就是最多增加三四百个Tris,对于几万个Tris数量级的游戏来说基本上可以忽略不计。

源码链接:

https://download.csdn.net/download/tracyzly/10325199

Unity版本:

2017.2.0p4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!