void Awake() { myCamera = GetComponent <Camera>(); //keeps track of the camera component without needing to call GetComponent all the time //if we're not playing, and we're in the inspector, we've got some initialization to do if (!Application.isPlaying && Application.isEditor) { if (distanceMultiplier > 0) { defaultCullDistance = myCamera.farClipPlane / distanceMultiplier; } else { Debug.LogError("Distance multiplier is less than or equal to 0."); } oldDefaultCullDistance = defaultCullDistance; //sets up the mass editing of default culling planes //fills up layerCullInfos[] with LayerCullInfo objects and sets the layer names appropriately for (int index = 0; index < NUMBER_OF_UNITY_LAYERS; index++) { if (layerCullInfos[index] == null) { layerCullInfos[index] = new LayerCullInfo(); layerCullInfos[index].cullDistance = defaultCullDistance; } if (LayerMask.LayerToName(index) != "") { layerCullInfos[index].layerName = LayerMask.LayerToName(index); } else { layerCullInfos[index].layerName = "Layer Not Defined"; } } } }
/// <summary> /// Makes sure that all values are appropriate and valid. /// Called automatically in the editor, and called by the AssignUpdatedValues coroutine at runtime. /// </summary> void OnValidate() { if (!myCamera) { myCamera = GetComponent <Camera>(); } //makes sure there are always the right number of elements in the layerCullInfos array if (layerCullInfos.Length != NUMBER_OF_UNITY_LAYERS) { LayerCullInfo[] newLayerCullInfos = new LayerCullInfo[NUMBER_OF_UNITY_LAYERS]; //when the above isn't true, we need to reset the array, but we want to save as much information as possible, so we create a temporary array //store all salvageable data in the temporary array for (int index = 0; index < layerCullInfos.Length; index++) { newLayerCullInfos[index].cullDistance = layerCullInfos[index].cullDistance; } //if the array was resized to be smaller than the correct size, fill the remaining slots in the temp array with the default value for (int index = layerCullInfos.Length; index < NUMBER_OF_UNITY_LAYERS; index++) { newLayerCullInfos[index].cullDistance = defaultCullDistance; } layerCullInfos = newLayerCullInfos; //copy the temporary array into the regular array so that it now has the right number of elements again } //have to make sure that the default distance is not less than the near clip plane (since nothing should be culled closer than that) if (defaultCullDistance < myCamera.nearClipPlane) { defaultCullDistance = myCamera.nearClipPlane + 0.01f; //roughly the smallest distance the far clip plane can be } //makes sure the cull distance for each layer isn't smaller than the near clip plane foreach (LayerCullInfo cullInfo in layerCullInfos) { if (cullInfo.cullDistance < myCamera.nearClipPlane) { cullInfo.cullDistance = myCamera.nearClipPlane + 0.01f; } } //Makes sure the distance multiplier won't make any of the cull distances smaller than the near clip plane float nearestCullDistance = layerCullInfos[0].cullDistance; foreach (LayerCullInfo cullInfo in layerCullInfos) { if (cullInfo.cullDistance < nearestCullDistance) { nearestCullDistance = cullInfo.cullDistance; } } if (nearestCullDistance * distanceMultiplier < (myCamera.nearClipPlane + 0.01f)) { distanceMultiplier = (myCamera.nearClipPlane + 0.01f) / nearestCullDistance; } //Makes sure that all distances set to the old default are updated if the default has been changed. //Used to automatically update the array in any places that are just using the default foreach (LayerCullInfo cullInfo in layerCullInfos) { if (cullInfo.cullDistance == oldDefaultCullDistance) { cullInfo.cullDistance = defaultCullDistance; } } //searches the array for the furthest distance //culling distances cannot be set to a value larger than the far clip plane on the camera, so we increase the far clip plane to match float furthestCullDistance = layerCullInfos[0].cullDistance; foreach (LayerCullInfo cullInfo in layerCullInfos) { if (cullInfo.cullDistance > furthestCullDistance) { furthestCullDistance = cullInfo.cullDistance; } } myCamera.farClipPlane = furthestCullDistance * distanceMultiplier; oldDefaultCullDistance = defaultCullDistance; //stores the current default for comparison's sake if a change is made }