I have a script that changes the lighting in a level based on the player’s Y position (it will get darker as the player goes down, and lighter as the player goes up). However, I notice that my frame rate drops from 70FPS to 30-40FPS in a certain area while this script is playing. I’m not sure if it is the script or something else in the game that is causing this frame drop.
What I want to know is the cost of accessing RenderSettings. If it costs too much to access it directly, I’d like to know alternative methods for changing the lighting in real time. My current lighting setup is as follows:
-1 Directional Light (intensity values are adjusted)
-Adjusting Ambient Light from RenderSettings
-Adjusting Fog values from RenderSettings (black fog is used)
Please keep in mind that I don’t have UnityPro.
Here’s the code for the script:
using UnityEngine;
using System.Collections;
public class DimLightDepth : MonoBehaviour {
public Light theLight;
public Transform playerTransform;
//When to dim or brighten lights
public float topHeight = 40f;
public float bottomHeight = 10f;
//Top and bottom colors for ambient lighting
public Color topColor = Color.grey;
public Color bottomColor = Color.black;
//The point where we know whether
//to lighten or darken the ambient color
public float ambientSwitchPoint = 20f;
//Minimum and maximum amounts of fog
public float maxFog = 0.13f;
public float minFog = 0f;
public Color fogTopColor = Color.black;
public Color fogBottomColor = Color.black;
//Maximum intensity of directional light
public float maxLightIntensity;
//Speed of the transition
public float speed = 0.2f;
//If effect is enabled or not
public bool enableFX;
// Use this for initialization
void Start () {
playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
theLight = light;
maxLightIntensity = theLight.intensity;
enableFX = false;
float playerHeight = playerTransform.position.y;
float fogAmount = (1f - (playerHeight / (topHeight - bottomHeight))) * maxFog;
SetFog(fogBottomColor, Mathf.Clamp (fogAmount, minFog, maxFog));
//Initializing the lighting
if(playerHeight > ambientSwitchPoint){
RenderSettings.ambientLight = topColor;
}
else{
RenderSettings.ambientLight = bottomColor;
}
}
void FixedUpdate () {
float playerHeight = playerTransform.position.y;
//Check if we should change lighting
if (playerHeight >= topHeight || playerHeight <= bottomHeight){
enableFX = false;
}
else {
enableFX = true;
}
//Change lighting if applicable
if(enableFX){
float ratio = (playerHeight - bottomHeight) / (topHeight - bottomHeight);
float fogAmount = maxFog * ( 1f - ratio);
SetFog(fogBottomColor, Mathf.Clamp (fogAmount, minFog, maxFog));
if(playerHeight > ambientSwitchPoint){
SetAmbientLight (topColor);
}
else{
SetAmbientLight (bottomColor);
}
SetLightIntensity (Mathf.Clamp (maxLightIntensity * ratio, 0f, maxLightIntensity));
}
}
//Sets the fog
void SetFog(Color targetColor, float targetAmount){
Color currentColor = RenderSettings.fogColor;
float currentFogAmt = RenderSettings.fogDensity;
if (currentColor != targetColor){
RenderSettings.fogColor = Color.Lerp(currentColor, targetColor, speed);
}
if(currentFogAmt != targetAmount){
RenderSettings.fogDensity = Mathf.Lerp (currentFogAmt, targetAmount, speed);
}
}
void SetAmbientLight(Color targetColor){
Color currentColor = RenderSettings.ambientLight;
if(currentColor != targetColor){
RenderSettings.ambientLight = Color.Lerp (currentColor, targetColor, speed / 2f);
}
}
void SetLightIntensity(float targetAmount){
float currentIntensity = theLight.intensity;
if (currentIntensity != targetAmount){
theLight.intensity = Mathf.Lerp (currentIntensity, targetAmount, speed * 2f);
}
}
}