Unity使用LineRender實(shí)現(xiàn)繪畫功能
本文實(shí)例為大家分享了Unity LineRender實(shí)現(xiàn)繪畫功能的具體代碼,供大家參考,具體內(nèi)容如下
老規(guī)矩,直接上代碼:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLine_ZH : MonoBehaviour
{
private GameObject _Clone;
private LineRenderer _Line;
int _Number;
//帶有LineRender物體
[Header("LineRender預(yù)制體")]
public GameObject _Target;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//實(shí)例化對象
_Clone = Instantiate(_Target, _Target.transform.position, Quaternion.identity);
//獲得該物體上的LineRender組件
_Line = _Clone.GetComponent<LineRenderer>();
//設(shè)置起始和結(jié)束的顏色
//_Line.SetColors(Color.red, Color.blue);
_Line.startColor = Color.red;
_Line.endColor = Color.blue;
//設(shè)置起始和結(jié)束的寬度
// _Line.SetWidth(0.2f, 0.1f);
_Line.startWidth = 0.2f; ;
_Line.endWidth = 0.1f;
//計(jì)數(shù)
_Number = 0;
}
if (Input.GetMouseButton(0))
{
//每一幀檢測,按下鼠標(biāo)的時(shí)間越長,計(jì)數(shù)越多
_Number++;
//設(shè)置頂點(diǎn)數(shù)
//_Line.SetVertexCount(_Number);
_Line.positionCount = _Number;
//設(shè)置頂點(diǎn)位置(頂點(diǎn)的索引,將鼠標(biāo)點(diǎn)擊的屏幕坐標(biāo)轉(zhuǎn)換為世界坐標(biāo))
_Line.SetPosition(_Number - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));
}
//清除繪畫
if (Input.GetMouseButtonDown(1))
{
GameObject[] _Draw = GameObject.FindGameObjectsWithTag("DarwLine");
for (int i = 0; i < _Draw.Length; i++)
{
Destroy(_Draw[i]);
}
}
}
}
注意添加 預(yù)制體 和 材質(zhì)球:
腳本要搭載:

預(yù)制體(可自定義):

預(yù)制體材質(zhì)(可自定義):

最終效果:

暫時(shí)先這樣吧
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用react render props實(shí)現(xiàn)倒計(jì)時(shí)的示例代碼
- 談?wù)凴eact中的Render Props模式
- 何時(shí)/使用 Vue3 render 函數(shù)的教程詳解
- Vue Render函數(shù)創(chuàng)建DOM節(jié)點(diǎn)代碼實(shí)例
- Vue自定義render統(tǒng)一項(xiàng)目組彈框功能
- vue渲染方式render和template的區(qū)別
- vue+render+jsx實(shí)現(xiàn)可編輯動態(tài)多級表頭table的實(shí)例代碼
- 通過實(shí)例了解Render Props回調(diào)地獄解決方案
相關(guān)文章
Unity 百度AI實(shí)現(xiàn)人像動漫化效果
這篇文章主要介紹了Unity如何接入百度AI接口, 運(yùn)用對抗生成網(wǎng)絡(luò)技術(shù),為用戶量身定制千人千面的二次元動漫形象,并支持通過參數(shù)設(shè)置,生成二次元動漫人像。感興趣的可以學(xué)習(xí)一下2022-01-01
同時(shí)兼容JS和C#的RSA加密解密算法詳解(對web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時(shí)兼容JS和C#的RSA加密解密算法,通過該算法可以對web提交的數(shù)據(jù)進(jìn)行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
c# 如何實(shí)現(xiàn)獲取二維數(shù)組的列數(shù)
這篇文章主要介紹了c# 實(shí)現(xiàn)獲取二維數(shù)組的列數(shù)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
C# 并發(fā)控制框架之單線程環(huán)境下實(shí)現(xiàn)每秒百萬級調(diào)度
本文介紹了一款專為工業(yè)自動化及機(jī)器視覺開發(fā)的C#并發(fā)流程控制框架,通過模仿Go語言并發(fā)模式設(shè)計(jì),支持高頻調(diào)度及復(fù)雜任務(wù)處理,已在多個項(xiàng)目中驗(yàn)證其穩(wěn)定性和可靠性2024-10-10
淺談C# StringBuilder內(nèi)存碎片對性能的影響
這篇文章主要介紹了淺談StringBuilder內(nèi)存碎片對性能的影響,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

