この投稿では、UnityEngine.BoundsInt構造体の公式ドキュメントだけでは把握しにくい仕様・挙動を、実際のコード例を交えて説明します。
コード例・挙動は、Unity 6.3に準じます。
コンストラクタと内部のデータ保持
公式ドキュメントには掲載されていませんが、パラメーターなしのコンストラクター以外にも、次に示す2つのコンストラクターが定義されています。
// positionとsizeを引数にとる
var bounds0 = new BoundsInt(
position: new Vector3Int(0, 0, 0),
size: new Vector3Int(2, 3, 1)
);
// 6個のintを引数にとる
var bounds1 = new BoundsInt(
xMin: 0,
yMin: 0,
zMin: 0,
sizeX: 2,
sizeY: 3,
sizeZ: 1
);
BoundsInt構造体は、内部では次の2つのフィールドを持ちます。
- 起点となる位置をつかさどるVector3Int型の「m_Position」
- サイズをつかさどるVector3Int型の「m_Size」
positionプロパティー・sizeプロパティーは、このフィールドの値を取得・更新します。
なお、sizeは負の値も取れることに注意が必要です。
min系・max系の各種プロパティー
BoundsInt構造体は、次の各種ゲッタープロパティー・セッタープロパティーを持ちます。
これらのゲッタープロパティーが返す値は、内部のm_Positionフィールドとm_Sizeフィールドから計算される値です。
m_Sizeプロパティーが負の値をとりえるため、各種ゲッタープロパティーも、それを反映した値となることに、注意してください。
例えば、sizeの要素がすべて正の数ならば、minはpositionと等しくなります。
var bounds = new BoundsInt(
position: new Vector3Int(1, 1, 0),
size: new Vector3Int(2, 3, 1)
);
Assert.AreEqual(
actual: bounds.min,
expected: new Vector3Int(1, 1, 0) // minとpositionが等しい
);
Assert.AreEqual(
actual: bounds.max,
expected: new Vector3Int(3, 4, 1)
);
しかしsizeの要素が負の数ならば、minとpositionは一致しません。
var bounds = new BoundsInt(
position: new Vector3Int(1, 1, 0),
size: new Vector3Int(-2, 3, 1)
);
Assert.AreEqual(
actual: bounds.min,
expected: new Vector3Int(-1, 1, 0) // minとpositionは一致しない
);
Assert.AreEqual(
actual: bounds.max,
expected: new Vector3Int(1, 4, 1)
);
なおゲッターだけでなく、セッターもあります。
allPositionsWithinプロパティー
BoundsInt内の含まれるすべてのポジション(Vector3Int型)を列挙するためのプロパティーです。IEnumerator<Vector3Int>, IEnumerator, IDisposableを実装したBoundsInt.PositionEnumerator型を返します。
var bounds = new BoundsInt(
position: new Vector3Int(0, 0, 0),
size: new Vector3Int(2, 3, 1)
);
var expected = new List<Vector3Int>
{
new(0, 0, 0),
new(1, 0, 0),
new(0, 1, 0),
new(1, 1, 0),
new(0, 2, 0),
new(1, 2, 0),
};
var counter = 0;
foreach (var position in bounds.allPositionsWithin)
{
Assert.AreEqual(
actual: position,
expected: expected[counter++]
);
}
BoundsInt.allPositionsWithinで列挙される要素の中に、minプロパティーのポジションは必ず含まれます。一方で、maxプロパティーのポジションは列挙されない点に注意してください。
centerプロパティー
centerプロパティーは、
- 起点となる位置をつかさどるVector3Int型の「m_Position」
- サイズをつかさどるVector3Int型の「m_Size」
から導出した、BoundsIntの真ん中の位置をつかさどるゲッタープロパティーです。セッタープロパティーはありません。
なお、Vector3Int型ではなく、Vector3型です。
var bounds = new BoundsInt(
position: new Vector3Int(0, 0, 0),
size: new Vector3Int(2, 3, 1)
);
Assert.AreEqual(
actual: bounds.center,
expected: new Vector3(
x: 1.0F,
y: 1.5F,
z: 0.5F
)
);
Containsメソッド
Containsメソッドは、引数に渡したVector3Int型のポジションが、そのBoundsIntに含まれるかどうかをbool型で返すメソッドです。
- x成分が
xMin以上xMax未満 - y成分が
yMin以上yMax未満 - z成分が
zMin以上zMax未満
をすべて満たすときtrueになり、そうでないならばfalseとなります。
つまりminプロパティーのポジションを引数に渡すとtrueを返しますが、maxプロパティーのポジションを引数に渡すとfalseを返します。
var bounds = new BoundsInt(
position: new Vector3Int(0, 0, 0),
size: new Vector3Int(2, 3, 1)
);
Assert.IsTrue(bounds.Contains(new Vector3Int(0, 0, 0)));
Assert.IsTrue(bounds.Contains(new Vector3Int(1, 2, 0)));
Assert.IsFalse(bounds.Contains(new Vector3Int(2, 2, 0))); // x == xMax なので含まれない
Assert.IsFalse(bounds.Contains(new Vector3Int(1, 3, 0))); // y == yMax なので含まれない
Assert.IsFalse(bounds.Contains(new Vector3Int(1, 2, 1))); // z == zMax なので含まれない
ClampToBoundsメソッド
ClampToBoundsメソッドは、引数として渡したBoundsIntの範囲内に収まるように、BoundsIntのpositionとsizeを変更するメソッドです。
var boundsA = new BoundsInt(
position: new Vector3Int(0, 0, 0),
size: new Vector3Int(3, 6, 1)
);
var boundsB = new BoundsInt(
position: new Vector3Int(2, 1, 0),
size: new Vector3Int(4, 3, 1)
);
boundsA.ClampToBounds(boundsB);
Assert.AreEqual(
actual: boundsA.min,
expected: new Vector3Int(2, 1, 0)
);
Assert.AreEqual(
actual: boundsA.max,
expected: new Vector3Int(5, 4, 1)
);
Assert.AreEqual(
actual: boundsA.size,
expected: new Vector3Int(3, 3, 1)
);
SetMinMaxメソッド
SetMinMaxメソッドは、BoundsIntのminとmaxをまとめて設定するためのメソッドです。
公式ドキュメントには、個別に設定するよりも高速という記載があります。