简体   繁体   English

如何统一设置自定义检查器字段的初始值

[英]How to set initial values for custom inspector fields in unity

I have two similar classes ClassA and ClassB . 我有两个类似的类ClassAClassB 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 利亚姆

The Reset method of MonoBehaviours seems that it will provide the functionality you are looking for. MonoBehaviours的Reset方法似乎将提供您正在寻找的功能。 It will look something like this: 它看起来像这样:

void Reset() 
{
    _overwrite = true;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM