【问题标题】:GameObject.SetActiveRecursively (true) obsolete?GameObject.SetActiveRecursively (true) 过时了吗?
【发布时间】:2017-05-21 08:05:58
【问题描述】:

当我尝试使用时:

GameObject.Find("teleportation").SetActiveRecursively(true);

GameObject.SetActiveRecursively(bool) 已过时,它说要改用 GameObject.SetActive(),现在由子代继承。

在我的层次结构中,我有一个对象处于活动状态,而他的孩子处于非活动状态。 如果我在活动对象上使用GameObject.SetActive(),则不会激活孩子。 如果我在活动对象上使用GameObject.SetActiveRecursively(),则会激活孩子。

那么,我应该使用不同的东西来让孩子活跃吗? 我不明白为什么GameObject.SetActive() 不起作用。

【问题讨论】:

  • 我没有在文档中看到它说“GameObject.SetActive() ... 现在由孩子继承。”你能解释一下你所说的那句话是什么意思吗?
  • 快速谷歌搜索表明此功能已被 some time 淘汰 - 根据 documentation,它根本不存在于最新版本中
  • @Amy 你可以通过代码看到它,该行带有绿色波浪下划线。这意味着它已经贬值了。
  • 我理解 deprecated 和 obsoleted 的含义。那不是我问的。 “现在由孩子继承”语句是什么意思?

标签: c# unity3d unity5


【解决方案1】:

SetActiveRecursively 函数就像您提到的那样已过时。您现在应该使用SetActive()

您可以使用递归和SetActive() 来复制SetActiveRecursively 的功能。也可以放在扩展方法中。

public static class ExtentionMethod
{
    public static void SetActiveRecursivelyExt(this GameObject obj, bool state)
    {
        obj.SetActive(state);
        foreach (Transform child in obj.transform)
        {
            SetActiveRecursivelyExt(child.gameObject, state);
        }
    }
}

用法

GameObject obj = GameObject.Find("teleportation");
obj.SetActiveRecursivelyExt(true);

【讨论】:

  • 所以一个已经存在并方便地处理这项工作的功能已被弃用,而没有提供替代品..!?为什么会这样?
  • @ThomasHilbert 这就是你的 Unity。替换为 GameObject.SetActive(),但其行为略有不同。
猜你喜欢
  • 2020-07-21
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
相关资源
最近更新 更多