【发布时间】:2015-12-02 19:56:15
【问题描述】:
我想在统一的 2DTexture 上画一条线。这在编辑器中运行良好。但是,在编译的版本中,它会闪烁,因为它会用清晰的颜色填充纹理的像素,然后重新绘制线条。
这是我的绘图代码:
public void DrawLineWithMouse (Vector2 _middleVector)
{
tex.SetPixels(fillPixels); // Fills texture with an array of clear pixels
Vector3 newMousePos = Input.mousePosition;
newMousePos.z = 0;
int x0 = ((int)newMousePos.x)/RenderingManager.pixelSize / thickness; //Screen.width-
int y0 = ((int)newMousePos.y)/RenderingManager.pixelSize / thickness; //Screen.height-
int x1 = (int)_middleVector.x - (x0 - (int)_middleVector.x );
int y1 = (int)_middleVector.y - (y0 - (int)_middleVector.y );
DrawLineHelper(tex, x0, y0, x1, y1, Color.yellow); // redraws the line
tex.Apply();
}
当我创建新纹理然后应用它时,我不会闪烁。但是,这非常、非常昂贵,并且仅用于测试目的。
public void DrawLineWithMouse (Vector2 _middleVector)
{
tex = new Texture2D(Screen.width/RenderingManager.pixelSize / thickness, Screen.height/RenderingManager.pixelSize / thickness);
tex.SetPixels(fillPixels);
tex.SetPixels(fillPixels);
tex.filterMode = FilterMode.Point;
Vector3 newMousePos = Input.mousePosition;
newMousePos.z = 0;
int x0 = ((int)newMousePos.x)/RenderingManager.pixelSize / thickness; //Screen.width-
int y0 = ((int)newMousePos.y)/RenderingManager.pixelSize / thickness; //Screen.height-
int x1 = (int)_middleVector.x - (x0 - (int)_middleVector.x );
int y1 = (int)_middleVector.y - (y0 - (int)_middleVector.y );
DrawLineHelper(tex, x0, y0, x1, y1, Color.yellow);
GetComponent<Renderer>().material.mainTexture = tex;
【问题讨论】:
-
很好奇你是怎么打电话给
DrawLineWithMouse()的。您是在Update还是FixedUpdate中调用它? -
if (Input.GetMouseButton(0)){drawLineUtility.DrawLineWithMouse(startMousePointInPixels);}在更新中