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
}

 

posted @ 2017-12-13 14:47  观海云不远  阅读(825)  评论(0编辑  收藏  举报