如何讓它更美麗?
Gizmos.DrawLine(new Vector2(_leftBorder, _topBorder), new Vector2(_rightBorder, _topBorder));
Gizmos.DrawLine(new Vector2(_leftBorder, _topBorder), new Vector2(_leftBorder, _bottomBorder));
Gizmos.DrawLine(new Vector2(_rightBorder, _bottomBorder), new Vector2(_leftBorder, _bottomBorder));
Gizmos.DrawLine(new Vector2(_rightBorder, _bottomBorder), new Vector2(_rightBorder, _topBorder));
uj5u.com熱心網友回復:
也許您可以將向量存盤在欄位中并使用有意義的方法名稱:
private Vector2 _topLeft = new Vector2(_leftBorder, _topBorder);
private Vector2 _topRight = new Vector2(_rightBorder, _topBorder);
private Vector2 _bottomLeft = new Vector2(_leftBorder, _bottomBorder);
private Vector2 _bottomRight = new Vector2(_rightBorder, _bottomBorder);
private void DrawRectangle()
{
Gizmos.DrawLine(_topLeft, _topRight);
Gizmos.DrawLine(_topLeft, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _topRight);
}
這至少更具可讀性和可重用性。
uj5u.com熱心網友回復:
首先要做的事情:我發現Tim 的答案對于您的用例來說絕對是最清晰、最直觀和可重用的實作。如果我自己實作它,我會聽從 Tim 的建議。
鑒于減少重復對您來說似乎很重要,我將提供一個可能的實作,您首先創建兩個List<Vector2>(一個用于“左手向量”,一個用于代碼示例中的“右手向量”),然后使用 double.ForEach()進行所有需要的呼叫Gizmos.DrawLine( ):
var baseVectors = new List<Vector2>
{
new Vector2(_leftBorder, _topBorder),
new Vector2(_rightBorder, _bottomBorder)
};
var orthogonalVectors = new List<Vector2>
{
new Vector2(_rightBorder, _topBorder),
new Vector2(_leftBorder, _bottomBorder)
};
baseVectors
.ForEach(baseVector => orthogonalVectors
.ForEach(orthVector => Gizmos.DrawLine(baseVector, orthVector)));
如果您決定實施這一點,我強烈建議您為您的串列找到更多描述性/正確的名稱。我想不出任何更好的建議(尤其是在我看來,這Vector2更像是一個點而不是一個向量)。
uj5u.com熱心網友回復:
我是這樣寫代碼的,不知道能不能做得更漂亮
public Vector2 _topLeft;
public Vector2 _topRight;
public Vector2 _bottomLeft;
public Vector2 _bottomRight;
void Awake()
{
_topLeft = new Vector2(_leftBorder, _topBorder);
_topRight = new Vector2(_rightBorder, _topBorder);
_bottomLeft = new Vector2(_leftBorder, _bottomBorder);
_bottomRight = new Vector2(_rightBorder, _bottomBorder);
}
public void DrawRectangle()
{
Gizmos.DrawLine(_topLeft, _topRight);
Gizmos.DrawLine(_topLeft, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _topRight);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/433598.html
