Unity使用UGUI划线

Unity 里面虽然提供Linerender绘制线条,但是只能在3D空间划线,有时候需要在UI上绘制指定的线条,柱状图,饼状图等就可以采用下面的方式了。
在这里插入图片描述
创建DrawLine,继承MaskableGraphic类,重写OnPopulateMesh(VertexHelper vh)方法,重新使用mesh绘制线条,实例代码如下

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DrawLine : MaskableGraphic
{
    public Texture texture;
    public override Texture mainTexture => texture;

	List<List<UIVertex>> vertexQuadList = new List<List<UIVertex>>();
    List<UIVertex> vertexQuad;
    public float lineWidth=2;
    Vector3 lastleftPoint;
    Vector3 lastrightPoint;
    Vector3 lastPos;
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
        for (int i = 0; i < vertexQuadList.Count; i++)
        {
            vh.AddUIVertexQuad(vertexQuadList[i].ToArray());
        }
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            vertexQuadList.Clear();
            lastPos = Input.mousePosition;
            lastleftPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) + Vector3.up * lineWidth;
            lastrightPoint = lastPos - new Vector3(Screen.width / 2, Screen.height / 2, 0) - Vector3.up * lineWidth;
        }
        else
        {
            if (Input.GetMouseButton(0))
            {
                Vector3 newVec = Input.mousePosition - lastPos;
                if (newVec.magnitude < 0.1f)
                {
                    return;
                }

                vertexQuad = new List<UIVertex>();
                Vector3 vec = Vector3.Cross(newVec.normalized, Vector3.forward).normalized;
                Vector3 newleftPoint = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0) + vec * lineWidth;
                Vector3 newrightPoint = Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0) - vec * lineWidth;
                UIVertex uIVertex = new UIVertex();
                uIVertex.position = lastleftPoint;
                uIVertex.color = color;
                vertexQuad.Add(uIVertex);
                UIVertex uIVertex1 = new UIVertex();
                uIVertex1.position = lastrightPoint;
                uIVertex1.color = color;
                vertexQuad.Add(uIVertex1);
                UIVertex uIVertex3 = new UIVertex();
                uIVertex3.position = newrightPoint;
                uIVertex3.color = color;
                vertexQuad.Add(uIVertex3);
                UIVertex uIVertex2 = new UIVertex();
                uIVertex2.position = newleftPoint;
                uIVertex2.color = color;
                vertexQuad.Add(uIVertex2);
                lastleftPoint = newleftPoint;
                lastrightPoint = newrightPoint;
                vertexQuadList.Add(vertexQuad);

                lastPos = Input.mousePosition;

                SetVerticesDirty();
            }
        }
    }

}

工程链接:https://download.csdn.net/download/qq_33547099/14037991

在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值