简体   繁体   English

在Unity中,如何在Inspector中更改布尔值时触发Set方法?

[英]In Unity, How to Trigger the Set Method When Changing Boolean Value in the Inspector?

Let's say I have the following class attached to a game object:假设我将以下类附加到游戏对象:

public class SomeClass : MonoBehaviour
{

    [SerializeField]
    private bool aBool;

    public bool ABool 
    {
        get 
        {
            return aBool;
        }
        set 
        {
            Debug.Log("we are setting the bool!");
            
            // triggered when changing from false to true
            if (aBool == false && value == true) 
            {
                // do stuff
            }

            // triggered when changing from true to false
            if (aBool == true && value == false)
            {
                // do stuff
            }

            aBool = value;
        } 
    }

    // Other fantastic methods and etc here.
}

When I play my game and check the aBool checkbox on the game object in the inspector, the set method is not being triggered.当我玩游戏并在检查器中选中游戏对象上的 aBool 复选框时,未触发 set 方法。 My debug log statement is not triggering, so it looks like it is not entering the set method.我的调试日志语句没有触发,所以看起来没有进入set方法。

How do I trigger the set method when I set the boolean in the inspector window, when playing the game?玩游戏时,如何在检查器窗口中设置布尔值时触发 set 方法? Have I done something wrong when creating the property?我在创建属性时做错了什么吗?

Posting for anyone else in the same situation.为处于相同情况的其他人发帖。

In unity, when you have a bool exposed in the inspector, which also has a property overriding the set method, it does not call the set method when the bool is set in the inspector.在 unity 中,当您在检查器中暴露了一个 bool,它也有一个覆盖 set 方法的属性,当在检查器中设置 bool 时,它不会调用 set 方法。 Instead, it is modifying the bool directly.相反,它直接修改 bool。

If you want to be able to change a bool in the inspector and detect when it is changed in a script, you will need to poll for changes in the update function.如果您希望能够在检查器中更改 bool 并检测它何时在脚本中更改,则需要轮询更新函数中的更改。

This makes my particular use case a bit more verbose, but oh well.这使我的特定用例更加冗长,但哦,好吧。

The Inspector itself is not aware of any properties in your class. Inspector 本身不知道您的类中的任何属性。 It will always only expose the serialized fields .它总是只公开序列化的字段 See also Unity Script Serialization .另请参阅Unity 脚本序列化

What you see and edit in the Inspector is not the property ABool but rather the field aBool which itself is in no way aware of that ABool exists at all.您在 Inspector 中看到和编辑的不是属性ABool ,而是字段aBool ,它本身根本不知道ABool存在。

The confusion might be caused by the Inspector automatically making all exposed fields "human readable" starting with capital letters.混淆可能是由于 Inspector 自动使所有暴露的字段以大写字母开头“人类可读”引起的。


One way that was mentioned is using OnValidate which s automatically called when a value of this component was changed via the Inspector and do eg提到的一种方法是使用OnValidate当通过检查器更改此组件的值时自动调用它并执行例如

private void OnValidate()
{
    // Only call properties during PlayMode since they might depend on runtime stuff
    if(!Application.isPlaying) return;

    ABool = aBool;
}

Another way would be to implement your own field attribute like eg mentioned in OnValueChanged for fields in a Scriptable Object另一种方法是实现您自己的字段属性,例如在OnValueChanged 中提到的用于脚本对象中的字段

I think anyone did not notice that OP is setting abool variable and has getter and setter on Abool variable .Obviously setter and getter will get triggered when read write happens on Abool not abool.我想任何人都没有注意到 OP 正在设置 abool 变量,并且在 Abool 变量上有 getter 和 setter。显然,当读写发生在 Abool 而不是 abool 时,setter 和 getter 将被触发。 But OP is setting abool instead of Abool.但是 OP 正在设置 abool 而不是 Abool。 Also properties (variables with get/set methods) can not be exposed in inspector to be set by inspector by yourself.此外,属性(具有 get/set 方法的变量)不能在检查员中公开以由检查员自己设置。 They can only be altered through code.它们只能通过代码进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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