[英]How do I trace the source of the call “OnValidate()”
I want a script to have radio buttons for booleans and it seems like OnValidate()
would be the perfect way to do that.我想要一个脚本有布尔单选按钮,似乎
OnValidate()
将是做到这一点的完美方式。 However, i would need to trace what value was changed in the inspector and put a check for the identifier but I couldn't find the solution for the tracing part.但是,我需要跟踪检查器中更改了哪些值并检查标识符,但我找不到跟踪部分的解决方案。 How do i know what value was changed for
OnValidate()
to be called?我怎么知道要调用
OnValidate()
的值发生了什么变化?
As said in the comments might not be the most "beautiful" solution but I would do it eg like正如评论中所说,可能不是最“美丽”的解决方案,但我会这样做,例如
// These are the fields in the Inspector
// changing any via the Inspector will Invoke OnValidade
[SerializeField] private bool bool1;
[SerializeField] private bool bool2;
// These are private and will be used to check what was changed
private bool _oldBool1;
private bool _oldBool2;
private void OnValidate()
{
if(bool1 != _oldBool1)
{
// bool1 was changed
if(bool1)
{
// Probably: set all other values to false
}
else
{
// Probably check if all other values are false, if so this may not be false
}
}
if(bool2 != _oldBool2)
{
// bool2 was changed
if(bool2)
{
// Probably: set all other values to false
}
else
{
// Probably check if all other values are false, if so this may not be false
}
}
// Etc
// And finally store the new values
_oldBool1 = bool1;
_oldBool2 = bool2;
// Etc
}
Afaik the changes via script of the serialized fields should not Invoke another OnValidate
, only changes via the Inspector or the first time the asset is loaded. Afaik 通过序列化字段的脚本进行的更改不应调用另一个
OnValidate
,只能通过检查器或第一次加载资产时进行更改。
This function is called when the script is loaded or a value is changed in the Inspector (Called in the editor only).
这个 function 在加载脚本或在检查器中更改值时调用(仅在编辑器中调用)。
You could probably also work with a List/Array instead of individual fields of course当然,您也可以使用列表/数组而不是单个字段
Typed on smartphone so can't test it right now but I hope the idea gets clear在智能手机上打字,所以现在无法测试,但我希望这个想法很清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.