• 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
    }
  • 相关阅读:
    LeetCode: Binary Search Tree Iterator 解题报告
    LeetCode: Search Insert Position 解题报告
    LeetCode: Search a 2D Matrix 解题报告
    LeetCode: Distinct Subsequences 解题报告
    LeetCode: Pascal's Triangle II 解题报告
    LeetCode: Pascal's Triangle 解题报告
    LeetCode: Valid Palindrome 解题报告
    LeetCode: Palindrome 回文相关题目
    K8S使用入门-创建第一个容器
    Prometheus监控实战day2——监控主机和容器
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/unity-LineRenderer-paint.html
Copyright © 2020-2023  润新知