Hello every one, I was searching around and could not find the exact answer I was looking for, I was wondering if anyone knew of a way to create a list or just outputting all objects currently seen (rendered) by the camera, without resorting to putting scripts on all the objects, functions like OnWillRenderObject() just appear to output the single object they area attached to, anyone know of a good way to do this?
You could get All mesh renderers and check their isVisable
Renderer’s have a “isVisible” property, although I haven’t had much luck with it. Searched Unity Answers and found a solution rather quickly, thanks to aldonaletto from this [question][1]. You can check if a Renderer is within the bounds of a camera’s frustum.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VisibleRenderers : MonoBehaviour {
public List<Renderer> visibleRenderers = new List<Renderer>();
// Update is called once per frame
void Update () {
FindObjects();
}
// Find and store visible renderers to a list
void FindObjects ()
{
// Retrieve all renderers in scene
Renderer[] sceneRenderers = FindObjectsOfType<Renderer>();
// Store only visible renderers
visibleRenderers.Clear();
for (int i = 0; i < sceneRenderers.Length; i++)
if (IsVisible(sceneRenderers*))*
visibleRenderers.Add(sceneRenderers*);*
// debug console
string result = "Total Renderers = " + sceneRenderers.Length + ". Visible Renderers = " + visibleRenderers.Count;
foreach (Renderer renderer in visibleRenderers)
result += "
" + renderer.name;
Debug.Log(result);
}
// Is the renderer within the camera frustrum?
bool IsVisible(Renderer renderer) {
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
return (GeometryUtility.TestPlanesAABB(planes, renderer.bounds)) ? true : false;
}
}
Checking each frame with FindObjectsOfType is probably bad practice, so depending on your requirements, this might be something that can be improved on.
_*[1]: http://answers.unity3d.com/questions/560057/detect-if-entity-is-visible-rendererisvisible-will.html*_