- Home /
EditorGuiLayout - Objectfield
Hello, I have an editorscript and I want to have a togglegroup with an objectfield in it.
First Question: Is it possible to just use a public variable in my normal script or do I HAVE to make an objectfield in the editorscript.
Second: How do I make an objectfield. I have seen the reference: http://docs.unity3d.com/Documentation/ScriptReference/EditorGUILayout.ObjectField.html
but it doesn't work. Now when i try it myself I get stuck at the GUIlayoutoptions[] parameter.
Anyone got an example?
var t = target as DayOrNight;
if(true) {
t.BlendValue = EditorGUILayout.FloatField("Blend Value", t.BlendValue);
}
t.DayColor = EditorGUILayout.ColorField("Day Color", t.DayColor);
t.NightColor = EditorGUILayout.ColorField("Night Color", t.NightColor);
groupEnabled = EditorGUILayout.BeginToggleGroup("Has Light", groupEnabled);
t.rotatingLight = EditorGUILayout.ObjectField("light source", t.rotatingLight, typeof(Light),false) as Light;
EditorGUILayout.EndToggleGroup();
if(GUI.changed) {
EditorUtility.SetDirty(t);
}
Answer by Demigiant · Nov 07, 2012 at 10:01 AM
First) I suppose you mean if you can use a public variable and have it automatically shown in the default Inspector. If so, the answer is yes. Actually, each time you have a public Transform that is shown in the Inspector, that is an ObjectField.
Second) let's say you have a serializable Transform you want to assign within a custom Inspector, you do it like this:
myTransform = EditorGUILayout.ObjectField("My Label", myTransform, typeof(Transform), false) as Transform;
The "false" parameter is the one that determines if you want users to be able to assign scene/hierarchy objects or only assets from the project panel.
Thank you, I have tried it and you can't use local variables for it. So I declared it at the top of my script. Now I get the error that it can't convert an Object to a Light (I want to have to attach a directional light). Is that possible?
I just tried it to be sure, and it works perfectly. Did you write something like this?
someLight = EditorGUILayout.ObjectField("Light", someLight, typeof(Light), true) as Light;
Ah, I forgot the "as Light". It now compiles, but I still cannot attach my Directional Light in the inspector. I get the sign that you usually get when types don't match, but it says "None (Light)".
Also, when I enabled my group toggle and switch to different inspector and then go back, my group toggle is disabled again. I'll update my question with the code I have.
Oops, my bad again. I set the AllowSceneObjects to false >.>
thanks! if you have an answer for my enable/disable problem, that'd be great:D
Your answer
Follow this Question
Related Questions
How can I get the full path to an object in an ObjectField? 3 Answers
EditorGUILayout.ObjectField cannot be changed. 1 Answer
How can I get a list of specific game objects in the object picker opened from an ObjectField()? 0 Answers
why I Cant Add Multiple Objects to the "Object Field" in Editor Window? 3 Answers
EditorGUILayout.ObjectField does not allow me to select a script. 1 Answer