[英]How to draw line smooth in unity (c# base coding problem)
我想從 unityUI 畫線。 (我不想統一使用 Line Renderer)。
所以我想出了下面的編碼,但我的問題是線條大小不是恆定的。
public class MyUILineRenderer : Graphic
{
public Vector2[] pointPos;
public float[] angles;
public MyUIGridRenderer gridRenderer;
public Vector2Int gridSize = new Vector2Int(1, 1);
public float lineThickness = 0.5f;
public float width;
public float height;
public float unitWidth;
public float unitHeight;
public float angleHelp;
public bool calAngle;
public Color[] colors;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
width = rectTransform.rect.width;
height = rectTransform.rect.height;
unitWidth = width / (float)gridSize.x;
unitHeight = height / (float)gridSize.y;
if (calAngle)
{
angles = new float[pointPos.Length];
}
if (pointPos.Length < 2)
{
return;
}
float angle = 0;
for (int i = 0; i < pointPos.Length; i++)
{
Vector2 point = pointPos[i];
if (i < pointPos.Length - 1)
{
if (calAngle)
{
angle = angles[i] = -GetAngle(pointPos[i], pointPos[i + 1]) + angleHelp;
}
else
{
angle = angles[i];
}
}
DrawLine(vh, point, angle);
}
int count = pointPos.Length * 2 - 2;
for (int i = 0; i < count; i += 2)
{
vh.AddTriangle(i + 0, i + 1, i + 3);
vh.AddTriangle(i + 0, i + 2, i + 3);
}
}
public float GetAngle(Vector2 currentPos, Vector2 targetPos) {
return (float)(Mathf.Atan2(targetPos.y - currentPos.y, targetPos.x - currentPos.x) * (180 / Mathf.PI));
}
private void DrawLine(VertexHelper vh, Vector2 point, float angle)
{
UIVertex vertex = UIVertex.simpleVert;
vertex.color = colors[0];
vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(-lineThickness, 0);
vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
vh.AddVert(vertex);
vertex.color = colors[1];
vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(lineThickness, 0);
vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
vh.AddVert(vertex);
}
}
當我像這樣團結起來
正如你們所看到的,線條粗細不是恆定的。
我怎么能像下圖那樣畫線?
如果它有用,請從文檔中找到這個改編的腳本來畫線。
using UnityEngine;
public class Example : MonoBehaviour
{
// Draws a line from "startVertex" var to the curent mouse position.
public Material mat;
Vector3 startVertex;
Vector3 mousePos;
void Start()
{
startVertex = Vector3.zero;
}
void Update()
{
mousePos = Input.mousePosition;
// Press space to update startVertex
if (Input.GetKeyDown(KeyCode.Space))
{
startVertex = new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0);
}
}
void OnPostRender()
{
if (!mat)
{
Debug.LogError("Please Assign a material on the inspector");
return;
}
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(Color.red);
//GL.Vertex(startVertex);
//GL.Vertex(new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0));
GL.Vertex(new Vector3(0, 0, 0));
GL.Vertex(new Vector3(0.3f, 0.3f, 0f));
GL.Vertex(new Vector3(0.3f, 0.3f, 0f));
GL.Vertex(new Vector3(0.3f, 0.5f, 0f));
GL.Vertex(new Vector3(0.3f, 0.5f, 0f));
GL.Vertex(new Vector3(0.7f, 0.5f, 0f));
GL.End();
GL.PopMatrix();
}
}
檢查您需要如何在屏幕坐標中插入每對點。 請注意,Unity 在 MonoBehaviours 上調用 OnPostRender,這些行為與啟用的 Camera 組件附加到相同的 GameObject 上,因此您需要附加此腳本才能使其工作。
找到紅線的截圖:
如果您可能需要從世界空間中的場景中繪制點,您可以獲得The Size of the Frustum at a Given Distance from the Camera以將您需要的點轉換到屏幕空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.