zoukankan      html  css  js  c++  java
  • 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
    }
  • 相关阅读:
    C语言经典算法100例(二)
    Plus One @LeetCode
    hdu 4099 Revenge of Fibonacci 字典树+大数
    算法入门系列一--DP初步
    《当裸辞的程序猿遇上最冷季八》——第一次约女孩吃饭
    UVa 297
    手机NFC通信的安全车钥匙
    innosetup卸载软件后,删除定时任务schedule task
    VS2008调试程序时出现"XXX mutex not created."
    centOS7关闭防火墙的命令
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/unity-LineRenderer-paint.html
Copyright © 2011-2022 走看看