void Awake() { // NOTE: Awake is called even when the script itself is disabled, so we manually have to check // if the script is disabled or not. if (!this.enabled) { return; } // NOTE: This makes sure the LODGroup system resets properly when you load another scene if (masterExists) { masterExists = false; instanceMaster = null; camTrans = null; groupCount = 0; } if (transform.childCount > 0) { // NOTE: At the end of the Awake() function, we give every instance of the script an instance ID instanceID = groupCount++; } else { Debug.LogError("Sys_LODGroups Error: LOD Group has no children!", gameObject); this.enabled = false; } }
void Start() { // NOTE: Because of the way the preceding Awake() is stuctured, any object still running this script // HAS to have at least 1 child object. Ineligible objects have already been filtered out. bool master = !masterExists; // This block creates the LODGroup itself and fills in some of the data. It could be made into a neat // LODGroup constructor call of course, but I think this is superfluous because construction of this // object will only ever take place here. LODGroup thisGroup = new LODGroup(); thisGroup.id = instanceID; thisGroup.mainObject = gameObject; thisGroup.origin = transform.position; thisGroup.updateByCount = updateByCount; thisGroup.disableRendererOnly = disableRendererOnly; thisGroup.excludedObjects = excludedObjects; if (master) // NOTE: This is going to be the master object { masterExists = true; instanceMaster = this; // NOTE: The reason I'm caching this transform is because Camera.main is actually // a search type function - VERY expensive. camTrans = Camera.main.transform; lodGroups = new LODGroup[groupCount]; #if UNITY_EDITOR transform.name = "!LODGroup Master"; transform.SetAsFirstSibling(); #endif } #if UNITY_EDITOR else if (instanceID > groupCount) { Debug.LogError("Sys_LODGroups Error: (ID " + lodGroups.Length + ") can't be created because the array (Length " + lodGroups.Length + ") is not big enough!", gameObject); return; } #endif // NOTE: Grab Arrays GrabArrays(thisGroup); // NOTE: Do initial cull check thisGroup.culled = (Vector3.Distance(camTrans.position, transform.position) < cullingDistance); if (thisGroup.culled) { Cull(thisGroup); } // NOTE: Write to the array and kill all slaves if (!master) { instanceMaster.lodGroups[instanceID] = thisGroup; //print ("Slave (ID " + ticket + ") registered and terminated succesfully!"); Destroy(this); } else { lodGroups[instanceID] = thisGroup; } }