Unity3D 使用LineRenderer自由画线

原理

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

代码

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

请添加图片描述

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();
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUE ZHEN PENG

码字不易,如果你想请我喝杯果汁

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值