Unity Custom Editor: 1 True Bool Only Within A Group
Hi,
In custom inspector what's the best way to deal with setting only 1 bool to true and everything else to false? So in other words, i only want 1 bool to be able to be true and if that 1 bool is true then all the other bools are set to false.
i recall seeing something like being able to group our bools together in the custom inspector and easily performing this function but i can no longer find it. The other alternative would be something like using an if statement and within the if setting everything else to false but i feel like there's a more elegant way of doing it for a custom inspector.
Any help will be appreciated!
Answer by dan_wipf · Mar 26, 2019 at 09:27 AM
hm if you have a list of bools where only one bool can be true at a time, why don’t you use an int switch?
int casehandler = 0;
switch(casehandler){
case 0:
//do something
break;
case 1:
//do something
break;
case 2:
//do something
break;
//and so on
}
but to answer your question with this you can call a bool in an array to set true/false(default true), and the rest to the opposite of what you choose. boolhandler(boolarray,index of which bool you want to change, true/false);
bool[] boolhandler(bool[] b,int index, bool setTo = true){
for(int i = 0; i<b.Length;i++){
if(i == index){
b[i] = setTo;
}
else{
b[i] = !setTo;
}
}
return b;
}
//example
bool[] myboolarray = referencearray;
myboolarray = boolhandler(myboolarray, 2, true);
Your answer
Follow this Question
Related Questions
No type in ObjectField created in uxml (UIElements) 3 Answers
Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers
Objects in Custom Editor Resetting on Play 1 Answer
Unity 2019 LTS can't find scripts in "Editor" Folder. 0 Answers
CustomEditor stops displaying after changing script location 1 Answer