[英]How to set initial values for custom inspector fields in unity
I have two similar classes ClassA
and ClassB
. 我有两个类似的类
ClassA
和ClassB
。 Both classes contain a bool: 这两个类都包含bool:
In Class A: 在A类中:
[SerializeField]
private bool _overwrite = true;
public bool overwrite
{
get { return _overwrite; }
set { _overwrite = value; }
}
In Class B: 在B类中:
[SerializeField]
private bool _hide = true;
public bool hide
{
get { return _hide; }
set { _hide = value; }
}
Both scripts have a CustomEditor
script. 这两个脚本都有一个
CustomEditor
脚本。 In both Editor scripts, inside the OnInspectorGUI()
method the following two lines are used to add the respective bool's to the Inspector. 在这两个编辑器脚本中,在
OnInspectorGUI()
方法内部,以下两行用于将各个布尔值添加到Inspector。
ClassA.overwrite = EditorGUILayout.ToggleLeft("Overwrite", ClassA.overwrite);
ClassB.hide = EditorGUILayout.ToggleLeft("Hide", ClassB.hide);
When I add ClassA to a GameObject, the "Overwrite" field is unchecked, however when I add ClassB to a GameObject, the "Hide" field is checked. 当我向游戏对象添加ClassA时,未选中“覆盖”字段,但是当我向游戏对象添加ClassB时,选中了“隐藏”字段。
I don't understand what is different, or what other factor is involved in setting a default / initial value for a property. 我不了解有什么不同,或者为属性设置默认值/初始值涉及什么其他因素。
Ideally I want them both to be checked by default. 理想情况下,我希望两者都默认选中。
Any ideas what I might be missing? 有什么想法可能会丢失吗?
Thanks for your time, 谢谢你的时间,
Liam 利亚姆
What is grabObject
? 什么是
grabObject
?
If it is the component variable which you have found a reference to in the OnEnable
method using the target
variable of the Editor
script, then simply changing your editor scripts to: 如果它是使用
Editor
脚本的target
变量在OnEnable
方法中找到引用的组件变量,则只需将编辑器脚本更改为:
grabObject.overwrite = EditorGUILayout.ToggleLeft("Overwrite", grabObject.overwrite);
and: 和:
grabObject.hide = EditorGUILayout.ToggleLeft("Hide", grabObject.hide);
should solve your problem. 应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.