LineRenderer 简单画线

19 篇文章 0 订阅

 通过unity内置的LineRenderer 绘制线条。

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

public class Painter : MonoBehaviour
{
    //颜色
    public Color painterColor = Color.yellow;
    //宽度
    public float Width=0.2f;

    private LineRenderer currentLineRenderer;
    private int currentPoint = -1;
    private Vector3 lastPoint;
    private bool isStart=false;



    public void EndDraw()
    {
        //鼠标抬起后,清除上一次的笔记记录。
        currentLineRenderer = null;
        currentPoint = -1;
    }

    public bool IsLine = true;
    public void Draw()
    {
        if(IsLine)
        {
            DrawLine();
        }
        else
        {
            DrawSeal();
        }               
    }

    //划线
    public void DrawLine()
    {
        Vector3 point = transform.position;
        //判断现在是否有LineRenderer,配合鼠标抬起,可以再次下笔的时候新建一个画笔
        if (!currentLineRenderer)
        {
            GameObject line = new GameObject("Line");
            currentLineRenderer = line.AddComponent<LineRenderer>();
            currentLineRenderer.numCapVertices = 90;
            //设置你喜欢的宽度
            currentLineRenderer.startWidth = currentLineRenderer.endWidth = Width;
            //给一个你喜欢的材质球
            currentLineRenderer.material = new Material(Shader.Find("Particles/Additive"));
            currentLineRenderer.startColor = painterColor;
            currentLineRenderer.endColor = painterColor;

            currentLineRenderer.material.SetColor("_TintColor", painterColor);
        
            currentLineRenderer.positionCount = 0;
            PainterContrl.Instance.AllLine.Add(currentLineRenderer);
        }
        //如果鼠标停在一个地方,就没必要一直加点了,提升效率
        if (lastPoint != point)
        {
            //画之前,先给LineRenderer扩容,它并不聪明
            currentLineRenderer.positionCount++;
            //把点给它,它自己会画
            currentLineRenderer.SetPosition(++currentPoint, point);
            lastPoint = point;
        }
    }

    //画印章
    public void DrawSeal()
    {

        Vector3 point = transform.position;
        if(IsOne)
        {
            IsOne = false;
            CreatSeal(point);
        }
        else
        {
            float Dis = Vector3.Distance(lastPoint, point);
            if(Dis > SealMidOffset)
            {
                CreatSeal(point);

            }
        }
    }
    private bool IsOne = true;
    public float SealMidOffset = 1f;

    [HideInInspector]public float SealWidth = 1;
    [HideInInspector] public float SealHeight = 1f;
    private void CreatSeal(Vector3 Point)
    {
        GameObject line = new GameObject("Line");
        currentLineRenderer = line.AddComponent<LineRenderer>();
        //给一个你喜欢的材质球
        currentLineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        //设置你喜欢的宽度
        currentLineRenderer.startWidth = SealHeight;
        currentLineRenderer.endWidth = SealHeight;
        currentLineRenderer.material.SetTexture("_MainTex", PainterContrl.Instance.Seals[PainterContrl.Instance.SealIndex]);     
        currentLineRenderer.positionCount = 2;
        currentLineRenderer.SetPosition(0, Point - Vector3.up * SealWidth / 2);
        currentLineRenderer.SetPosition(1, Point + Vector3.up * SealWidth / 2);

        PainterContrl.Instance.AllLine.Add(currentLineRenderer);
        lastPoint = Point;
        currentLineRenderer = null;
    }
    //private void OnGUI()
    //{
    //    GUI.skin.label.fontSize = 50;
    //    GUI.color = Color.red;
    //    GUILayout.Label(painterColor.ToString());
    //}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值