可視範囲や距離のステートが変化するスフィアに反応する最も効率的な方法は、onStateChanged コールバックフィールドを使用することです。CullingGroupEvent 構造体を引数とする関数に onStateChanged を設定すると、カリングが完了した後、ステートが変わった各スフィアに対して onStateChanged が呼び出されます。CullingGroupEvent 構造体のメンバーによって、スフィアの新旧のステートがわかります。
group.onStateChanged = StateChangedMethod;
private void StateChangedMethod(CullingGroupEvent evt)
{
if(evt.hasBecomeVisible)
Debug.LogFormat("Sphere {0} has become visible!", evt.index);
if(evt.hasBecomeInvisible)
Debug.LogFormat("Sphere {0} has become invisible!", evt.index);
}
onStateChanged デリゲートに加え、CullingGroup には、境界球配列内のスフィアの最新の可視範囲と距離を求める API があります。スフィアのステートをチェックするには、IsVisible メソッドと GetDistance メソッドを使用します。
bool sphereIsVisible = group.IsVisible(0);
int sphereDistanceBand = group.GetDistance(0);
複数のスフィアのステートをチェックするには、QueryIndices メソッドを使用します。このメソッドは、スフィアの連続する範囲をスキャンして、与えられた可視範囲や距離ステートに該当するものを見つけます。
// Allocate an array to hold the resulting sphere indices - the size of the array determines the maximum spheres checked per call
int[] resultIndices = new int[1000];
// Also set up an int for storing the actual number of results that have been placed into the array
int numResults = 0;
// Find all spheres that are visible
numResults = group.QueryIndices(true, resultIndices, 0);
// Find all spheres that are in distance band 1
numResults = group.QueryIndices(1, resultIndices, 0);
// Find all spheres that are hidden in distance band 2, skipping the first 100
numResults = group.QueryIndices(false, 2, resultIndices, 100);
クエリ API で見つけられる情報は、CullingGroup に使用されるカメラが、実際にカリングするときにだけ更新されるということに注意してください。