Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- // The Optimizer script is free to use in your projects. It allows you to have lots of uncombined objects
- // on the scene by hiding them when not visible or far from the camera in set distance.
- // It uses distance to current camera on scene.
- // Variables:
- // [checkInterval] - how often is the distance to camera checked (the smaller, the more accurate but bigger performance impact)
- // [range] - in what distance should the object be disabled
- // [mode]:
- // {CullOn} - if the object is behind the camera, hide it right away without checking the distance
- // {CullOff} - the hide range is even in every direction
- // Created by Damian Kościelny (zero3growlithe)
- // You are allowed to use this script in your commercial projects without necessarly putting me into credits
- // but would be great if you did ;)
- public class Optimizer : MonoBehaviour {
- [HideInInspector]
- public Transform cam;
- [HideInInspector]
- public Transform toHide;
- [HideInInspector]
- public bool init;
- public enum Modes {CullOn, CullOff}
- public Modes mode;
- public float checkInterval = 0.2f;
- public float range = 50f;
- float timer;
- [HideInInspector]
- public bool activeState;
- // Use this for initialization
- void Start () {
- if (init)
- return;
- activeState = true;
- // cam = Camera.current.transform;
- toHide = this.transform;
- GameObject temp = new GameObject(transform.name+"_optimized");
- init = true;
- temp.transform.parent = transform.parent;
- temp.transform.position = transform.position;
- transform.parent = temp.transform;
- CopyComponent(this, temp);
- Optimizer tempS = (Optimizer)temp.GetComponent<Optimizer>();
- tempS.init = true;
- Destroy(this);
- }
- // Update is called once per frame
- void LateUpdate () {
- if (Time.time > timer){
- if ((!cam && Camera.current != null) ||
- (Camera.current != null && cam != Camera.current.transform))
- cam = Camera.current.transform;
- OptiCheck();
- }
- }
- void OptiCheck (){
- if (!cam || !toHide || toHide == null)
- return;
- float dist = (cam.position-toHide.position).magnitude;
- float side = 0;
- if (mode == Modes.CullOn)
- side = cam.InverseTransformPoint(toHide.position).z;
- if (dist < range && side > -1){
- if (!toHide.gameObject.activeSelf)
- DisableCheck(true);
- } else if (toHide.gameObject.activeSelf)
- DisableCheck(false);
- timer = Time.time + checkInterval;
- }
- void DisableCheck (bool state){
- if (activeState && toHide.gameObject.activeSelf && state == false){
- toHide.gameObject.SetActive(false);
- activeState = false;
- }
- if (!activeState && !toHide.gameObject.activeSelf && state == true){
- toHide.gameObject.SetActive(true);
- activeState = true;
- }
- }
- Component CopyComponent(Component original, GameObject destination){
- System.Type type = original.GetType();
- Component copy = destination.AddComponent(type);
- System.Reflection.FieldInfo[] fields = type.GetFields();
- foreach (System.Reflection.FieldInfo field in fields){
- field.SetValue(copy, field.GetValue(original));
- }
- return copy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment