using UnityEngine;
public class CullingGroupBehaviour : MonoBehaviour
{
private CullingGroup cullingGroup;
private BoundingSphere[] bounds;
Transform[] targets;
public Transform ReferencePoint;
void Start()
{
var gobjs = GameObject.FindGameObjectsWithTag("Sphere");
targets = new Transform[gobjs.Length];
for(int i = 0; i < gobjs.Length; i++)
{
targets[i] = gobjs[i].transform;
}
cullingGroup = new CullingGroup();
cullingGroup.targetCamera = Camera.main;
cullingGroup.SetDistanceReferencePoint(transform);
cullingGroup.SetBoundingDistances(new float[] { 25.0f });
bounds = new BoundingSphere[targets.Length];
for (int i = 0; i < bounds.Length; i++)
{
bounds[i].radius = 1.5f;
}
cullingGroup.SetBoundingSpheres(bounds);
cullingGroup.SetBoundingSphereCount(targets.Length);
cullingGroup.onStateChanged = OnChange;
}
void Update()
{
for (int i = 0; i < bounds.Length; i++)
{
bounds[i].position = targets[i].position;
}
}
void OnDestroy()
{
cullingGroup.Dispose();
cullingGroup = null;
}
void OnChange(CullingGroupEvent ev)
{
if (ev.currentDistance > 0)
{
targets[ev.index].gameObject.GetComponent<Renderer>().material.color = Color.green;
}
else
{
targets[ev.index].gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
}