码迷,mamicode.com
首页 > 编程语言 > 详细

Unity LineRenderer制作画版

时间:2017-12-13 17:28:20      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:cas   gpo   collect   end   pre   new   nts   parent   start   

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制作画版

标签:cas   gpo   collect   end   pre   new   nts   parent   start   

原文地址:http://www.cnblogs.com/lurenjiashuo/p/unity-LineRenderer-paint.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!