本文介绍了如何为线条设置正确的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Unity开发第一款游戏.我正在尝试在我的游戏场上画线.代码:
I'm working on my first game in Unity.I'm trying to draw lines on my game field.Code:
private void DrawLine(Vector3 start, Vector3 stop, GameObject template)
{
GameObject toInstiateGridLine = template;
GameObject gridLineInstance = Instantiate(toInstiateGridLine, start, Quaternion.identity) as GameObject;
LineRenderer gridLineRenderer = gridLineInstance.GetComponent<LineRenderer>();
gridLineRenderer.SetVertexCount(2);
gridLineRenderer.SetWidth(0.01f, 0.01f);
gridLineRenderer.SetColors(Color.black, Color.black);
gridLineRenderer.SetPosition(0, start);
gridLineRenderer.SetPosition(1, stop);
}
它可以解决一个问题.我得到的是粉红色线条,而不是我期望的黑色.
It works with one problem. I get pink lines instead of black that I expect.
在运行时中创建的LineRenderer组件的设置:
Settings of LineRenderer component that has been created in runtime:
推荐答案
您缺少材料.缺少材料时,粉红色是标准颜色.
You are missing material. Pink is the standard color when material is missing.
LineRenderer gridLineRenderer = gridLineInstance.GetComponent<LineRenderer>();
Material mat = new Material(Shader.Find("Unlit/Texture"));
gridLineRenderer.material = mat;
或者,您可以直接更改材料颜色.我认为,直接调用将导致创建标准默认材料
Or, you can change material color directly. As I consider, calling directly will cause to create standard default material
gridLineRenderer.material.color = Color.white;
这篇关于如何为线条设置正确的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!