Unity LineRenderer制作画版

Posted 观海云不远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity LineRenderer制作画版相关的知识,希望对你有一定的参考价值。

Source:

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

public class AtwalPaint : MonoBehaviour {
    public Material currentMaterial;

    private Color paintColor = Color.red;
    private float paintSize = 0.1f;
    private bool isPress = false;
    private LineRenderer lineRenderer;
    private List<Vector3> positions = new List<Vector3>();
    private int lineCount = 0;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isPress = true;
            GameObject go = new GameObject("LineRenderer_" + lineCount);
            go.transform.parent = gameObject.transform;
            lineRenderer = go.AddComponent<LineRenderer>();
            lineRenderer.startColor = paintColor;
            lineRenderer.endColor = paintColor;
            lineRenderer.startWidth = paintSize;
            lineRenderer.endWidth = paintSize;
            lineRenderer.material = currentMaterial;
            lineRenderer.numCapVertices = 5;
            lineRenderer.numCornerVertices = 5;
            lineCount++;
            AddPosition();
        }

        if (isPress)
        {
            AddPosition();
        }

        if (Input.GetMouseButtonUp(0))
        {
            lineRenderer = null;
            positions.Clear();
            isPress = false;
        }
    }

    void AddPosition()
    {
        Vector3 position = GetMousePoint();
        if (positions.Count > 0)
        {
            if (Vector3.Distance(position, positions[positions.Count - 1]) < 0.1f)
            {
                return;
            }
        }
        position.z = -0.02f * lineCount;
        positions.Add(position);
        lineRenderer.positionCount = positions.Count;
        lineRenderer.SetPositions(positions.ToArray());
    }

    Vector3 GetMousePoint()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray, out hit);
        if (isCollider)
        {
            return hit.point;
        }
        return Vector3.zero;
    }


    #region color
    public void OnRedColorChange(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.red;
        }
    }
    public void OnGreenColorChange(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.green;
        }
    }
    public void OnBlueColorChange(bool isOn)
    {
        if (isOn)
        {
            paintColor = Color.blue;
        }
    }
    #endregion

    #region size
    public void On1SizeChange(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.1f;
        }
    }
    public void On2SizeChange(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.2f;
        }
    }
    public void On4SizeChange(bool isOn)
    {
        if (isOn)
        {
            paintSize = 0.4f;
        }
    }
    #endregion
}

 

以上是关于Unity LineRenderer制作画版的主要内容,如果未能解决你的问题,请参考以下文章

游戏开发解答教你在Unity中使用LineRenderer制作行军蚂蚁线(行军 | 虚线 | 路径 | 线段)

Unity制作红色射线

游戏开发实战Unity实现类似GitHub地球射线的效果(LineRenderer | 贝塞尔曲线)

游戏开发实战Unity实现类似GitHub地球射线的效果(LineRenderer | 贝塞尔曲线)

游戏开发实战Unity实现类似GitHub地球射线的效果(LineRenderer | 贝塞尔曲线)

Unity之LineRenderer组件