- Home /
Quick Custom Inspector Question
Hi, all! I was wondering how the syntax would go if I wanted to make a field that let me edit 2 variables on the same line that say "Min" before the first field and "Max" before the second field, allowing me to edit 2 variables.
Exactly like the way a Vector3Field exposes 3 fields in one line that say 'X', 'Y', and 'Z' before them, I'd like to make one that says 'min' and 'max' with only two fields.
Can this be done? Thanks!- YA
Answer by Seth-Bergman · Nov 14, 2012 at 01:48 AM
you can create a custom class, then declare an instance of your class:
class Range{
var min : float;
var max : float;
}
var myRange : Range = new Range();
(javascript)
EDIT: add this script to an object and select the object. Now in the inspector, you can see the new var "myRange":
Now, to access the values of myRange via script, you could say myRange.min or myRange.max.. for example:
myRange.min = 2.5;
SECOND EDIT:
Here is what you mean:
// Create a Horizontal set of two float fields
class testEditor extends EditorWindow {
@MenuItem("Examples/testEditor")
static function Init() {
var window = GetWindow(testEditor);
window.Show();
}
function OnGUI() {
var r : Rect = EditorGUILayout.BeginHorizontal ();
EditorGUILayout.FloatField (1.0);
EditorGUILayout.FloatField (2.0);
EditorGUILayout.EndHorizontal ();
}
}
Hope this helps :)
FINAL EDIT:
played around a bit more, here's how to add the labels...
// Create a Horizontal set of float fields
class testEditor extends EditorWindow {
@MenuItem("Examples/testEditor")
static function Init() {
var window = GetWindow(testEditor);
window.Show();
}
function OnGUI() {
var r : Rect = EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField("min",GUILayout.Width(30));
EditorGUILayout.FloatField (1.0);
EditorGUILayout.LabelField("max",GUILayout.Width(30));
EditorGUILayout.FloatField (2.0);
EditorGUILayout.EndHorizontal ();
}
}
Ah. I thought it would be something along those lines. And how would I declare this as a nice custom inspector float field? Would it just be a simple float field?
just add the above script to a gameobject in your project, and the var "myRange" will become visible
Oh I know but it would be great if I could be neater about this. It's all right if you don't have the syntax for it.
?? what are you asking? I guess I'm misunderstanding when you say:
"Exactly like the way a Vector3Field exposes 3 fields in one line that say 'X', 'Y', and 'Z' "
I assumed you meant in the inspector.. Do you want to ASSIGN both values with one line of code, in a script? If so, you'll want to create a CONSTRUCTOR for your new class.. Is that what you mean?
Oh I see that I have been incredibly unclear! No I know how classes and their constructors operate. I am opting for a neater solution (this code has a lot of user-set variables). I mean in an accompanying Editor Script. When I said a Vector 3 example I actually meant, say, the position variables in a Transform component. What would the syntax be to do this for any object of the Range class except with two fields?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Block Placement Help 2 Answers
Simple Code Not Working 4 Answers
" ';' expected. Insert a semicolon at the end." Error Help 1 Answer