使用LineRenderer实现画图程序

首先我们来看下LineRenderer的一些参数属性


使用LineRenderer实现画图程序_第1张图片
image.png

Color里面的颜色想要设置成功需要把材质的Shader设置为Sprites->Default


使用LineRenderer实现画图程序_第2张图片
image.png
下面我们来看看怎么写一个画图小程序,首先看看我的布局,首先是三种颜色的选择和三种画笔大小的选择,然后一个背景
使用LineRenderer实现画图程序_第3张图片
image.png

下面是创建脚本LineRendererController写入以下代码挂载在LineRenderers游戏对象上

using System.Collections.Generic;
using UnityEngine;

public class LineRendererController : MonoBehaviour {

    public  Color color=Color.red;
    public  float painSize = 0.1f;
    private LineRenderer CurrentLine;
    public Material materia;
    private List positions = new List();
    private bool isMouseDown = false;//是否一直按住鼠标
    private Vector3 lastMousePosition;//下一个位置
  

    #region 选择颜色
    public void OnRedColorChanged(bool isOn)
    {
        if (isOn)
        {
            color = Color.red;
        }
    }
    public void OnGreenColorChanged(bool isOn)
    {
        if (isOn)
        {
            color = Color.green;
    
        }
 
    }
    public void OnBlueColorChanged(bool isOn)
    {
        if (isOn)
        {
            color = Color.blue;
  
        }
    }
    #endregion

    #region 选择大小
    public void OnPoint1Changed(bool isOn)
    {
        if (isOn)
        {
            painSize = 0.1f;

        }
    }
    public void OnPoint2Changed(bool isOn)
    {
        if (isOn)
        {
            painSize =0.2f;

        }

    }
    public void OnPoint4Changed(bool isOn)
    {
        if (isOn)
        {
            painSize =0.4f;

        }
    }
    #endregion

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //创建一个游戏对象命名并添加LineRenderer组件
            GameObject go = new GameObject();
            go.name = "LineRenderer";
            CurrentLine = go.AddComponent();
            go.transform.SetParent(this.transform);
            //给Line添加material设置Lin宽度大小和颜色
            CurrentLine.material = this.materia;
            CurrentLine.startWidth=painSize;
            CurrentLine.endWidth = painSize;
            CurrentLine.startColor = color;
            CurrentLine.endColor = color;
           
            positions.Clear();
            //设置Line转折的时候的圆滑度
            CurrentLine.numCornerVertices = 5;
            //设置Line起始点和结束的圆滑度
            CurrentLine.numCapVertices = 5;

            Vector3 position = GetMousePoint();//获取位置Position
            //把Position添加到List
            AddPosition(position);
            isMouseDown = true;
        }
        //如果一直按着鼠标
        if (isMouseDown)
        {
            //一直获取位置Position
            Vector3 position = GetMousePoint();
           // 如果当前位置和下一个位置大于0.2才添加位置
            if (Vector3.Distance(position,lastMousePosition)>0.2f)
            AddPosition(position);

        }
        if (Input.GetMouseButtonUp(0))
        {
            CurrentLine = null;
            positions.Clear();
            isMouseDown = false;
        }
    }
    //添加位置并画线
    void AddPosition(Vector3 position)
    { 
        //把画Line的位置想Z轴移动-1,就是不让Line和Plane重合
        position.z -= 0.1f;
        positions.Add(position);//添加位置
        //当前Line的总共需要描点的Count
        CurrentLine.positionCount = positions.Count;
        //把每次描点的位置添加到LinRenderer组建里
        CurrentLine.SetPositions(positions.ToArray());
        //下一个Lin位置设置为当前位置
        lastMousePosition = position;
    }
    //发射射线获取位置Position
    Vector3 GetMousePoint()
    {
        Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray,out hit))
        {
                return hit.point;
        }
        return Vector3.zero;
    }

}

最后把代码里面三个选择颜色和三个设置大小的方法对应绑上Toggle上的OnValueChanged上


image.png

这样我画图的小程序就完成了


使用LineRenderer实现画图程序_第4张图片
image.png

你可能感兴趣的:(使用LineRenderer实现画图程序)