Unity3d 使用Line Renderer动态画曲线

Unity LineRenderer画曲线


一、新建一个空物体

第一步:在场景中新建一个空物体,以便后面步骤挂载脚本。

二、编写脚本

代码如下(示例):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class test : MonoBehaviour
{
   
    //线条粗细
    private float paintSize = 0.1f;
    //用来存储鼠标位置
    private List<Vector3> mousePos = new List<Vector3>();
    //鼠标是否长按
    private bool isLongPressed;
    private LineRenderer lr;
    private Vector3 lastPos;
    //线条材质
    public Material material;
    //设置父容器
    public Transform fatherTrasform;



    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //动态添加LineRenderer组件
            GameObject line = new GameObject();
            line.transform.SetParent(transform);
            lr = line.AddComponent<LineRenderer>();
            line.transform.parent = fatherTrasform.transform;
            //初始化
            InitLineRender();
        }
        if (Input.GetMouseButtonUp(0))
        {
            isLongPressed = false;
            mousePos.Clear();
            lr = null;
        }
        if (isLongPressed)
        {
            //长按绘制且两点距离大于0.2才开始添加点
            if (Vector3.Distance(lastPos, GetMousePosition()) > =0.2f)
            {
                paintPos.Add(GetMousePosition());
                lastPos = GetMousePosition();
            }
            lr.positionCount = mousePos.Count;
            lr.SetPositions(mousePos.ToArray());
        }

       
    }
    void InitLineRender()
    {
        lr.material = material;//设置材质
        lr.endColor = paintColor;
        lr.startWidth = paintSize;//设置线的宽度
        lr.endWidth = paintSize;
        lr.numCapVertices = 2;//设置端点圆滑度
        lr.numCornerVertices = 2;//设置拐角圆滑度,顶点越多越圆滑
        lastPos = GetMousePosition();//获取鼠标在世界坐标中的位置
        mousePos.Add(lastPos);
        lr.positionCount = paintPos.Count;//设置构成线条的点的数量
        lr.SetPositions(paintPos.ToArray());
    }

    // 获取鼠标位置,将屏幕坐标转为世界坐标
    private Vector3 GetMousePosition()
    {
        Vector3 screenPosition;//将物体从世界坐标转换为屏幕坐标
        Vector3 mousePositionOnScreen;//获取到点击屏幕的屏幕坐标
        Vector3 mousePositionInWorld;//将点击屏幕的屏幕坐标转换为世界坐标
        //获取鼠标在相机中(世界中)的位置,转换为屏幕坐标;
       screenPosition = Camera.main.WorldToScreenPoint(transform.position);
        //获取鼠标在场景中坐标
        mousePositionOnScreen = Input.mousePosition;
        //让场景中的Z=鼠标坐标的Z
        mousePositionOnScreen.z = screenPosition.z;
        //将相机中的坐标转化为世界坐标
        mousePositionInWorld = Camera.main.ScreenToWorldPoint(mousePositionOnScreen);
        return mousePositionInWorld;

    }
    }



三、补充

这段代码可以获取Line Render所画线的平均中心位置

Vector3 pos= lr.bounds.center;
使用Line Renderer在物体上写字需要一些额外的代码与技巧。以下是一个简单的步骤: 1. 创建一个新的GameObject并将其命名为“TextRenderer”。 2. 将Line Renderer组件添加到该GameObject上。 3. 将该GameObject的位置设置为你想要在其上写字的物体的位置。 4. 创建一个新的C#脚本并将其命名为“TextDrawer”。 5. 在脚本中添加以下代码: ``` using UnityEngine; using System.Collections.Generic; public class TextDrawer : MonoBehaviour { public LineRenderer lineRenderer; public Font font; public string text; public float characterSpacing = 0.1f; public float lineWidth = 0.1f; void Start() { if (lineRenderer == null) lineRenderer = GetComponent<LineRenderer>(); if (font == null) font = Resources.GetBuiltinResource<Font>("Arial.ttf"); DrawText(); } void DrawText() { List<Vector3> positions = new List<Vector3>(); List<Vector2> uvs = new List<Vector2>(); CharacterInfo charInfo; float x = 0; int length = text.Length; for (int i = 0; i < length; ++i) { char c = text[i]; if (font.GetCharacterInfo(c, out charInfo, 100)) { positions.Add(new Vector3(x, charInfo.minY, 0)); positions.Add(new Vector3(x + charInfo.glyphWidth, charInfo.minY, 0)); positions.Add(new Vector3(x + charInfo.glyphWidth, charInfo.maxY, 0)); positions.Add(new Vector3(x, charInfo.maxY, 0)); uvs.Add(charInfo.uvBottomLeft); uvs.Add(new Vector2(charInfo.uvBottomRight.x, charInfo.uvBottomLeft.y)); uvs.Add(charInfo.uvTopRight); uvs.Add(new Vector2(charInfo.uvBottomLeft.x, charInfo.uvTopRight.y)); x += charInfo.advance + characterSpacing; } } lineRenderer.positionCount = positions.Count; lineRenderer.SetPositions(positions.ToArray()); lineRenderer.startWidth = lineWidth; lineRenderer.endWidth = lineWidth; lineRenderer.material = font.material; lineRenderer.material.mainTexture = font.material.mainTexture; lineRenderer.textureMode = LineTextureMode.Tile; lineRenderer.numCornerVertices = 5; lineRenderer.uv2 = uvs.ToArray(); } } ``` 6. 将TextRenderer游戏对象拖动到TextDrawer脚本的lineRenderer字段中。 7. 将要写字的文本字符串拖动到TextDrawer脚本的text字段中。 8. 可以更改characterSpacing和lineWidth字段来设置字符之间的间距和线条的宽度。 现在,你应该可以看到在你的物体上显示文本了!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值