FairyGUI.Image.PrintTo C# (CSharp) Method

PrintTo() public method

截取当前图片的一部分输出到另一个Mesh。不支持图片的填充模式、九宫格的平铺模式。
public PrintTo ( Mesh mesh, Rect localRect ) : void
mesh UnityEngine.Mesh 目标Mesh
localRect UnityEngine.Rect 制定图片的区域
return void
        public void PrintTo(Mesh mesh, Rect localRect)
        {
            if (_requireUpdateMesh)
                Rebuild();

            Rect uvRect = _texture.uvRect;
            if (_flip != FlipType.None)
                ToolSet.FlipRect(ref uvRect, _flip);

            Vector3[] verts;
            Vector2[] uv;
            Color32[] colors;
            int[] triangles;
            int vertCount = 0;

            if (!_scaleByTile || _scale9Grid == null || (_texture.width == _contentRect.width && _texture.height == _contentRect.height))
            {
                verts = new Vector3[graphics.vertices.Length];
                uv = new Vector2[graphics.uv.Length];

                Rect bound = ToolSet.Intersection(ref _contentRect, ref localRect);

                float u0 = bound.xMin / _contentRect.width;
                float u1 = bound.xMax / _contentRect.width;
                float v0 = (_contentRect.height - bound.yMax) / _contentRect.height;
                float v1 = (_contentRect.height - bound.yMin) / _contentRect.height;
                u0 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u0);
                u1 = Mathf.Lerp(uvRect.xMin, uvRect.xMax, u1);
                v0 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v0);
                v1 = Mathf.Lerp(uvRect.yMin, uvRect.yMax, v1);
                NGraphics.FillUVOfQuad(uv, 0, Rect.MinMaxRect(u0, v0, u1, v1));

                bound.x = 0;
                bound.y = 0;
                NGraphics.FillVertsOfQuad(verts, 0, bound);
                vertCount += 4;
            }
            else if (_scaleByTile)
            {
                verts = new Vector3[graphics.vertices.Length];
                uv = new Vector2[graphics.uv.Length];

                int hc = Mathf.CeilToInt(_contentRect.width / _texture.width);
                int vc = Mathf.CeilToInt(_contentRect.height / _texture.height);
                float tailWidth = _contentRect.width - (hc - 1) * _texture.width;
                float tailHeight = _contentRect.height - (vc - 1) * _texture.height;

                Vector2 offset = Vector2.zero;
                for (int i = 0; i < hc; i++)
                {
                    for (int j = 0; j < vc; j++)
                    {
                        Rect rect = new Rect(i * _texture.width, j * _texture.height,
                                i == (hc - 1) ? tailWidth : _texture.width, j == (vc - 1) ? tailHeight : _texture.height);
                        Rect uvTmp = uvRect;
                        if (i == hc - 1)
                            uvTmp.xMax = Mathf.Lerp(uvRect.xMin, uvRect.xMax, tailWidth / _texture.width);
                        if (j == vc - 1)
                            uvTmp.yMin = Mathf.Lerp(uvRect.yMin, uvRect.yMax, 1 - tailHeight / _texture.height);

                        Rect bound = ToolSet.Intersection(ref rect, ref localRect);
                        if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                        {
                            float u0 = (bound.xMin - rect.x) / rect.width;
                            float u1 = (bound.xMax - rect.x) / rect.width;
                            float v0 = (rect.y + rect.height - bound.yMax) / rect.height;
                            float v1 = (rect.y + rect.height - bound.yMin) / rect.height;
                            u0 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u0);
                            u1 = Mathf.Lerp(uvTmp.xMin, uvTmp.xMax, u1);
                            v0 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v0);
                            v1 = Mathf.Lerp(uvTmp.yMin, uvTmp.yMax, v1);
                            NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                            if (i == 0 && j == 0)
                                offset = new Vector2(bound.x, bound.y);
                            bound.x -= offset.x;
                            bound.y -= offset.y;

                            NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                            vertCount += 4;
                        }
                    }
                }
            }
            else
            {
                Rect gridRect = (Rect)_scale9Grid;

                if (_flip != FlipType.None)
                    ToolSet.FlipInnerRect(_texture.width, _texture.height, ref gridRect, _flip);

                GenerateGrids(gridRect, uvRect);

                verts = new Vector3[36];
                uv = new Vector2[36];
                Vector2 offset = new Vector2();

                Rect drawRect;
                Rect texRect;
                int row, col;
                float u0, u1, v0, v1;

                for (int pi = 0; pi < 9; pi++)
                {
                    col = pi % 3;
                    row = pi / 3;
                    drawRect = Rect.MinMaxRect(gridX[col], gridY[row], gridX[col + 1], gridY[row + 1]);
                    texRect = Rect.MinMaxRect(gridTexX[col], gridTexY[row + 1], gridTexX[col + 1], gridTexY[row]);
                    Rect bound = ToolSet.Intersection(ref drawRect, ref localRect);
                    if (bound.xMax - bound.xMin >= 0 && bound.yMax - bound.yMin > 0)
                    {
                        u0 = (bound.xMin - drawRect.x) / drawRect.width;
                        u1 = (bound.xMax - drawRect.x) / drawRect.width;
                        v0 = (drawRect.yMax - bound.yMax) / drawRect.height;
                        v1 = (drawRect.yMax - bound.yMin) / drawRect.height;
                        u0 = Mathf.Lerp(texRect.xMin, texRect.xMax, u0);
                        u1 = Mathf.Lerp(texRect.xMin, texRect.xMax, u1);
                        v0 = Mathf.Lerp(texRect.yMin, texRect.yMax, v0);
                        v1 = Mathf.Lerp(texRect.yMin, texRect.yMax, v1);
                        NGraphics.FillUVOfQuad(uv, vertCount, Rect.MinMaxRect(u0, v0, u1, v1));

                        if (vertCount == 0)
                            offset = new Vector2(bound.x, bound.y);
                        bound.x -= offset.x;
                        bound.y -= offset.y;
                        NGraphics.FillVertsOfQuad(verts, vertCount, bound);

                        vertCount += 4;
                    }
                }
            }

            if (vertCount != verts.Length)
            {
                Array.Resize(ref verts, vertCount);
                Array.Resize(ref uv, vertCount);
            }
            int triangleCount = (vertCount >> 1) * 3;
            triangles = new int[triangleCount];
            int k = 0;
            for (int i = 0; i < vertCount; i += 4)
            {
                triangles[k++] = i;
                triangles[k++] = i + 1;
                triangles[k++] = i + 2;

                triangles[k++] = i + 2;
                triangles[k++] = i + 3;
                triangles[k++] = i;
            }

            colors = new Color32[vertCount];
            for (int i = 0; i < vertCount; i++)
            {
                Color col = _color;
                col.a = this.alpha;
                colors[i] = col;
            }

            mesh.Clear();
            mesh.vertices = verts;
            mesh.uv = uv;
            mesh.triangles = triangles;
            mesh.colors32 = colors;
        }

Usage Example

Example #1
0
 static public int PrintTo(IntPtr l)
 {
     try {
         FairyGUI.Image   self = (FairyGUI.Image)checkSelf(l);
         UnityEngine.Mesh a1;
         checkType(l, 2, out a1);
         UnityEngine.Rect a2;
         checkValueType(l, 3, out a2);
         self.PrintTo(a1, a2);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }