美文网首页unityUnity3D 成神之路
使用LineRenderer实现画图程序

使用LineRenderer实现画图程序

作者: _凉笙 | 来源:发表于2017-11-17 19:23 被阅读30次

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


image.png

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


image.png 下面我们来看看怎么写一个画图小程序,首先看看我的布局,首先是三种颜色的选择和三种画笔大小的选择,然后一个背景
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<Vector3> positions = new List<Vector3>();
    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<LineRenderer>();
            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

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


image.png

相关文章

  • 使用LineRenderer实现画图程序

    首先我们来看下LineRenderer的一些参数属性 Color里面的颜色想要设置成功需要把材质的Shader设置...

  • 2019-01-18 3D Metal Printing

    unity 使用LineRenderer实现飘带飞舞的效果 2018年02月11日 12:08:36刘峰1011阅...

  • <转载>Unity3D中LineRenderer的使

    原文地址: Unity3D中LineRenderer的使用 LineRenderer线渲染器主要是用于在3D中渲染...

  • Android OpenGL入门

    GLSurfaceView GLSurfaceView是Android应用程序中实现OpenGl画图的重要组成部分...

  • canvas

    在canvas上画图 所有在 中的画图必须使用js实现 第一步:创建一个Canvas绘图上下文 首先,我们需要创建...

  • Quart2D 画图一 (简单画线、形状)

    自我使用学习画图的一些用法,可以由简单开始,实现复杂画图操作 画线 画形状 注意: 这些方法一定要在视图的 dra...

  • Unity之LineRenderer组件

    LineRenderer线渲染器主要是用于在3D中渲染线段,虽然我们也可以使用GL图像库来渲染线段,但是使用Lin...

  • 2021-01-05

    1、画简单的图形可以使用windows的画图。 2、全世界最著名的计算机网络是因特网。 3、画图工具一般在程序组中...

  • Unity中,画出任意形状的带有物理碰撞的线

    示意 思路 滑屏时持续生成LineRenderer的点,每隔一小段时间,生成相应的2D矩形碰撞体。 具体实现 Br...

  • LineRenderer初探

    如图给角色绘制游戏中可见的视野范围:

网友评论

    本文标题:使用LineRenderer实现画图程序

    本文链接:https://www.haomeiwen.com/subject/dazjvxtx.html