Unity3D 使用LineRenderer自由画线

原理

  1. 一个LineRenderer是一次画线,需要使用对象池
  2. 一帧记录一个鼠标位置

代码

这是线绘制器的代码,依赖于笔者写过的一个简易对象池
传送门:>>对象池

Unity3D 使用LineRenderer自由画线_第1张图片

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

namespace Yueh0607.ClothOperations
{
    [RequireComponent(typeof(LineRenderer))]
    public class LineDrawer : MonoBehaviour
    {
        [SerializeField] string linePrefabLocation;

        List<LineRenderer> lines = new List<LineRenderer>();

        Action<Vector3> posCallBack = null;

        void Awake()
        {
            if(Camera.main.orthographic == false)
            {
                throw new System.Exception("Camera.main.orthographic must be true");
            }

            if (linePrefabLocation == string.Empty) throw new System.Exception("line Prefab is null");
            PoolManager.Instance.CreatePool("linePrefab",linePrefabLocation, 0);
            PoolManager.Instance.GetPool("linePrefab").OnCreate = (x) =>
            {
                x.SetActive(false) ;
            };

            PoolManager.Instance.GetPool("linePrefab").OnGet = (x) =>
            {
                x.SetActive(true);
            };

            PoolManager.Instance.GetPool("linePrefab").OnSet = (x) =>
            {
                x.SetActive(false);
            };
            PoolManager.Instance.GetPool("linePrefab").MinCount = 10;

        }

        private void Update()
        {
            if(Input.GetMouseButtonDown(0))
            {
                DrawBegin();
            }
            else if(Input.GetMouseButton(0))
            {
                Drawing();
            }
            else if(Input.GetMouseButtonUp(0))
            {
                DrawEnd();
            }
        }


        LineRenderer current;
        void DrawBegin()
        {
            current = PoolManager.Instance.GetComponentSync<LineRenderer>("linePrefab");
            current.positionCount = 0;
        }
        void Drawing()
        {
            current.positionCount++;
            Camera.main.ScreenToViewportPoint(Input.mousePosition);

            Vector3 pos = (Vector2)Camera.main.ScreenToWorldPoint(
                new Vector3(Input.mousePosition.x, Input.mousePosition.y,
                Mathf.Abs(Camera.main.nearClipPlane - current.transform.position.z))
                );
            current.SetPosition(current.positionCount - 1,pos );

            posCallBack?.Invoke(pos);
        }
        void DrawEnd()
        {
            // PoolManager.Instance.RecycleComponent("linePrefab", current);

            lines.Add(current);
            current = null;
        }


        public void SetDrawCallback(Action<Vector3> worldPostionCallback)
        {
            posCallBack = worldPostionCallback;
        }
        public void ClearCanvas()
        {
            foreach(var x in lines)
            {
                PoolManager.Instance.RecycleComponent<LineRenderer>("linePrefab", x);
            }
            lines.Clear();
        }

    }
}

你可能感兴趣的:(c#,Unity)