Unity使用LineRender断笔写字
作者:御雪妃舞 时间:2023-01-07 09:29:38
做多媒体项目时,经常会最后来个客户签名并保存之类的,签名保存之前的博客Unity3d截图方法合集有介绍过了,今天闲着把断笔写字的也贴出来吧,以前用leap motion时尝试用 leap motion演示中的食指写字,当时的写字其实只能一笔画,说白了其实就是个寿命无限长的拖尾,虽然效果不太好,但是很流畅,尝试过用leap motion断笔写字,但是效果不好,很容易误写,然后就产生了此方法,就是鼠标或者触摸屏写字了。
讲一下思路,就是不断的将鼠标的屏幕坐标转换成世界坐标,然后用LineRender持续画线,添加到队列中,这样做的好处是可持续撤销误写的笔画,知道全部撤销,重新写。
来来来,鄙人写字很丑,不许笑,先上图:
下面言归正传,这个做起来比较简单,一个脚本就能实现了
工程目录图如下:
只有一个脚本,一个材质,一个场景就可以了
场景中新建一个lineRender和write物体,write物体挂上DrawLine脚本。
下面重点来了,主要就是这个脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DrawLine : MonoBehaviour
{
//线段预制
[Tooltip("Line renderer used for the line drawing.")]
public LineRenderer linePrefab;
//线段相关保存和下标
private List<GameObject> linesDrawn = new List<GameObject>();
private LineRenderer currentLine;
private int lineVertexIndex = 2;
void Update()
{
//删除最近一笔
if (Input.GetKeyDown(KeyCode.U))
{
// U-key means Undo
DeleteLastLine();
}
if (currentLine == null &&
Input.GetMouseButton(0))
{
// 鼠标按下,开始画线
currentLine = Instantiate(linePrefab).GetComponent<LineRenderer>();
currentLine.name = "Line" + linesDrawn.Count;
currentLine.transform.parent = transform;
Vector3 cursorPos = Input.mousePosition;
cursorPos.z = 0f;
//将鼠标按下的屏幕坐标转换成世界坐标
Vector3 cursorSpacePos = Camera.main.ScreenToWorldPoint(cursorPos);
cursorSpacePos.z = 0f;
currentLine.SetPosition(0, cursorSpacePos);
currentLine.SetPosition(1, cursorSpacePos);
lineVertexIndex = 2;
linesDrawn.Add(currentLine.gameObject);
StartCoroutine(DrawLines());
}
if (currentLine != null &&
Input.GetMouseButtonUp(0))
{
// 鼠标左键抬起结束当前笔画
currentLine = null;
}
}
//撤销最后一笔
public void DeleteLastLine()
{
if (linesDrawn.Count > 0)
{
GameObject goLastLine = linesDrawn[linesDrawn.Count - 1];
linesDrawn.RemoveAt(linesDrawn.Count - 1);
Destroy(goLastLine);
}
}
//持续画线
IEnumerator DrawLines()
{
while (Input.GetMouseButton(0))
{
yield return new WaitForEndOfFrame();
if (currentLine != null)
{
lineVertexIndex++;
currentLine.SetVertexCount(lineVertexIndex);
Vector3 cursorPos = Input.mousePosition;
cursorPos.z = 0f;
Vector3 cursorSpacePos = Camera.main.ScreenToWorldPoint(cursorPos);
cursorSpacePos.z = 0f;
currentLine.SetPosition(lineVertexIndex - 1, cursorSpacePos);
}
}
}
}
挂上脚本,你运行就可以写字了,就这么简单,尝试一下。
谢谢支持!有问题或者代码优化建议欢迎评论。
工程下载
来源:https://blog.csdn.net/nanggong/article/details/52514352
标签:Unity,LineRender,写字
0
投稿
猜你喜欢
Java synchronized轻量级锁的核心原理详解
2022-10-13 23:28:33
mybatis注入Date日期值为null的解决方法
2021-12-08 01:49:17
DUCC配置平台实现一个动态化线程池示例代码
2023-11-28 12:07:39
使用JAXBContext 设置xml节点属性
2021-09-21 00:05:44
Mybatis 动态sql的编写与开启二级缓存
2023-12-12 11:59:08
springboot bean循环依赖实现以及源码分析
2022-06-05 11:50:15
计算字符串和文件MD5值的小例子
2023-12-10 20:31:19
AccessibilityService实现微信发红包功能
2023-06-23 03:51:53
带着新人看java虚拟机01(推荐)
2023-05-28 04:38:16
Java关键字finally_动力节点Java学院整理
2022-09-25 11:15:30
C#写入XML文档
2022-03-21 18:08:33
java String类常量池分析及"equals"和"==”区别详细介绍
2021-11-28 01:54:48
Android apk 插件启动内存释放问题
2022-05-16 07:26:39
C#可用于登录验证码的四位随机数生成方法
2021-08-01 00:31:01
Android仿微信语音对讲录音功能
2021-10-12 22:13:36
Android自定义View实现五子棋小游戏
2022-05-18 14:56:59
android panellistview 圆角实现代码
2022-09-03 17:34:19
springBoot整合redis使用案例详解
2022-07-02 10:51:33
JavaFx Tooltip悬浮提示使用及自定义代码详解
2023-05-11 15:06:05
Spring Cloud Alibaba Nacos Config配置中心实现
2022-08-02 00:43:05