【问题标题】:Does 'this' refer to gameObject or class or the name of the c# script?'this' 是指游戏对象或类还是 c# 脚本的名称?
【发布时间】:2020-06-06 00:04:54
【问题描述】:

我试图了解如何为 Unity3D 实现 DontDestroyOnLoad,我偶然发现了这篇文章:https://honkbarkstudios.com/developer-blog/dontdestroyonload-tutorial/

本文由 Christian Engvall 撰写,提供以下代码作为教程:

using UnityEngine;
using System.Collections;

public class MusicController : MonoBehaviour {

    public static MusicController Instance;

    void Awake() 
    {
        this.InstantiateController();
    }

    private void InstantiateController() {
        if(Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else if(this != Instance) {
            Destroy(this.gameObject);
        }
    }
}

更多信息:Engvall 创建了一个名为 MusicController 的游戏对象,并为其附加了一个音频源。他还附加了一个 C# 脚本,也称为 MusicController。然后该脚本包含一个类型为 MusicController 的公共静态变量,他将游戏对象拖入其中。代码的目标是让音频在场景中不间断地播放,即在新场景加载时不要破坏包含音频源的游戏对象。

我很困惑“this”是指 gameObject 还是 MusicController 类。当我阅读this.InstantiateController(); 时,似乎“this”必须是 MusicController 公共类。但是,在下面

if(Instance == null)
{
    Instance = this;
    DontDestroyOnLoad(this);
}
else if(this != Instance) {
    Destroy(this.gameObject);
}

似乎“this”必须引用链接到名为 Instance 的 MusicController 公共静态变量的游戏对象。

所以我很困惑。它是哪一个?还是完全是另外一回事?

我尝试阅读 6 年前的这篇堆栈溢出帖子,但不幸的是我仍然感到困惑。 The 'this' keyword in Unity3D scripts (c# script, JavaScript)

提前感谢您的帮助。

【问题讨论】:

    标签: c# unity3d this persistent gameobject


    【解决方案1】:

    您发布的代码是制作 MusicComponent 的简单单例。

    this 总是指类的实例。因此,在这种情况下,它是 MusicController 的实例。

    this.gameObject 仅表示您正在获取附加到MusicControllergameObject

    Instance 是静态的,这意味着只有一个“实例”固定在内存中的某个位置。 MusicComponent 的每个实例都可以访问那个单一的“实例”对象。所以,在你强调的方法中:

    //I'm an instance of MusicComponent and I want to see if
    //some other instance exists. If there's another music component already
    //then Instance will NOT be null
    if(Instance == null)
    {
        //Now I know I'm the only MusicComponent created so I'll set this static property to me
        //This lets any new MusicComponents created know that there is already one alive
        //and they should not stay alive
        Instance = this;
        DontDestroyOnLoad(this);
    }
    else if(this != Instance) {
        //Turns out, I'm not the first instance of MusicComponent created because
        //because another one has already set themselves to static property
        //I know this but my pointer (this) isn't the same as the pointer set to the 
        //Instance static property
        Destroy(this.gameObject);
    }
    

    我希望这能解决一些问题。

    【讨论】:

    • 谢谢。那真的很有帮助。我想我在概念化将类的实例设置为类本身时遇到了麻烦。我承认不是很有经验,但我会认为一个实例只能设置为一个实例。对我来说,概念化 'this.InstantiateController()' 比 'Instance = this' 更容易......如果这有任何意义的话。但是您的解释非常有帮助,我认为至少对我来说已经足够了。谢谢。
    • @Siggytron 静态属性意味着该类的所有实例共享相同的属性。因此,当musicComponent1 设置Instance = this 时,musicComponent2 可以看到Instance 指向musicComponent1。我承认,当你刚开始时,把你的头绕起来有点棘手。但请继续努力;你做得很好。
    【解决方案2】:

    这就像在说我自己。

    在组件的统一脚本中,它指的是你所在的类实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2017-12-13
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      相关资源
      最近更新 更多