Example #1
0
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        // Vector4 v = GetDrawingDimensions();
        // var uv = (overrideSprite != null) ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;
        // print("//?uv"+uv);

        // var color32 = color;
        // toFill.Clear();
        // toFill.AddVert(new Vector3(v.x, v.y), color32, new Vector2(uv.x, uv.y));
        // toFill.AddVert(new Vector3(v.x, v.w), color32, new Vector2(uv.x, uv.w));
        // toFill.AddVert(new Vector3(v.z, v.w), color32, new Vector2(uv.z, uv.w));
        // toFill.AddVert(new Vector3(v.z, v.y), color32, new Vector2(uv.z, uv.y));
        // toFill.AddTriangle(0, 1, 2);
        // toFill.AddTriangle(2, 3, 0);


        vertexList = new List <Vector2>();
        toFill.Clear();
        //Sprite外部UV参数
        Vector4 uv = overrideSprite != null?DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;

        //Vector2[] uvs = overrideSprite.uv;
        //获取必要的参数
        float   width     = rectTransform.rect.width;
        float   height    = rectTransform.rect.height;
        float   uv_width  = uv.z - uv.x;
        float   uv_height = uv.w - uv.y;
        Vector2 uv_centre = new Vector2(uv_width * 0.5f + uv.x, uv_height * 0.5f + uv.y);
        Vector2 rate      = new Vector2(uv_width / width, uv_height / height);
        //计算半径和弧度
        float radius = redius <= 0 ? width / 2 : redius;
        float radian = (2 * Mathf.PI) / segments;
        //添加初始点
        UIVertex origin          = new UIVertex();
        var      spriteSize      = new Vector2(overrideSprite.rect.width, overrideSprite.rect.height);
        var      spritePivot     = overrideSprite.pivot / spriteSize;
        var      rectPivot       = rectTransform.pivot;
        Rect     r               = GetPixelAdjustedRect();
        var      drawingSize     = new Vector2(r.width, r.height);
        var      spriteBoundSize = overrideSprite.bounds.size;
        var      drawOffset      = (rectPivot - spritePivot) * drawingSize;

        Vector2[] vertices   = overrideSprite.vertices;
        Vector2   origin_pos = new Vector2((0.5f - rectTransform.pivot.x) * width, (0.5f - rectTransform.pivot.y) * height);//原点位置

        origin.color    = color;
        origin.position = origin_pos;
        float uv_x = originPos.x * rate.x + uv_centre.x;
        float uv_y = originPos.y * rate.y + uv_centre.y;

        origin.uv0 = new Vector2(uv_x, uv_y);
        toFill.AddVert(origin);
        //添加其他点

        // toFill.AddVert(new Vector3(v.x, v.y), color, new Vector2(uv.x, uv.y));
        // toFill.AddVert(new Vector3(v.x, v.w), color, new Vector2(uv.x, uv.w));
        // toFill.AddVert(new Vector3(v.z, v.w), color, new Vector2(uv.z, uv.w));
        // toFill.AddVert(new Vector3(v.z, v.y), color, new Vector2(uv.z, uv.y));

        float total_radian = 0;

        for (int i = 0; i < segments + 1; i++)
        {
            //角度转为弧度
            float x      = Mathf.Cos(total_radian + MathTool.AngleToRadian(rotation)) * radius;
            float y      = Mathf.Sin(total_radian + MathTool.AngleToRadian(rotation)) * radius;
            float real_x = x;
            float real_y = y;
            // float k = (origin_pos.y - y) / (origin_pos.x - x);
            // if (x > v.z)
            // {
            //     real_x = v.z;
            //     real_y = k * (real_x - x) + y;
            // }
            // if (x < v.x)
            // {
            //     real_x = v.x;
            //     real_y = k * (real_x - x) + y;
            // }
            // if (y > v.w)
            // {
            //     real_y = v.w;
            //     real_x = (real_y - y) / k + x;
            // }
            // if (y < v.y)
            // {
            //     real_y = v.y;
            //     real_x = (real_y - y) / k + x;
            // }
            total_radian += radian;
            origin        = new UIVertex();
            Vector2 other_pos = new Vector2(real_x, real_y) + origin_pos;
            origin.color    = color;
            origin.position = other_pos;
            uv_x            = ((originPos.x + x) * rate.x + uv_centre.x);
            uv_y            = ((originPos.y + y) * rate.y + uv_centre.y);
            origin.uv0      = new Vector2(uv_x, uv_y);
            toFill.AddVert(origin);
            vertexList.Add(other_pos);
        }
        //绘制圆形
        for (int i = 1; i <= segments; i++)
        {
            toFill.AddTriangle(i, 0, i + 1);
        }
    }