Debug.DrawLine没有显示在GameView中 [英] Debug.DrawLine not showing in the GameView
问题描述
我正在使用2D Unity应用程序,遇到一些奇怪的行为.
I'm working on a 2D Unity app and I'm encountering some weird behavior.
此代码可以正常工作.
This code works just fine.
Debug.DrawLine(button1.transform.position, button2.transform.position, Color.green);
运行该应用程序时,我在场景"视图中看到一条绿线.
When I run the app, I see a green line in the Scene view.
但是当我有以下一行时,游戏"视图中什么也没有显示.
But nothing appears in the Game view when I have the following line.
Physics2D.Linecast(button1.transform.position, button2.transform.position);
我对Unity如何在这两个按钮之间划一条线感到困惑,但是由于某种原因,它只是在游戏视图中没有做到这一点.
I'm confused as to how Unity is able to draw a line between these two buttons, but for some reason, it's just not doing it in the Game view.
有什么主意我要解决这个问题吗?
Any idea how I'd troubleshoot this?
推荐答案
Just line Serlite said, Physics2D.Linecast
is not used to draw a line but to detect if there is an object in the middle of two objects with raycast. It has nothing to do with drawing lines.
您已经知道,除非启用了Gizmos,否则Debug.DrawLine
仅在场景"视图中起作用.您可以 LineRenderer
或
As you already know, Debug.DrawLine
will only work in the Scene view unless Gizmos is enabled. You can just LineRenderer
or the GL
functions to draw lines which will work without enabling Gizmos and will also work in a build.
这里是在游戏和场景视图中绘制线条的帮助程序类.
Here is a helper class for drawing line in the Game and Scene view.
public struct LineDrawer
{
private LineRenderer lineRenderer;
private float lineSize;
public LineDrawer(float lineSize = 0.2f)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));
this.lineSize = lineSize;
}
private void init(float lineSize = 0.2f)
{
if (lineRenderer == null)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));
this.lineSize = lineSize;
}
}
//Draws lines through the provided vertices
public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
{
if (lineRenderer == null)
{
init(0.2f);
}
//Set color
lineRenderer.startColor = color;
lineRenderer.endColor = color;
//Set width
lineRenderer.startWidth = lineSize;
lineRenderer.endWidth = lineSize;
//Set line count which is 2
lineRenderer.positionCount = 2;
//Set the postion of both two lines
lineRenderer.SetPosition(0, start);
lineRenderer.SetPosition(1, end);
}
public void Destroy()
{
if (lineRenderer != null)
{
UnityEngine.Object.Destroy(lineRenderer.gameObject);
}
}
}
用法:
LineDrawer lineDrawer;
void Start()
{
lineDrawer = new LineDrawer();
}
void Update()
{
lineDrawer.DrawLineInGameView(Vector3.zero, new Vector3(0, 40, 0f), Color.blue);
}
完成后,您可以致电lineDrawer.Destroy();
.
这篇关于Debug.DrawLine没有显示在GameView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!