zero3growlithe

Unity 3D Script - Optimizer

Oct 3rd, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. // The Optimizer script is free to use in your projects. It allows you to have lots of uncombined objects
  5. // on the scene by hiding them when not visible or far from the camera in set distance.
  6. // It uses distance to current camera on scene.
  7.  
  8. // Variables:
  9. // [checkInterval] - how often is the distance to camera checked (the smaller, the more accurate but bigger performance impact)
  10. // [range] - in what distance should the object be disabled
  11. // [mode]:
  12. //  {CullOn} - if the object is behind the camera, hide it right away without checking the distance
  13. //  {CullOff} - the hide range is even in every direction
  14.  
  15. // Created by Damian Kościelny (zero3growlithe)
  16. // You are allowed to use this script in your commercial projects without necessarly putting me into credits
  17. // but would be great if you did ;)
  18.  
  19. public class Optimizer : MonoBehaviour {
  20.  
  21.     [HideInInspector]
  22.     public Transform cam;
  23.     [HideInInspector]
  24.     public Transform toHide;
  25.     [HideInInspector]
  26.     public bool init;
  27.  
  28.     public enum Modes {CullOn, CullOff}
  29.     public Modes mode;
  30.  
  31.     public float checkInterval = 0.2f;
  32.     public float range = 50f;
  33.     float timer;
  34.  
  35.     [HideInInspector]
  36.     public bool activeState;
  37.  
  38.     // Use this for initialization
  39.     void Start () {
  40.         if (init)
  41.             return;
  42.  
  43.         activeState = true;
  44.  
  45.         // cam = Camera.current.transform;
  46.         toHide = this.transform;
  47.  
  48.         GameObject temp = new GameObject(transform.name+"_optimized");
  49.         init = true;
  50.        
  51.         temp.transform.parent = transform.parent;
  52.         temp.transform.position = transform.position;
  53.         transform.parent = temp.transform;
  54.  
  55.         CopyComponent(this, temp);
  56.         Optimizer tempS = (Optimizer)temp.GetComponent<Optimizer>();
  57.         tempS.init = true;
  58.         Destroy(this);
  59.     }
  60.    
  61.     // Update is called once per frame
  62.     void LateUpdate () {
  63.         if (Time.time > timer){
  64.             if ((!cam && Camera.current != null) ||
  65.                 (Camera.current != null && cam != Camera.current.transform))
  66.                 cam = Camera.current.transform;
  67.             OptiCheck();
  68.         }
  69.     }
  70.  
  71.     void OptiCheck (){
  72.         if (!cam || !toHide || toHide == null)
  73.             return;
  74.         float dist = (cam.position-toHide.position).magnitude;
  75.         float side = 0;
  76.         if (mode == Modes.CullOn)
  77.             side = cam.InverseTransformPoint(toHide.position).z;
  78.         if (dist < range && side > -1){
  79.             if (!toHide.gameObject.activeSelf)
  80.                 DisableCheck(true);
  81.         } else if (toHide.gameObject.activeSelf)
  82.             DisableCheck(false);
  83.        
  84.         timer = Time.time + checkInterval;
  85.     }
  86.  
  87.     void DisableCheck (bool state){
  88.         if (activeState && toHide.gameObject.activeSelf && state == false){
  89.             toHide.gameObject.SetActive(false);
  90.             activeState = false;
  91.         }
  92.         if (!activeState && !toHide.gameObject.activeSelf && state == true){
  93.             toHide.gameObject.SetActive(true);
  94.             activeState = true;
  95.         }
  96.     }
  97.  
  98.     Component CopyComponent(Component original, GameObject destination){
  99.         System.Type type = original.GetType();
  100.         Component copy = destination.AddComponent(type);
  101.  
  102.         System.Reflection.FieldInfo[] fields = type.GetFields();
  103.  
  104.         foreach (System.Reflection.FieldInfo field in fields){
  105.             field.SetValue(copy, field.GetValue(original));
  106.         }
  107.         return copy;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment