空合并运算符对self的分配 [英] null coalescing operator assignment to self

查看:111
本文介绍了空合并运算符对self的分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Unity中设置了两个脚本来处理一些UI音频.一个是经理,另一个是在其中播放特定UI元素的声音.我所拥有的是这个的简化版本:

I currently have two scripts set up in Unity to handle some UI Audio. One is a manager and the other is there to play a sound for a specific UI element. A simplified version of what I have is this:

public class AudioUIManager : MonoBehaviour //Only one of these in the scene
{
    public AudioClip genericUISound; //This is set in the inspector.
}


public class AudioUITextAnimation : MonoBehaviour
{
    [SerializeField]
    private AudioClip specifiedUISound; //This is not set in the inspector
    [SerializeField]
    private AudioUIManager audioUIManager; // I get a reference to this elsewhere

    void Start()
    {
        //Use generic sounds from audio manager if nothing is specified.
        specifiedUISound = specifiedUISound ?? audioUIManager.genericUISound;
        print(specifiedUISound);
    }
}

我在这里想要实现的是让specifiedUISound字段使用在检查器中分配给它的声音.如果未分配声音,则使用UI管理器中的常规声音.这省去了我将相同的声音分配给需要相同声音的数百万个按钮的麻烦,但如果需要的话,还可以为一个按钮选择特定的声音.

What I'm trying to achieve here is to have the specifiedUISound field use the sound that it is assigned to it in the inspector. If no sound is assigned then use the generic sound from the UI manager. This saves me assigning the same sound to millions of buttons that require the same sound but gives me the option of having a specific sound for one button if I want to.

但是,即使为空且audioUIManager的声音不是,空值空缺运算符也会将null分配给specifiedUISound.另外,如果像这样使用三元运算符来检查null,我可以使它工作:

However, the null-coalessing operator is asigning null to specifiedUISound even though it is null and the audioUIManager's sound is not. Also, I can get this to work if I use the ternary operator to check for null like so:

specifiedUISound = specifiedUIsound == null ? audioUIManager.genericUISound : specifiedUISound;

我是否误解了空合并运算符?为什么会这样?

Am I misunderstanding the null coalescing operator? Why does this happen?

Jerry Switalski指出,只有在specifiedUISound被序列化时才会发生这种情况. (如果它是public或具有[SerializeField]属性).任何人都可以对这里发生的事情有所了解吗?

Jerry Switalski has pointed out that this only happens when the specifiedUISound is serialized. (if it is public or if it has the [SerializeField] attribute). Can anyone shed some light on what is going on here?

推荐答案

您正在遇到Unity的

You're running into Unity's custom equality operator:

当MonoBehaviour具有字段时,仅在编辑器中,我们不会将这些字段设置为真实null",而是设置为"fake null"对象.我们的自定义==运算符能够检查某些东西是否是这些虚假的空对象之一,并相应地执行操作.尽管这是一个奇特的设置,但它允许我们将信息存储在伪null对象中,当您在该对象上调用方法或向该对象请求属性时,它会为您提供更多上下文信息.没有这个技巧,您将只会得到NullReferenceException,即堆栈跟踪,但是您将不知道哪个GameObject的MonoBehaviour的字段为null.

When a MonoBehaviour has fields, in the editor only, we do not set those fields to "real null", but to a "fake null" object. Our custom == operator is able to check if something is one of these fake null objects, and behaves accordingly. While this is an exotic setup, it allows us to store information in the fake null object that gives you more contextual information when you invoke a method on it, or when you ask the object for a property. Without this trick, you would only get a NullReferenceException, a stack trace, but you would have no idea which GameObject had the MonoBehaviour that had the field that was null.

在编辑器中运行时,Unity将序列化的null替换为实际上不是null的前哨值.这样一来,他们就可以在某些情况下提供更多有用的错误消息.

While running in the editor, Unity replaces your serialized null with a sentinel value that isn't actually null. This allows them to provide more informative error messages in some circumstances.

specifiedUISound等于null吗?那取决于你的要求. C#具有多个平等"概念,包括数据相等和引用相等.

Is specifiedUISound equal to null? That depends on how you ask. C# has multiple notions of "equality", including data equality and reference equality.

一些检查会说这些值相等:==Object.Equals

Some checks will say the values are equal: == and Object.Equals

其他人会说他们不相等:??Object.ReferenceEquals

Others will say they are not equal: ?? and Object.ReferenceEquals

此行为将仅在编辑器中发生.在独立版本中运行时,任何null值都将仅为null.

This behavior will only occur in the editor. When running in a standalone build, any null values will just be null.

这篇关于空合并运算符对self的分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆