Unity使用LineRender斷筆寫字
做多媒體項目時,經常會最后來個客戶簽名并保存之類的,簽名保存之前的博客Unity3d截圖方法合集有介紹過了,今天閑著把斷筆寫字的也貼出來吧,以前用leap motion時嘗試用 leap motion演示中的食指寫字,當時的寫字其實只能一筆畫,說白了其實就是個壽命無限長的拖尾,雖然效果不太好,但是很流暢,嘗試過用leap motion斷筆寫字,但是效果不好,很容易誤寫,然后就產生了此方法,就是鼠標或者觸摸屏寫字了。
講一下思路,就是不斷的將鼠標的屏幕坐標轉換成世界坐標,然后用LineRender持續(xù)畫線,添加到隊列中,這樣做的好處是可持續(xù)撤銷誤寫的筆畫,知道全部撤銷,重新寫。
來來來,鄙人寫字很丑,不許笑,先上圖:
下面言歸正傳,這個做起來比較簡單,一個腳本就能實現(xiàn)了
工程目錄圖如下:
只有一個腳本,一個材質,一個場景就可以了
場景中新建一個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);
}
}
//持續(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ōu)化建議歡迎評論。
工程下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:C#教程
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4719.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10c# ArrayList的使用方法小總結
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載


