Culling Groups - Some Trick to "onStateChanged"?
I've been at it for hours now and cannot seem to get onStateChanged to happen except for during initialization within either Start() or OnEnable().
Is there some trick to getting that to work, or maybe I just don't understand how it should? I'm sure I'm just missing a piece of the puzzle. I've gone as far as to putting "group.onStateChanged = OnStateChanged;" into Update() but even that didn't do anything.
public static CullingGroupManager Instance;
public float radius = 1;
public CullingGroup group;
public BoundingSphere[] spheres;
public RegisterInCullingGroup[] objects;
public int count;
void Awake()
{
Instance = this;
group = new CullingGroup();
group.targetCamera = Camera.main;
objects = FindObjectsOfType<RegisterInCullingGroup>();
spheres = new BoundingSphere[objects.Length];
count = objects.Length;
for (int i = 0; i < count; i++)
{
spheres[i] = new BoundingSphere(objects[i].transform.position, radius);
}
group.SetBoundingSpheres(spheres);
group.SetBoundingSphereCount(count);
group.SetBoundingDistances(new float[] { 10f, 50f });
group.onStateChanged = OnStateChanged;
}
// Do whatever tasks I need when the Culling Group state changes
private void OnStateChanged(CullingGroupEvent _event)
{
if (_event.hasBecomeVisible)
{
Debug.LogFormat("Sphere {0} has become visible!", _event.index);
objects[_event.index].gameObject.SetActive(true);
}
else if (_event.hasBecomeInvisible)
{
Debug.LogFormat("Sphere {0} has become invisible!", _event.index);
objects[_event.index].gameObject.SetActive(false);
}
}
void OnDestroy()
{
group.Dispose();
group = null;
}
I'm still struggling with this problem days later and could really use some help.
If anyone has code they're willing to share that uses "CullingGroup.onStateChanged" that actually works in 2019.4.x I'd be grateful. At this point I'm not convinced that onStateChanged works. I've tried at least a dozen supposedly-functional code samples found online and none of them receive data from onStateChanged more than once.