问题描述
我是Unity3d的新手。我正在尝试在Unity3d中开发着色游戏。到目前为止,我设法使用LineRenderer使用鼠标进行着色编码。我希望用户能够在空白页面精灵的范围内进行着色。如何在页面边框内遮盖Linerenderer。希望我清楚这个问题。由于我是Unity3d的新手,请详细回答。以下是我到目前为止用于着色的代码
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject drawing = Instantiate(drawingPrefab);
//drawing.transform.SetParent(page.transform,true);
lineRenderer = drawing.GetComponent<LineRenderer>();
lineRenderer.positionCount = 0;
}
if (Input.GetMouseButton(0))
{
FreeDraw();
}
}
void FreeDraw()
{
Debug.Log("Free Draw");
lineRenderer.startWidth = 0.2f;
lineRenderer.endWidth = 0.2f;
Vector3 mousePos = new Vector3(Input.mousePosition.x,Input.mousePosition.y,1f);
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1,Camera.main.ScreenToWorldPoint(mousePos));
lineRenderer.material.color = new Color(255,1);
}
解决方法
我知道2种非常简单的方法:
通过代码添加边界: 在更新功能中,您只需检查鼠标位置是否在特定的“正方形”内即可。
void Update()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
// I know... its shitty code
if(mousePosition.x > 3 || mousePosition.x < -3 || mousePosition.y > 3 || mousePosition.y < -3)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
GameObject drawing = Instantiate(drawingPrefab);
//drawing.transform.SetParent(page.transform,true);
lineRenderer = drawing.GetComponent<LineRenderer>();
lineRenderer.positionCount = 0;
}
if (Input.GetMouseButton(0))
{
FreeDraw();
}}
这样,只有当光标位于x上从-3到3和y上从-3到3的正方形时,播放器才会画一条线。
2。您可以创建第二个摄像头,该摄像头仅看到特定的图层(只有线性渲染器具有),而主凸轮则看不到该图层。然后,您可以将其设置为溢出,但可以根据需要缩放2n cam,这样玩家就只能在特定的正方形中看到线性渲染器。
在我看来,代码版本更容易,而且我没有精力解释第二种方法。
编辑:我没想到:例如,您可以仅在线性渲染器组件2下的层中进行订购。在Sprite渲染器中,纸精灵应小于2,纸周围的东西应大于2。仅当您对每件事都有自己的精灵(纸张和背景)时,此方法才有效