When running this with the Unity profiler, a script "ShootableLights.js" was taking up 20% of the CPU time, specifically in the Update() method. I've been able to reduce this to "0.0%" with a quick change that doesn't seem to have a visual impact on the game.
I shifted the two noisy loops from the end of Update to both Start() and WasShot(). These loops seem to be for updating the lighting contributions of the destroyable lights on their surroundings. The impact of this code change is that the lights only recalculate their contributions when theyre made or destroyed. If these lights are animated in any way while alive, my code alteration will cause problems.
I hope this is useful.
-P
The altered code:
Code: Select all
#pragma strict
var destroy_effect : GameObject;
var light_color = Color(1,1,1);
var destroyed = false;
enum LightType {AIRPLANE_BLINK, NORMAL, FLICKER}
public var light_type = LightType.NORMAL;
private var blink_delay = 0.0;
private var light_amount = 1.0;
function WasShot(obj : GameObject, pos : Vector3, vel : Vector3) {
if(!destroyed){
destroyed = true;
light_amount = 0.0;
Instantiate(destroy_effect, transform.FindChild("bulb").position, Quaternion.identity);
}
if(obj && obj.collider && obj.collider.material.name == "glass (Instance)"){
GameObject.Destroy(obj);
}
var combined_color = Color(light_color.r * light_amount,light_color.g * light_amount,light_color.b * light_amount);
for(var light : Light in gameObject.GetComponentsInChildren(Light)){
light.color = combined_color;
}
for(var renderer : MeshRenderer in gameObject.GetComponentsInChildren(MeshRenderer)){
renderer.material.SetColor("_Illum", combined_color);
if(renderer.gameObject.name == "shade"){
renderer.material.SetColor("_Illum", combined_color * 0.5);
}
}
}
function Start () {
var combined_color = Color(light_color.r * light_amount,light_color.g * light_amount,light_color.b * light_amount);
for(var light : Light in gameObject.GetComponentsInChildren(Light)){
light.color = combined_color;
}
for(var renderer : MeshRenderer in gameObject.GetComponentsInChildren(MeshRenderer)){
renderer.material.SetColor("_Illum", combined_color);
if(renderer.gameObject.name == "shade"){
renderer.material.SetColor("_Illum", combined_color * 0.5);
}
}
}
function Update () {
if(!destroyed){
switch(light_type){
case LightType.AIRPLANE_BLINK:
if(blink_delay <= 0.0){
blink_delay = 1.0;
if(light_amount == 1.0){
light_amount = 0.0;
} else {
light_amount = 1.0;
}
}
blink_delay -= Time.deltaTime;
break;
}
}
}