- Home /
Change Point Light Values through Script
I am relatively new to scripting in Unity so this may be a question that requires more than I initially thought. I want to create an editor script that changes the values of a selected amount of point lights within a scene. For instance, I might want to change the intensity of 15 point lights from 1 to .35 without changing each individually.
I was going to use this bit of script as a jumping off point from the "How to Randomly Change the Intensity of a Point Light with a Script" thread, but I thought I would see if anyone else could point me in the right direction. http://answers.unity3d.com/questions/35577/how-to-randomly-change-the-intensity-of-a-point-light-with-a-script
EDIT : Maybe a more specific question is, how do I get a list of the point lights properties to display in the Editor Gui Layout and allow the user to change to the properties for all of the selected point lights? I have a simple Editor Script set up right now, but do not know how to draw a selected game objects properties. saltydog_nm 2 hours ago
Any help is appreciated!
Answer by noahmjohnson · Feb 17, 2011 at 05:35 PM
Here is the script I wrote after some poking around. Thanks for your help Dave.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[RequireComponent(typeof(Light))]
public class BatchLightProperties : EditorWindow {
int NumberofLights;
List<Object> lightObjects = new List<Object>();
Color lightColor = Color.white;
float lightIntensity = 1f;
float lightRange = 10f;
// Add menu named "Batch Light Properties" to the Window menu
[MenuItem ("Custom/Batch Light Properties")]
static void Init () {
// Get existing open window or if none, make a new one:
BatchLightProperties window = (BatchLightProperties)EditorWindow.GetWindow (typeof (BatchLightProperties));
}
void OnGUI () {
GUILayout.Label ("Light Properties", EditorStyles.boldLabel);
NumberofLights = EditorGUILayout.IntField("No. of Lights", NumberofLights);
for(int k = 0; k < NumberofLights; k++)
{
if(lightObjects.Count < NumberofLights)
{
lightObjects.Add(new Object());
}
}
for(int j = 0; j < lightObjects.Count; j++)
{
lightObjects[j] = EditorGUILayout.ObjectField("Light: ",lightObjects[j],typeof(Object));
}
EditorGUILayout.Separator();
EditorGUILayout.Separator();
lightRange = EditorGUILayout.FloatField("Light Range", lightRange);
lightColor = EditorGUILayout.ColorField("Light Color", lightColor);
lightIntensity = EditorGUILayout.Slider("Light Intensity", lightIntensity,0,8);
}
void Update ()
{
foreach(Object obj in lightObjects){
if(obj != null && obj.GetType() == typeof(GameObject))
{
GameObject lightGame = obj as GameObject;
if (obj != null)
{
lightGame.light.color = lightColor;
lightGame.light.range = lightRange;
lightGame.light.intensity = lightIntensity;
}
}
}
}
}
Answer by DaveA · Feb 14, 2011 at 05:26 PM
There are many scripts in the wiki that deal with altering multiple objects. The Asset Store has some nice things too. But if you want to write your own, take a look at this one to get some ideas:
http://www.unifycommunity.com/wiki/index.php?title=ReplaceSelection
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
EditorGUILayout - Use of unassigned local variable 2 Answers
Problem with OSX/Unity during long bake series (bug? workaround?) 0 Answers
Running a script when Unity starts. 3 Answers
How can i get the center of the entire editor window 0 Answers