溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Unity使用LineRender斷筆寫字

發(fā)布時(shí)間:2020-09-01 17:29:23 來源:腳本之家 閱讀:235 作者:御雪妃舞 欄目:編程語言

做多媒體項(xiàng)目時(shí),經(jīng)常會(huì)最后來個(gè)客戶簽名并保存之類的,簽名保存之前的博客Unity3d截圖方法合集有介紹過了,今天閑著把斷筆寫字的也貼出來吧,以前用leap motion時(shí)嘗試用 leap motion演示中的食指寫字,當(dāng)時(shí)的寫字其實(shí)只能一筆畫,說白了其實(shí)就是個(gè)壽命無限長的拖尾,雖然效果不太好,但是很流暢,嘗試過用leap motion斷筆寫字,但是效果不好,很容易誤寫,然后就產(chǎn)生了此方法,就是鼠標(biāo)或者觸摸屏寫字了。

講一下思路,就是不斷的將鼠標(biāo)的屏幕坐標(biāo)轉(zhuǎn)換成世界坐標(biāo),然后用LineRender持續(xù)畫線,添加到隊(duì)列中,這樣做的好處是可持續(xù)撤銷誤寫的筆畫,知道全部撤銷,重新寫。

來來來,鄙人寫字很丑,不許笑,先上圖:

Unity使用LineRender斷筆寫字

Unity使用LineRender斷筆寫字

下面言歸正傳,這個(gè)做起來比較簡單,一個(gè)腳本就能實(shí)現(xiàn)了

工程目錄圖如下:

Unity使用LineRender斷筆寫字

只有一個(gè)腳本,一個(gè)材質(zhì),一個(gè)場景就可以了

場景中新建一個(gè)lineRender和write物體,write物體掛上DrawLine腳本。

下面重點(diǎn)來了,主要就是這個(gè)腳本:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class DrawLine : MonoBehaviour
{
  //線段預(yù)制
  [Tooltip("Line renderer used for the line drawing.")]
  public LineRenderer linePrefab;
 
  //線段相關(guān)保存和下標(biāo)
  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))
    {
      // 鼠標(biāo)按下,開始畫線
      currentLine = Instantiate(linePrefab).GetComponent<LineRenderer>();
      currentLine.name = "Line" + linesDrawn.Count;
      currentLine.transform.parent = transform;
 
      Vector3 cursorPos = Input.mousePosition;
      cursorPos.z = 0f;
 
      //將鼠標(biāo)按下的屏幕坐標(biāo)轉(zhuǎn)換成世界坐標(biāo)
      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))
    {
      // 鼠標(biāo)左鍵抬起結(jié)束當(dāng)前筆畫
      currentLine = null;
    }
  }
 
  //撤銷最后一筆
  public void DeleteLastLine()
  {
    if (linesDrawn.Count > 0)
    {
      GameObject goLastLine = linesDrawn[linesDrawn.Count - 1];
      linesDrawn.RemoveAt(linesDrawn.Count - 1);
      Destroy(goLastLine);
    }
  }
 
  //持續(xù)畫線
  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);
      }
    }
  }
}

掛上腳本,你運(yùn)行就可以寫字了,就這么簡單,嘗試一下。

謝謝支持!有問題或者代碼優(yōu)化建議歡迎評(píng)論。

工程下載

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI