UISprite扩展强化,减少游戏包大小功能扩展【NGUI】

概述

NGUI的uisprite给我们提供了几个默认的样式type。比如裁剪,平铺等等。

合理的利用这些可以让我们的游戏资源变得更加小。比如裁剪。我们利用九宫,只需要很小的一张图。我们就能裁剪到很大。并且效果不错。

但是这样就能满足我们的胃口吗?不,所以我们得加入更多的样式。让我们的资源飞起来。

言归正传。看下面:

预览图

 

扩展后的

2

 

扩展内容效果(透明 区域 四分图 2分图 菱形等等)

8

 

注:四分图及两分图。可以将一个圆形或者对称的矩形。美术分割成四分之一或者二分之一。通过这个,在合成完整的。极限的减少游戏包大小!

下载地址在最下

 

扩展须知

首先。我们打开UISprite的脚本。发现在UIBasicSpriteEditor中实现的这些功能。所以,我们先把我们的type添加进去:

如下:

public enum Type
	{
		Simple,
		Sliced,
		Tiled,
		Filled,
		Advanced,
        /// <summary> 透明 </summary>
        Alphaed,
        /// <summary> 镜像 </summary>
        Mirrored,
        /// <summary> 区域 </summary>
        Cuted,
        /// <summary> 透明裁剪 </summary>
        SlicedAlphaed,
        /// <summary> 多边形 </summary>
        Polygon,
        /// <summary> 四分图 </summary>
        Quarter,
        /// <summary> 二分图 </summary>
        Half,
	}

然后再在将Fill函数修改成如下:

	/// <summary>
	/// Fill the draw buffers.
	/// </summary>

	protected void Fill (BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> 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.Alphaed://add by sxb
            AlphaedFill(verts, uvs, cols);
            break;

            case Type.Mirrored://add by sxb
            MirroredFill(verts, uvs, cols);
            break;

            case Type.Cuted://add by sxb
            CutedFill(verts, uvs, cols);
            break;

            case Type.SlicedAlphaed://add by sxb
            if (mSlicedAlphaSide == false)
            {
                SlicedAlphaedFill(verts, uvs, cols);
            }
            else
            {
                SlicedAlphaedSideFill(verts, uvs, cols);
            }
            break;

            case Type.Polygon://add by sxb
            PolygonedFill(verts, uvs, cols);
            break;

            case Type.Quarter://add by sxb
            QuarterFill(verts, uvs, cols); 
            break;

            case Type.Half://add by sxb
            HalfFill(verts, uvs, cols);  
            break;
		}
       
    }

然后添加type的处理方法。添加在任意位置都可以代码如下

#region Add by sxb (扩展UISprite)

    [HideInInspector]
    [SerializeField]
    float mLeftBottomAlpha = 1.0f;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mLeftTopAlpha = 1.0f;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mRightTopAlpha = 1.0f;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mRightBottomAlpha = 1.0f;//Add by sxb

    [HideInInspector]
    [SerializeField]
    bool mSlicedAlphaSide = false;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mSlicedLeftAlpha = 1.0f;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mSlicedCenterAlpha = 1.0f;//Add by sxb
    [HideInInspector]
    [SerializeField]
    float mSlicedRightAlpha = 1.0f;//Add by sxb

    [HideInInspector]
    [SerializeField]
    Vector2 mTiledOffset = Vector2.zero;//Add by sxb

    [HideInInspector]
    [SerializeField]
    bool mSlicedFilled = false;//Add by sxb

    [HideInInspector]
    [SerializeField]
    Transform mTranSlider = null;//Add by sxb

    public bool slicedAlphaSide
    {
        set
        {
            mSlicedAlphaSide = value;
        }
        get
        {
            return mSlicedAlphaSide;
        }
    }
    public bool slicedFilled
    {
        set
        {
            mSlicedFilled = value;
        }
        get
        {
            return mSlicedFilled;
        }
    }
    public int polygonWidth
    {
        set
        {
            mTiledOffset.x = (float)value;
        }
        get
        {
            return (int)mTiledOffset.x;
        }
    }
    public void SetCutData(float left, float top, float right, float bottom)
    {
        mLeftBottomAlpha = left;
        mLeftTopAlpha = top;
        mRightTopAlpha = right;
        mRightBottomAlpha = bottom;
    }
    private void RefreshSlider()
    {
        if (mTranSlider != null)
        {
            if (mFillDirection == FillDirection.Horizontal)
            {
                float w = mFillAmount * (float)width;
                if (mInvert == true)
                {
                    w = (1.0f - mFillAmount) * (float)width;
                }
                if (w > mTiledOffset.x)
                {
                    if (mTranSlider.gameObject.activeSelf == false)
                    {
                        mTranSlider.gameObject.SetActive(true);
                    }
                    mTranSlider.localPosition = new Vector3(w, mTranSlider.localPosition.y, mTranSlider.localPosition.z);
                }
                else
                {
                    if (mTranSlider.gameObject.activeSelf == true)
                    {
                        mTranSlider.gameObject.SetActive(false);
                    }
                }
            }
            else if (mFillDirection == FillDirection.Vertical)
            {
                float h = mFillAmount * (float)height;
                if (mInvert == true)
                {
                    h = (1.0f - mFillAmount) * (float)height;
                }
                if (h > mTiledOffset.y)
                {
                    if (mTranSlider.gameObject.activeSelf == false)
                    {
                        mTranSlider.gameObject.SetActive(true);
                    }
                    mTranSlider.localPosition = new Vector3(mTranSlider.localPosition.x, h, mTranSlider.localPosition.z);
                }
                else
                {
                    if (mTranSlider.gameObject.activeSelf == true)
                    {
                        mTranSlider.gameObject.SetActive(false);
                    }
                }
            }
        }
    }
    protected void SliceFilledFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        if (mFillAmount < 0.001f) return;

        if (!hasBorder)
        {
            FilledFill(verts, uvs, cols);
            return;
        }

        Vector4 dr = drawingDimensions;
        Vector4 br = border * pixelSize;

        mTempPos[0].x = dr.x;
        mTempPos[0].y = dr.y;
        mTempPos[3].x = dr.z;
        mTempPos[3].y = dr.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;
        }

        Vector4 fillPos = dr;
        if (mFillDirection == FillDirection.Horizontal)
        {
            if (mInvert)
            {
                fillPos.x = dr.z - (dr.z - dr.x) * mFillAmount;
            }
            else
            {
                fillPos.z = dr.x + (dr.z - dr.x) * mFillAmount;
            }
        }
        else if (mFillDirection == FillDirection.Vertical)
        {
            if (mInvert)
            {
                fillPos.y = dr.w - (dr.w - dr.y) * mFillAmount;
            }
            else
            {
                fillPos.w = dr.y + (dr.w - dr.y) * mFillAmount;
            }
        }

        Color colF = color;
        colF.a = finalAlpha;
        Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;

        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;

                //Vector3 vertLeftBottom = new Vector3(mTempPos[x].x, mTempPos[y].y);
                //Vector3 vertRightTop = new Vector3(mTempPos[x2].x, mTempPos[y2].y);
                //Vector2 uvLeftBottom = new Vector2(mTempUVs[x].x, mTempUVs[y].y);
                //Vector2 uvRightTop = new Vector2(mTempUVs[x2].x, mTempUVs[y2].y);

                Vector3 vertLeftBottom = new Vector3(DealVertValueScall(mTempPos[x].x, fillPos.x, fillPos.z),
                    DealVertValueScall(mTempPos[y].y, fillPos.y, fillPos.w));
                Vector3 vertRightTop = new Vector3(DealVertValueScall(mTempPos[x2].x, fillPos.x, fillPos.z),
                    DealVertValueScall(mTempPos[y2].y, fillPos.y, fillPos.w));

                Vector2 uvLeftBottom = new Vector2(DealUvValueScall(mTempUVs[x].x, mTempUVs[x2].x, vertLeftBottom.x, mTempPos[x].x, mTempPos[x2].x),
                    DealUvValueScall(mTempUVs[y].y, mTempUVs[y2].y, vertLeftBottom.y, mTempPos[y].y, mTempPos[y2].y));
                Vector2 uvRightTop = new Vector2(DealUvValueScall(mTempUVs[x].x, mTempUVs[x2].x, vertRightTop.x, mTempPos[x].x, mTempPos[x2].x),
                    DealUvValueScall(mTempUVs[y].y, mTempUVs[y2].y, vertRightTop.y, mTempPos[y].y, mTempPos[y2].y));

                verts.Add(new Vector3(vertLeftBottom.x, vertLeftBottom.y));
                verts.Add(new Vector3(vertLeftBottom.x, vertRightTop.y));
                verts.Add(new Vector3(vertRightTop.x, vertRightTop.y));
                verts.Add(new Vector3(vertRightTop.x, vertLeftBottom.y));

                uvs.Add(new Vector2(uvLeftBottom.x, uvLeftBottom.y));
                uvs.Add(new Vector2(uvLeftBottom.x, uvRightTop.y));
                uvs.Add(new Vector2(uvRightTop.x, uvRightTop.y));
                uvs.Add(new Vector2(uvRightTop.x, uvLeftBottom.y));

                cols.Add(col);
                cols.Add(col);
                cols.Add(col);
                cols.Add(col);
            }
        }
    }

    private float DealVertValueScall(float baseNum, float min, float max)
    {
        if (baseNum < min)
        {
            return min;
        }
        else if (baseNum > max)
        {
            return max;
        }
        else
        {
            return baseNum;
        }
    }
    private float DealUvValueScall(float min, float max, float baseVertNum, float minVert, float maxVert)
    {
        float factor = (baseVertNum - minVert) / (maxVert - minVert);
        float v = min + factor * (max - min);
        return v;
    }

    /// <summary>
    /// 裁剪透明
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void SlicedAlphaedFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        if (!hasBorder)
        {
            AlphaedFill(verts, uvs, cols);
            return;
        }

        Vector4 dr = drawingDimensions;
        Vector4 br = border * pixelSize;

        mTempPos[0].x = dr.x;
        mTempPos[0].y = dr.y;
        mTempPos[3].x = dr.z;
        mTempPos[3].y = dr.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 (verts.size > 0)
        {
            float xMin = dr.x;
            float yMin = dr.y;
            float xMax = dr.z;
            float yMax = dr.w;
            Vector3 a = new Vector3(xMin, yMin, mLeftBottomAlpha);
            Vector3 b1 = new Vector3(xMin, yMax, mLeftTopAlpha);
            Vector3 b2 = new Vector3(xMax, yMin, mRightBottomAlpha);
            Vector3 c = new Vector3(xMax, yMax, mRightTopAlpha);
            float k = (yMax - yMin) / (xMax - xMin);

            for (int i = 0, max = verts.size; i < max; i++)
            {
                Vector3 pos = verts.buffer[i];
                float z = 1.0f;
                float y = k * pos.x;
                if (y > pos.y)
                {
                    z = CountValueBySingleData(a, b2, c, pos);
                }
                else
                {
                    z = CountValueBySingleData(a, b1, c, pos);
                }
                Color colF = color;
                colF.a = finalAlpha * z;
                Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;
                cols.Add(col);
            }
        }
    }
    /// <summary>
    /// 裁剪侧透明
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void SlicedAlphaedSideFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        Vector4 dr = drawingDimensions;
        Vector4 br = border * pixelSize;

        mTempPos[0].x = dr.x;
        mTempPos[0].y = dr.y;
        mTempPos[3].x = dr.z;
        mTempPos[3].y = dr.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;

                if (x == 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[x].x + mTempPos[x2].x) / 2.0f, mTempPos[y2].y));
                    verts.Add(new Vector3((mTempPos[x].x + mTempPos[x2].x) / 2.0f, 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[x].x + mTempUVs[x2].x) / 2.0f, mTempUVs[y2].y));
                    uvs.Add(new Vector2((mTempUVs[x].x + mTempUVs[x2].x) / 2.0f, mTempUVs[y].y));

                    verts.Add(new Vector3((mTempPos[x].x + mTempPos[x2].x) / 2.0f, mTempPos[y].y));
                    verts.Add(new Vector3((mTempPos[x].x + mTempPos[x2].x) / 2.0f, 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[x2].x) / 2.0f, mTempUVs[y].y));
                    uvs.Add(new Vector2((mTempUVs[x].x + mTempUVs[x2].x) / 2.0f, mTempUVs[y2].y));
                    uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
                    uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));
                }
                else
                {
                    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 (verts.size > 0)
        {
            float xMin = dr.x;
            float yMin = dr.y;
            float xMax = dr.z;
            float yMax = dr.w;

            Vector3 leftBottom = new Vector3(xMin, yMin, mSlicedLeftAlpha);
            Vector3 leftTop = new Vector3(xMin, yMax, mSlicedLeftAlpha);
            Vector3 CenterBottom = new Vector3((xMin + xMax) / 2.0f, yMin, mSlicedCenterAlpha);
            Vector3 CenterTop = new Vector3((xMin + xMax) / 2.0f, yMax, mSlicedCenterAlpha);
            Vector3 RightTop = new Vector3(xMax, yMax, mSlicedRightAlpha);

            float centerX = (xMin + xMax) / 2.0f;

            for (int i = 0, max = verts.size; i < max; i++)
            {
                Vector3 pos = verts.buffer[i];
                float z = 1.0f;
                if (pos.x < centerX)
                {
                    z = CountValueBySingleData(leftBottom, leftTop, CenterTop, pos);
                }
                else
                {
                    z = CountValueBySingleData(CenterBottom, CenterTop, RightTop, pos);
                }
                Color colF = color;
                colF.a = finalAlpha * z;
                Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;
                cols.Add(col);
            }
        }
    }
    private float CountValueBySingleData(Vector3 a, Vector3 b, Vector3 c, Vector3 result)
    {
        Vector3 ab = b - a;
        Vector3 ac = c - a;
        Vector3 n = new Vector3(ab.y * ac.z - ab.z * ac.y, ac.x * ab.z - ab.x * ac.z, ab.x * ac.y - ab.y * ac.x);
        return a.z - (n.x * (result.x - a.x) + n.y * (result.y - a.y)) / n.z;
    }

    /// <summary>
    /// 镜像手法
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void MirroredFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        Texture tex = material.mainTexture;
        if (tex == null) return;

        Vector4 dr = drawingDimensions;
        Vector2 size = new Vector2(mInnerUV.width * tex.width, mInnerUV.height * tex.height);
        size *= pixelSize;

        Color colF = color;
        colF.a = finalAlpha;
        Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;

        float x0 = dr.x;
        float y0 = dr.y;

        float u0 = mInnerUV.xMin;
        float v0 = mInnerUV.yMin;

        int numY = 0;

        while (y0 < dr.w)
        {
            x0 = dr.x;
            float y1 = y0 + size.y;
            float v1 = mInnerUV.yMax;

            if (y1 > dr.w)
            {
                v1 = Mathf.Lerp(mInnerUV.yMin, mInnerUV.yMax, (dr.w - y0) / size.y);
                y1 = dr.w;
            }

            int numX = 0;

            while (x0 < dr.z)
            {
                float x1 = x0 + size.x;
                float u1 = mInnerUV.xMax;

                if (x1 > dr.z)
                {
                    u1 = Mathf.Lerp(mInnerUV.xMin, mInnerUV.xMax, (dr.z - x0) / size.x);
                    x1 = dr.z;
                }
                if (numX % 2 == 1 && numY % 2 == 0)
                {
                    verts.Add(new Vector3(x0, y0));
                    verts.Add(new Vector3(x0, y1));
                    verts.Add(new Vector3(x1, y1));
                    verts.Add(new Vector3(x1, y0));

                    uvs.Add(new Vector2(u1, v0));
                    uvs.Add(new Vector2(u1, v1));
                    uvs.Add(new Vector2(u0, v1));
                    uvs.Add(new Vector2(u0, v0));
                }
                else if (numX % 2 == 0 && numY % 2 == 1)
                {
                    verts.Add(new Vector3(x0, y0));
                    verts.Add(new Vector3(x0, y1));
                    verts.Add(new Vector3(x1, y1));
                    verts.Add(new Vector3(x1, y0));

                    uvs.Add(new Vector2(u0, v1));
                    uvs.Add(new Vector2(u0, v0));
                    uvs.Add(new Vector2(u1, v0));
                    uvs.Add(new Vector2(u1, v1));
                }
                else if (numX % 2 == 1 && numY % 2 == 1)
                {
                    verts.Add(new Vector3(x0, y0));
                    verts.Add(new Vector3(x0, y1));
                    verts.Add(new Vector3(x1, y1));
                    verts.Add(new Vector3(x1, y0));

                    uvs.Add(new Vector2(u1, v1));
                    uvs.Add(new Vector2(u1, v0));
                    uvs.Add(new Vector2(u0, v0));
                    uvs.Add(new Vector2(u0, v1));
                }
                else
                {
                    verts.Add(new Vector3(x0, y0));
                    verts.Add(new Vector3(x0, y1));
                    verts.Add(new Vector3(x1, y1));
                    verts.Add(new Vector3(x1, y0));

                    uvs.Add(new Vector2(u0, v0));
                    uvs.Add(new Vector2(u0, v1));
                    uvs.Add(new Vector2(u1, v1));
                    uvs.Add(new Vector2(u1, v0));
                }

                cols.Add(col);
                cols.Add(col);
                cols.Add(col);
                cols.Add(col);

                x0 += size.x + mTiledOffset.x - 0.5f;
                numX++;
            }
            y0 += size.y + mTiledOffset.y - 0.5f;
            numY++;
        }
    }
    /// <summary>
    /// 多边形
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void PolygonedFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        Color colF = color;
        colF.a = finalAlpha;
        Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;
        Vector4 v = drawingDimensions;
        Vector4 u = drawingUVs;

        if (mTiledOffset.x > 0.0f)
        {
            float deltaPos = (float)width / 2.0f;
            if (deltaPos > (float)height / 2.0f)
            {
                deltaPos = (float)height / 2.0f;
            }
            if (mTiledOffset.x < deltaPos)
            {
                deltaPos = mTiledOffset.x;
            }
            float deltaUV = (u.z - u.x) * deltaPos / (v.z - v.x);

            mTempPos[0] = new Vector2(v.x, v.y);
            mTempPos[1] = new Vector2(v.x + deltaPos, v.y + deltaPos);
            mTempPos[2] = new Vector2(v.z - deltaPos, v.w - deltaPos);
            mTempPos[3] = new Vector2(v.z, v.w);


            mTempUVs[0] = new Vector2(u.x, u.y);
            mTempUVs[1] = new Vector2(u.x + deltaUV, u.y + deltaUV);
            mTempUVs[2] = new Vector2(u.z - deltaUV, u.w - deltaUV);
            mTempUVs[3] = new Vector2(u.z, u.w);

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

                    if (x == 0 && y == 0)
                    {
                        verts.Add(new Vector3(mTempPos[x].x, mTempPos[y2].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[y2].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));
                    }
                    else if (x == 0 && y == 2)
                    {
                        verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].y));
                        verts.Add(new Vector3(mTempPos[x].x, mTempPos[y].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[y].y));
                        uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y2].y));
                        uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));
                    }
                    else if (x == 2 && y == 0)
                    {
                        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[y2].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[y2].y));
                    }
                    else if (x == 2 && y == 2)
                    {
                        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[x].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[x].x, mTempUVs[y2].y));
                        uvs.Add(new Vector2(mTempUVs[x2].x, mTempUVs[y].y));
                    }
                    else
                    {
                        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));
                    }

                    cols.Add(col);
                    cols.Add(col);
                    cols.Add(col);
                    cols.Add(col);
                }
            }
            return;
        }

        mTempPos[0] = new Vector2(v.x, v.y);
        mTempPos[1] = new Vector2(v.x, v.w);
        mTempPos[2] = new Vector2(v.z, v.w);
        mTempPos[3] = new Vector2(v.z, v.y);

        mTempUVs[0] = new Vector2(u.x, u.y);
        mTempUVs[1] = new Vector2(u.x, u.w);
        mTempUVs[2] = new Vector2(u.z, u.w);
        mTempUVs[3] = new Vector2(u.z, u.y);

        // Fill the buffer with the quad for the sprite
        for (int i = 0; i < 4; ++i)
        {
            verts.Add(mTempPos[i]);
            uvs.Add(mTempUVs[i]);
            cols.Add(col);
        }
    }

    /// <summary>
    /// 区域
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void CutedFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)//Add by sxb CutedFill
    {
        Color colF = color;
        colF.a = finalAlpha;
        Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;
        Vector4 v = drawingDimensions;

        float tx0 = mOuterUV.xMin;
        float ty0 = mOuterUV.yMin;
        float tx1 = mOuterUV.xMax;
        float ty1 = mOuterUV.yMax;

        tx0 = (mOuterUV.xMax + mOuterUV.xMin) / 2.0f - (mOuterUV.xMax - mOuterUV.xMin) / 2.0f * mLeftBottomAlpha;
        tx1 = (mOuterUV.xMax + mOuterUV.xMin) / 2.0f + (mOuterUV.xMax - mOuterUV.xMin) / 2.0f * mRightTopAlpha;
        ty0 = (mOuterUV.yMax + mOuterUV.yMin) / 2.0f - (mOuterUV.yMax - mOuterUV.yMin) / 2.0f * mRightBottomAlpha;
        ty1 = (mOuterUV.yMax + mOuterUV.yMin) / 2.0f + (mOuterUV.yMax - mOuterUV.yMin) / 2.0f * mLeftTopAlpha;

        v.x = (drawingDimensions.x + drawingDimensions.z) / 2.0f - (drawingDimensions.z - drawingDimensions.x) / 2.0f * mLeftBottomAlpha;
        v.z = (drawingDimensions.x + drawingDimensions.z) / 2.0f + (drawingDimensions.z - drawingDimensions.x) / 2.0f * mRightTopAlpha;
        v.y = (drawingDimensions.y + drawingDimensions.w) / 2.0f - (drawingDimensions.w - drawingDimensions.y) / 2.0f * mRightBottomAlpha;
        v.w = (drawingDimensions.y + drawingDimensions.w) / 2.0f + (drawingDimensions.w - drawingDimensions.y) / 2.0f * mLeftTopAlpha;

        mTempPos[0] = new Vector2(v.x, v.y);
        mTempPos[1] = new Vector2(v.x, v.w);
        mTempPos[2] = new Vector2(v.z, v.w);
        mTempPos[3] = new Vector2(v.z, v.y);

        mTempUVs[0] = new Vector2(tx0, ty0);
        mTempUVs[1] = new Vector2(tx0, ty1);
        mTempUVs[2] = new Vector2(tx1, ty1);
        mTempUVs[3] = new Vector2(tx1, ty0);

        // Fill the buffer with the quad for the sprite
        for (int i = 0; i < 4; ++i)
        {
            verts.Add(mTempPos[i]);
            uvs.Add(mTempUVs[i]);
            cols.Add(col);
        }
    }

    /// <summary>
    /// 透明手法
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void AlphaedFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)//Add by sxb
    {
        Vector4 v = drawingDimensions;

        float tx0 = mOuterUV.xMin;
        float ty0 = mOuterUV.yMin;
        float tx1 = mOuterUV.xMax;
        float ty1 = mOuterUV.yMax;

        mTempPos[0] = new Vector2(v.x, v.y);
        mTempPos[1] = new Vector2(v.x, v.w);
        mTempPos[2] = new Vector2(v.z, v.w);
        mTempPos[3] = new Vector2(v.z, v.y);

        mTempUVs[0] = new Vector2(tx0, ty0);
        mTempUVs[1] = new Vector2(tx0, ty1);
        mTempUVs[2] = new Vector2(tx1, ty1);
        mTempUVs[3] = new Vector2(tx1, ty0);

        // Fill the buffer with the quad for the sprite
        for (int i = 0; i < 4; ++i)
        {
            verts.Add(mTempPos[i]);
            uvs.Add(mTempUVs[i]);
            //---
            Color colF = color;
            switch (i)
            {
                case 0: colF.a = finalAlpha * mLeftBottomAlpha; break;
                case 1: colF.a = finalAlpha * mLeftTopAlpha; break;
                case 2: colF.a = finalAlpha * mRightTopAlpha; break;
                default: colF.a = finalAlpha * mRightBottomAlpha; break;
            }
            Color32 col = premultipliedAlpha ? NGUITools.ApplyPMA(colF) : colF;
            cols.Add(col);
        }
    }

    /// <summary>
    /// 四分图手法;
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void QuarterFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        Vector4 v = drawingDimensions;
        Vector4 u = drawingUVs;
        Color32 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);

    }

    /// <summary>
    /// 二分图手法;
    /// </summary>
    /// <param name="verts"></param>
    /// <param name="uvs"></param>
    /// <param name="cols"></param>
    protected void HalfFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
    {
        Vector4 v = drawingDimensions;
        Vector4 u = drawingUVs;
        Color32 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);
    }
    


    #endregion

 

最后。给我们的几个需要修改Edit的添加界面绘制。如下

修改 UIBasicSpriteEditor脚本。在多重判断处,添加如下代码

else if (type == UISprite.Type.SlicedAlphaed)
        {
            NGUIEditorTools.DrawBorderProperty("Border", serializedObject, "mBorder");
            NGUIEditorTools.DrawProperty("Flip", serializedObject, "mFlip");

            EditorGUI.BeginDisabledGroup(sp.hasMultipleDifferentValues);
            {
                sp = serializedObject.FindProperty("centerType");
                bool val = (sp.intValue != (int)UISprite.AdvancedType.Invisible);

                if (val != EditorGUILayout.Toggle("Fill Center", val))
                {
                    sp.intValue = val ? (int)UISprite.AdvancedType.Invisible : (int)UISprite.AdvancedType.Sliced;
                }
            }
            EditorGUI.EndDisabledGroup();

            NGUIEditorTools.DrawProperty("SlicedAlphaSide", serializedObject, "mSlicedAlphaSide");
            if (mBasicSprite != null && mBasicSprite.slicedAlphaSide == false)
            {
                NGUIEditorTools.DrawProperty("Left Bottm", serializedObject, "mLeftBottomAlpha", GUILayout.MinWidth(20f));
                NGUIEditorTools.DrawProperty("Left Top", serializedObject, "mLeftTopAlpha", GUILayout.MinWidth(20f));
                NGUIEditorTools.DrawProperty("Right Top", serializedObject, "mRightTopAlpha", GUILayout.MinWidth(20f));
                NGUIEditorTools.DrawProperty("Right Bottom", serializedObject, "mRightBottomAlpha", GUILayout.MinWidth(20f));
            }
            else
            {
                NGUIEditorTools.DrawProperty("Left", serializedObject, "mSlicedLeftAlpha", GUILayout.MinWidth(20f));
                NGUIEditorTools.DrawProperty("Center", serializedObject, "mSlicedCenterAlpha", GUILayout.MinWidth(20f));
                NGUIEditorTools.DrawProperty("Right", serializedObject, "mSlicedRightAlpha", GUILayout.MinWidth(20f));
            }
        }
        else if (type == UISprite.Type.Polygon)
        {//Add by mgp
            NGUIEditorTools.DrawProperty("Flip", serializedObject, "mFlip");
            NGUIEditorTools.DrawProperty("Width", serializedObject, "mTiledOffset.x", GUILayout.MinWidth(20f));
        }
        else if (type == UISprite.Type.Cuted)
        {//Add by mgp
            NGUIEditorTools.DrawProperty("Left", serializedObject, "mLeftBottomAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Top", serializedObject, "mLeftTopAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Right", serializedObject, "mRightTopAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Bottom", serializedObject, "mRightBottomAlpha", GUILayout.MinWidth(20f));
        }
        else if (type == UISprite.Type.Alphaed)
        {//Add by mgp
            NGUIEditorTools.DrawProperty("Left Bottm", serializedObject, "mLeftBottomAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Left Top", serializedObject, "mLeftTopAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Right Top", serializedObject, "mRightTopAlpha", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Right Bottom", serializedObject, "mRightBottomAlpha", GUILayout.MinWidth(20f));
        }
        else if (type == UISprite.Type.Mirrored)
        {//Add by mgp
            NGUIEditorTools.DrawProperty("Offset X", serializedObject, "mTiledOffset.x", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Offset Y", serializedObject, "mTiledOffset.y", GUILayout.MinWidth(20f));
            NGUIEditorTools.DrawProperty("Flip", serializedObject, "mFlip");
        }

好了。大功告成。

下载地址

 示例NUIG版本为 3.8.1。以上方案适用于所有NGUI版本。

链接:http://pan.baidu.com/s/1pJqSOmJ 密码:lj2o

 

 

本文链接:

https://bobsong.cn/821.html
1 + 8 =
3 评论
    落叶
    2015年06月02日 回复

    大神,有没有中间空白的那一种模式啊

      2015年07月02日 回复

      @落叶 自带的就有这功能的哦

    2015年09月22日 回复

    if (mBasicSprite != null && mBasicSprite.slicedAlphaSide == false) 的mBasicSprite 没有定义啊