Unity使用LineRender断笔写字-创新互联

做多媒体项目时,经常会最后来个客户签名并保存之类的,签名保存之前的博客Unity3d截图方法合集有介绍过了,今天闲着把断笔写字的也贴出来吧,以前用leap motion时尝试用 leap motion演示中的食指写字,当时的写字其实只能一笔画,说白了其实就是个寿命无限长的拖尾,虽然效果不太好,但是很流畅,尝试过用leap motion断笔写字,但是效果不好,很容易误写,然后就产生了此方法,就是鼠标或者触摸屏写字了。

成都创新互联公司是专业的连云港网站建设公司,连云港接单;提供成都网站制作、成都网站设计、外贸营销网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行连云港网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

讲一下思路,就是不断的将鼠标的屏幕坐标转换成世界坐标,然后用LineRender持续画线,添加到队列中,这样做的好处是可持续撤销误写的笔画,知道全部撤销,重新写。

来来来,鄙人写字很丑,不许笑,先上图:

Unity使用LineRender断笔写字

Unity使用LineRender断笔写字

下面言归正传,这个做起来比较简单,一个脚本就能实现了

工程目录图如下:

Unity使用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 linesDrawn = new List();
  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();
      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);
      }
    }
  }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:Unity使用LineRender断笔写字-创新互联
本文地址:http://lszwz.com/article/ccohsp.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款