Advertisement
Guest User

CullChildObjects

a guest
Apr 11th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. /// <summary>
  7. /// This culls all the objects which are child objects of the current object, that have a mesh renderer.
  8. /// This is much more efficient than attaching a cullObject to each individual object.
  9. /// </summary>
  10. public class CullChildObjects : MonoBehaviour
  11. {
  12.     private MeshRenderer[] meshes;
  13.     private Camera playerCamera;
  14.    
  15.     private bool outOfDistanceLastFrame;
  16.    
  17.     void Start ()
  18.     {
  19.         // attempt to get the camera controlled by the player
  20.         playerCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
  21.         if (playerCamera == null)
  22.             print ("The main camera was not found inside CulledObject script.");
  23.        
  24.         meshes = GetComponentsInChildren<MeshRenderer> ();
  25.        
  26.         // attempt to get the meshRenderer component on the child objects
  27.         if (meshes == null || meshes.Length == 0) {
  28.             print (tag);
  29.             print ("CullChildObjects require a mesh component on child objects, in order to enable and disable the objects.");
  30.         }
  31.     }
  32.    
  33.     void Update ()
  34.     {
  35.         // calculate the absolute distance between the camera and this object
  36.         var distance = Math.Abs (Vector3.Distance (transform.position, playerCamera.transform.position));
  37.        
  38.         // if the distance between the objects is above the render distance
  39.         if (distance > CulledObject.RenderDistance && !outOfDistanceLastFrame)
  40.             OnObjectOutOfDistance ();
  41.         else if (distance < CulledObject.RenderDistance && outOfDistanceLastFrame) // if we're in distance and we were not last frame
  42.             OnObjectInDistance ();
  43.     }
  44.    
  45.     /// <summary>
  46.     /// Called when the object is out of the distance of the camera - and it was in distance last frame.
  47.     /// </summary>
  48.     void OnObjectOutOfDistance ()
  49.     {
  50.         outOfDistanceLastFrame = true;
  51.        
  52.         for (int i = 0; i < meshes.Length; i++)
  53.             if (meshes [i] != null && meshes [i].enabled)
  54.                 meshes [i].enabled = false;
  55.     }
  56.    
  57.     /// <summary>
  58.     /// Called when the object is in distance of the camera - and it was not in distance last frame.
  59.     /// </summary>
  60.     void OnObjectInDistance ()
  61.     {
  62.         outOfDistanceLastFrame = false;
  63.        
  64.         for (int i = 0; i < meshes.Length; i++)
  65.             if (meshes [i] != null && !meshes [i].enabled)
  66.                 meshes [i].enabled = true;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement