NGUI图片显示的优化——镜像拼接

NGUI图片显示的优化——镜像拼接

此优化主要是实现图片的镜像拼接,对称的图片只需要半张切图、中心对称的只需要四分之一张图,通过代码显示成一张完整的图,以此来减小图片资源的大小。
这篇文章 https://blog.csdn.net/huutu/article/details/45050065 (不是我写的)对这种优化做了介绍,不过里面是简单样式的实现,而且使用的NGUI版本比较旧了。
我在上面版本的基础上增加了上下拼接及各样式的九宫格拉伸,使用的版本是Unity2018、NGUI v3.12.0。

UIBasicSprite.cs
添加枚举

[DoNotObfuscateNGUI] public enum Type
{
	Simple,
	Sliced,
	Tiled,
	Filled,
	Advanced,
    Quarter,    //中心对称
    Y_Sym,      //左右对称
    X_Sym,      //上下对称
    SlicedQuarter,  //中心对称+九宫格
    Sliced_Y_Sym,   //左右对称+九宫格
    Sliced_X_Sym,   //上下对称+九宫格
}

修改显示方式调用

protected void Fill (List<Vector3> verts, List<Vector2> uvs, List<Color> cols, Rect outer, Rect inner)
{
	mOuterUV = outer;
	mInnerUV = inner;

    switch (type)
    {
        case Type.Simple:
            SimpleFill(verts, uvs, cols);
            break;

        case Type.Sliced:
            SlicedFill(verts, uvs, cols);
            break;

        case Type.Filled:
            FilledFill(verts, uvs, cols);
            break;

        case Type.Tiled:
            TiledFill(verts, uvs, cols);
            break;

        case Type.Advanced:
            AdvancedFill(verts, uvs, cols);
            break;

        case Type.Quarter:
            QuarterFill(verts, uvs, cols);
            break;

        case Type.Y_Sym:
            Y_Sym_Fill(verts, uvs, cols);
            break;

        case Type.X_Sym:
            X_Sym_Fill(verts, uvs, cols);
            break;

        case Type.SlicedQuarter:
            SlicedQuarterFill(verts, uvs, cols);
            break;

        case Type.Sliced_Y_Sym:
            Sliced_Y_Sym_Fill(verts, uvs, cols);
            break;

        case Type.Sliced_X_Sym:
            Sliced_X_Sym_Fill(verts, uvs, cols);
            break;
    }
}

添加显示函数

void QuarterFill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 v = drawingDimensions;
    Vector4 u = drawingUVs;
    Color c = drawingColor;

    float width = v.z - v.x;
    float height = v.w - v.y;

    //左上
    verts.Add(new Vector3(v.x, v.y + height / 2));
    verts.Add(new Vector3(v.x, v.w));
    verts.Add(new Vector3(v.z - width / 2, v.w));
    verts.Add(new Vector3(v.z - width / 2, v.y + height / 2));

    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.z, u.y));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);

    //左下
    verts.Add(new Vector3(v.x, v.y));
    verts.Add(new Vector3(v.x, v.w - height / 2));
    verts.Add(new Vector3(v.z - width / 2, v.w - height / 2));
    verts.Add(new Vector3(v.z - width / 2, v.y));

    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.z, u.y));
    uvs.Add(new Vector2(u.z, u.w));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);

    //右下
    verts.Add(new Vector3(v.x + width / 2, v.y));
    verts.Add(new Vector3(v.x + width / 2, v.w - height / 2));
    verts.Add(new Vector3(v.z, v.w - height / 2));
    verts.Add(new Vector3(v.z, v.y));

    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.z, u.y));
    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.x, u.w));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);

    //右上
    verts.Add(new Vector3(v.x + width / 2, v.y + height / 2));
    verts.Add(new Vector3(v.x + width / 2, v.w));
    verts.Add(new Vector3(v.z, v.w));
    verts.Add(new Vector3(v.z, v.y + height / 2));

    uvs.Add(new Vector2(u.z, u.y));
    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.x, u.y));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
}

void Y_Sym_Fill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 v = drawingDimensions;
    Vector4 u = drawingUVs;
    Color c = drawingColor;

    float width = v.z - v.x;
    //float height = v.w - v.y;

    //左
    verts.Add(new Vector3(v.x, v.y));
    verts.Add(new Vector3(v.x, v.w));
    verts.Add(new Vector3(v.z - width / 2, v.w));
    verts.Add(new Vector3(v.z - width / 2, v.y));


    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.z, u.y));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);

    //右
    verts.Add(new Vector3(v.x + width / 2, v.y));
    verts.Add(new Vector3(v.x + width / 2, v.w));
    verts.Add(new Vector3(v.z, v.w));
    verts.Add(new Vector3(v.z, v.y));

    uvs.Add(new Vector2(u.z, u.y));
    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.x, u.y));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
}

void X_Sym_Fill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 v = drawingDimensions;
    Vector4 u = drawingUVs;
    Color c = drawingColor;

    //float width = v.z - v.x;
    float height = v.w - v.y;

    //上
    verts.Add(new Vector3(v.x, v.y + height / 2));
    verts.Add(new Vector3(v.x, v.w));
    verts.Add(new Vector3(v.z, v.w));
    verts.Add(new Vector3(v.z, v.y + height / 2));


    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.z, u.w));
    uvs.Add(new Vector2(u.z, u.y));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);

    //下
    verts.Add(new Vector3(v.x, v.y));
    verts.Add(new Vector3(v.x, v.w - height / 2));
    verts.Add(new Vector3(v.z, v.w - height / 2));
    verts.Add(new Vector3(v.z, v.y));

    uvs.Add(new Vector2(u.x, u.w));
    uvs.Add(new Vector2(u.x, u.y));
    uvs.Add(new Vector2(u.z, u.y));
    uvs.Add(new Vector2(u.z, u.w));

    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
    cols.Add(c);
}

void SlicedQuarterFill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 br = border * pixelSize;

    if (br.x == 0f && br.y == 0f && br.z == 0f && br.w == 0f)
    {
        QuarterFill(verts, uvs, cols);
        return;
    }

    Color c = drawingColor;
    Vector4 v = drawingDimensions;

    float width = v.z - v.x;
    float height = v.w - v.y;

    //左上
    mTempPos[0].x = v.x;
    mTempPos[0].y = v.y + height / 2;
    mTempPos[3].x = v.z - width / 2;
    mTempPos[3].y = v.w;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y].y));
            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols, ref c, x, y);
                AddVertexColours(cols, ref c, x, y2);
                AddVertexColours(cols, ref c, x2, y2);
                AddVertexColours(cols, ref c, x2, y);
            }
        }
    }

    //左下
    mTempPos[0].x = v.x;
    mTempPos[0].y = v.y;
    mTempPos[3].x = v.z - width / 2;
    mTempPos[3].y = v.w - height / 2;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[3 - y].y));
            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[3 - y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }

    //右上
    mTempPos[0].x = v.x + width / 2;
    mTempPos[0].y = v.y + height / 2;
    mTempPos[3].x = v.z;
    mTempPos[3].y = v.w;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[y].y));
            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }

    //右下
    mTempPos[0].x = v.x + width / 2;
    mTempPos[0].y = v.y;
    mTempPos[3].x = v.z;
    mTempPos[3].y = v.w - height / 2;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[3 - y].y));
            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[3 - y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }
}

void Sliced_Y_Sym_Fill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 br = border * pixelSize;

    if (br.x == 0f && br.y == 0f && br.z == 0f && br.w == 0f)
    {
        Y_Sym_Fill(verts, uvs, cols);
        return;
    }

    Color c = drawingColor;
    Vector4 v = drawingDimensions;

    float width = v.z - v.x;
    //float height = v.w - v.y;

    // 左
    mTempPos[0].x = v.x;
    mTempPos[0].y = v.y;
    mTempPos[3].x = v.z - width / 2;
    mTempPos[3].y = v.w;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y].y));
            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }

    //右
    mTempPos[0].x = v.x + width / 2;
    mTempPos[0].y = v.y;
    mTempPos[3].x = v.z;
    mTempPos[3].y = v.w;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[y].y));
            uvs.Add(new Vector2(mTempUVs[3 - x].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[3 - x2].x, mTempUVs[y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }
}

void Sliced_X_Sym_Fill(List<Vector3> verts, List<Vector2> uvs, List<Color> cols)
{
    Vector4 br = border * pixelSize;

    if (br.x == 0f && br.y == 0f && br.z == 0f && br.w == 0f)
    {
        X_Sym_Fill(verts, uvs, cols);
        return;
    }

    Color c = drawingColor;
    Vector4 v = drawingDimensions;

    //float width = v.z - v.x;
    float height = v.w - v.y;

    // 上
    mTempPos[0].x = v.x;
    mTempPos[0].y = v.y + height / 2;
    mTempPos[3].x = v.z;
    mTempPos[3].y = v.w;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[0].x = mOuterUV.xMax;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[3].x = mOuterUV.xMin;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[0].y = mOuterUV.yMax;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[3].y = mOuterUV.yMin;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y].y));
            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }

    //下
    mTempPos[0].x = v.x;
    mTempPos[0].y = v.y;
    mTempPos[3].x = v.z;
    mTempPos[3].y = v.w - height / 2;

    if (mFlip == Flip.Horizontally || mFlip == Flip.Both)
    {
        mTempPos[1].x = mTempPos[0].x + br.z;
        mTempPos[2].x = mTempPos[3].x - br.x;

        mTempUVs[3].x = mOuterUV.xMin;
        mTempUVs[2].x = mInnerUV.xMin;
        mTempUVs[1].x = mInnerUV.xMax;
        mTempUVs[0].x = mOuterUV.xMax;
    }
    else
    {
        mTempPos[1].x = mTempPos[0].x + br.x;
        mTempPos[2].x = mTempPos[3].x - br.z;

        mTempUVs[0].x = mOuterUV.xMin;
        mTempUVs[1].x = mInnerUV.xMin;
        mTempUVs[2].x = mInnerUV.xMax;
        mTempUVs[3].x = mOuterUV.xMax;
    }

    if (mFlip == Flip.Vertically || mFlip == Flip.Both)
    {
        mTempPos[1].y = mTempPos[0].y + br.y;
        mTempPos[2].y = mTempPos[3].y - br.w;

        mTempUVs[3].y = mOuterUV.yMin;
        mTempUVs[2].y = mInnerUV.yMin;
        mTempUVs[1].y = mInnerUV.yMax;
        mTempUVs[0].y = mOuterUV.yMax;
    }
    else
    {
        mTempPos[1].y = mTempPos[0].y + br.w;
        mTempPos[2].y = mTempPos[3].y - br.y;

        mTempUVs[0].y = mOuterUV.yMin;
        mTempUVs[1].y = mInnerUV.yMin;
        mTempUVs[2].y = mInnerUV.yMax;
        mTempUVs[3].y = mOuterUV.yMax;
    }

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

        for (int y = 0; y < 3; ++y)
        {
            if (centerType == AdvancedType.Invisible && x == 1 && y == 1) continue;

            int y2 = y + 1;

            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
            verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y2].y));
            verts.Add(new Vector3(mTempPos[x2].x, mTempPos[y].y));

            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[3 - y].y));
            uvs.Add(new Vector2(mTempUVs[x].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[3 - y2].y));
            uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[3 - y].y));

            if (!mApplyGradient)
            {
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
                cols.Add(c);
            }
            else
            {
                AddVertexColours(cols,ref c, x, y);
                AddVertexColours(cols,ref c, x, y2);
                AddVertexColours(cols,ref c, x2, y2);
                AddVertexColours(cols,ref c, x2, y);
            }
        }
    }
}

至此,镜像拼接的代码就修改完了。
这种方法可以一定程度上减小包体,但是会导致顶点、三角面数量增加,不过正常的UI制作不会有问题。

另外,NGUI在图片拼接时会出现黑线,具体怎么解决在 https://blog.csdn.net/caishj/article/details/94437870 说明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值