- Home /
How can I make one variable affect others in custom inspector?
For example, I have a custom inspector like this
public override void OnInspectorGUI()
{
a= EditorGUILayout.FloatField("A", a);
lock =EditorGUILayout.Toggle("Lock",lock);
b= EditorGUILayout.FloatField("B", b);
}
How do I make adjusting a
also change b
and vice versa when lock
is true?
Answer by Fornoreason1000 · Aug 20, 2016 at 01:54 AM
Hello, maybe this will help
As you should know OnInspectorGUI
runs fairly often, lets say every frame or so. then you have two floats A and B? and a bool called lock. you require A & B to be adjusted at a given ratio when _lock true.
lock
is a C# Keyword so you will need to rename it, _lock
will do.
a= EditorGUILayout.FloatField("A", a);
assisngs A to the return value of EditorGUILAyout.FloatFeild()
This is the same with B. so basically all you need to do is check if lock is true then modify the return value of A so B remains the same. To know how much they adjust by just make some temporary variable that store the old values of A and B to calculate how we should modify them
public override void OnInspectorGUI()
{
//Store unmodified values of A and B
float old_a = a;
float old_b = b;
//Modify them using Editor Functions
a= EditorGUILayout.FloatField("A", a);
lock =EditorGUILayout.Toggle("Lock",lock);
b= EditorGUILayout.FloatField("B", b);
// Check if _lock is true
if(_lock) {
//if A was adjusted
if(old_a != a) {
//modify B based on A
b = (old_b / old_a) * a
}
//if B was adjusted but not A
else if(old_b != b) {
//Modify A based on B
a = (old_a / old_b) * b;
}
}
}
This code firstly assigns two new variables , old_a
and old_b
. which are assigned to the original counterparts. then your original code runs, changing the values of a
and b
. Once this is done, an If Statement run if _lock
is true, if it is true. two more If statements compare the values of a,b and old_a,old_b. if a or B has changed, we calculate a new value for the unchanged one.
Hope it Helps
DISCLAIMER! the formulas used to modify A and B are ones i made on the spot. they will probably produce weird results.
Sources:
Answer by OsmiousH · Aug 20, 2016 at 11:02 AM
try this
public override void OnInspectorGUI(){
if(lock){
a=b;
b=a;
}
}
this will change a to b and b to a when lock is true
Nice, its alot simpler than my answer, however theres is a bug i can see. If you adjust a, with lock true it wont work becuade it gets reassigned back to b. Effectively breaking the ability to adjust any value at all.
A=B and B=A shouldn't really be called on the same block. You need it to respond only when one of the values changes, then adjust the other value after.
Your answer
Follow this Question
Related Questions
Custom Inspector (Editor) - override just one field control 1 Answer
How to access one class instance in editor script? 1 Answer
How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers
Get default preview for GameObject Editor 2 Answers
How to record hideFlags for Undo/Redo 0 Answers