这篇文章主要介绍了Unity 通过LineRenderer绘制两点之间的直线操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
我就废话不多说了,大家还是直接看代码吧~
private LineRenderer line;
//画线
line = this.gameObject.AddComponent<LineRenderer>();
//只有设置了材质 setColor才有作用
line.material = new Material(Shader.Find("Particles/Additive"));
line.SetVertexCount(2);//设置两点
line.SetColors(Color.yellow, Color.red); //设置直线颜色
line.SetWidth(0.01f, 0.01f);//设置直线宽度
//设置指示线的起点和终点
line.SetPosition(0, initPosition);
line.SetPosition(1, newPosition);
绘制圆
下面是以物体position为圆心,半径为R,在xz平面上的画圆
public float R;//半径
public int N;//不要超过45
line.SetVertexCount(N+1);//这里要加1,达成闭合曲线
for (int i = 0; i < N + 1; i++)
{
float x = R * Mathf.Cos((360 / N * i) * Mathf.Deg2Rad) + transform.position.x; //确定x坐标
float z = R * Mathf.Sin((360 / N * i) * Mathf.Deg2Rad) + transform.position.z; //确定z坐标
line.SetPosition(i, new Vector3(x, transform.position.y, z));
}
补充:Unity 通过LineRenderer组件画线段
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineByMouse : MonoBehaviour {
//画线组件预制体
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;
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
沃梦达教程
本文标题为:Unity 通过LineRenderer绘制两点之间的直线操作
基础教程推荐
猜你喜欢
- C#类和结构详解 2023-05-30
- 一个读写csv文件的C#类 2022-11-06
- winform把Office转成PDF文件 2023-06-14
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22
- C# List实现行转列的通用方案 2022-11-02
- C# 调用WebService的方法 2023-03-09
- C#控制台实现飞行棋小游戏 2023-04-22
- C# windows语音识别与朗读实例 2023-04-27
- unity实现动态排行榜 2023-04-27