Main Bug:
I am drawing a custom mesh using VertexHelper, but the drawing only appears in the Game View and not in the Scene View.
Code:
csharp
コードをコピーする
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
[ExecuteAlways]
public class UILineRenderer : Graphic
{
[System.Serializable]
public class LineData
{
public Vector2 startPoint;
public Vector2 endPoint;
}
public List<LineData> lines = new List<LineData>();
public float thickness = 5f;
public Color lineColor = Color.white;
public Color two = Color.blue;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
// Draw each line
foreach (var line in lines)
{
DrawLine(vh, line);
}
}
void DrawLine(VertexHelper vh, LineData line)
{
Vector2 direction = (line.endPoint - line.startPoint).normalized;
Vector2 normal = new Vector2(-direction.y, direction.x) * (thickness * 0.5f);
Vector2[] outerVertices = new Vector2[2];
Vector2[] innerVertices = new Vector2[2];
outerVertices[0] = line.startPoint + normal;
outerVertices[1] = line.endPoint + normal;
innerVertices[0] = line.startPoint - normal;
innerVertices[1] = line.endPoint - normal;
// Add vertices as a quad
AddQuad(vh, outerVertices[0], innerVertices[0], innerVertices[1], outerVertices[1], new Color[] { lineColor, lineColor, two, two });
}
void AddQuad(VertexHelper vh, Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4, Color[] color)
{
int idx = vh.currentVertCount;
vh.AddVert(v1, color[0], Vector2.zero);
vh.AddVert(v2, color[1], Vector2.zero);
vh.AddVert(v3, color[2], Vector2.zero);
vh.AddVert(v4, color[3], Vector2.zero);
vh.AddTriangle(idx, idx + 1, idx + 2);
vh.AddTriangle(idx + 2, idx + 3, idx);
}
}
Regarding the Situation:
When I attach this script to a prefab and instantiate the prefab, it is rendered in both the Scene View and the Game View.
However, when I attach it directly to a GameObject and render it, it only appears in the Game View and not at all in the Scene View.
It’s acceptable if it works only in the Game View, but if anyone has resolved a similar issue, I would appreciate your guidance.