Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作
更新時(shí)間:2021年04月12日 14:20:08 作者:鬼谷傳人
這篇文章主要介紹了Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,大家還是直接看代碼吧~
private LineRenderer line;
//畫線
line = this.gameObject.AddComponent<LineRenderer>();
//只有設(shè)置了材質(zhì) setColor才有作用
line.material = new Material(Shader.Find("Particles/Additive"));
line.SetVertexCount(2);//設(shè)置兩點(diǎn)
line.SetColors(Color.yellow, Color.red); //設(shè)置直線顏色
line.SetWidth(0.01f, 0.01f);//設(shè)置直線寬度
//設(shè)置指示線的起點(diǎn)和終點(diǎn)
line.SetPosition(0, initPosition);
line.SetPosition(1, newPosition);
繪制圓
下面是以物體position為圓心,半徑為R,在xz平面上的畫圓
public float R;//半徑
public int N;//不要超過45
line.SetVertexCount(N+1);//這里要加1,達(dá)成閉合曲線
for (int i = 0; i < N + 1; i++)
{
float x = R * Mathf.Cos((360 / N * i) * Mathf.Deg2Rad) + transform.position.x; //確定x坐標(biāo)
float z = R * Mathf.Sin((360 / N * i) * Mathf.Deg2Rad) + transform.position.z; //確定z坐標(biāo)
line.SetPosition(i, new Vector3(x, transform.position.y, z));
}
補(bǔ)充:Unity 通過LineRenderer組件畫線段

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineByMouse : MonoBehaviour {
//畫線組件預(yù)制體
public Transform gestureOnScreenPrefab;
private int strokeId = -1;
private Vector3 virtualKeyPosition = Vector2.zero;
private Rect drawArea;
private RuntimePlatform platform;
private int vertexCount = 0;
private List<LineRenderer> gestureLinesRenderer = new List<LineRenderer>();
private LineRenderer currentGestureLineRenderer;
//GUI
private string message="what is this ?";
private bool recognized;
private string newGestureName = "";
void Start()
{
platform = Application.platform;
drawArea = new Rect(0, 0, Screen.width - Screen.width / 3, Screen.height);
}
void Update()
{
if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount > 0)
{
virtualKeyPosition = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);
}
}
else
{
if (Input.GetMouseButton(0))
{
virtualKeyPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
}
}
if (drawArea.Contains(virtualKeyPosition))
{
if (Input.GetMouseButtonDown(0))
{
++strokeId;
// Transform tmpGesture = Instantiate(gestureOnScreenPrefab, transform.position, transform.rotation) as Transform;
// currentGestureLineRenderer = tmpGesture.GetComponent<LineRenderer>();
GameObject go = new GameObject("LineRenderer");
go.transform.position = Camera.main.transform.position;
go.transform.rotation = Camera.main.transform.rotation;
currentGestureLineRenderer = go.AddComponent<LineRenderer>();
currentGestureLineRenderer.startWidth = 0.1f;
gestureLinesRenderer.Add(currentGestureLineRenderer);
vertexCount = 0;
}
if (Input.GetMouseButton(0))
{
currentGestureLineRenderer.SetVertexCount(++vertexCount);
currentGestureLineRenderer.SetPosition(vertexCount - 1, Camera.main.ScreenToWorldPoint(new Vector3(virtualKeyPosition.x, virtualKeyPosition.y, 10)));
}
}
}
void OnGUI()
{
GUI.Box(drawArea, "Draw Area");
GUI.Label(new Rect(10, Screen.height - 40, 500, 50), message);
if (GUI.Button(new Rect(Screen.width - 100, 10, 100, 30), "clear line"))
{
foreach (LineRenderer lineRenderer in gestureLinesRenderer)
{
lineRenderer.SetVertexCount(0);
Destroy(lineRenderer.gameObject);
}
gestureLinesRenderer.Clear();
recognized = true;
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:
- Unity3D實(shí)現(xiàn)播放gif圖功能
- Unity 如何獲取鼠標(biāo)停留位置下的物體
- Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
- unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
- unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
- Unity3d 使用Gizmos畫一個(gè)圓圈
- unity 如何使用LineRenderer 動(dòng)態(tài)劃線
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- Unity解析gif動(dòng)態(tài)圖操作
相關(guān)文章
深入多線程之:解析線程的交會(huì)(Thread Rendezvous)詳解
本篇文章是對(duì)線程的交會(huì)(Thread Rendezvous)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# 進(jìn)行圖片壓縮的示例代碼(對(duì)jpg壓縮效果最好)
這篇文章主要介紹了C# 進(jìn)行圖片壓縮的示例代碼,幫助大家更好的利用c# 處理圖片,提高辦公效率,感興趣的朋友可以了解下2020-11-11
C#中比較常用的DateTime結(jié)構(gòu)的使用方法
這篇文章主要介紹了C#中比較常用的DateTime結(jié)構(gòu)的使用方法,需要的朋友可以參考下2015-11-11
C#使用String和StringBuilder運(yùn)行速度測(cè)試及各自常用方法簡(jiǎn)介
今天小編就為大家分享一篇關(guān)于C#使用String和StringBuilder運(yùn)行速度測(cè)試及各自常用方法簡(jiǎn)介,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10

