【问题标题】:How i can unhide all Object childrens?我如何取消隐藏所有对象子项?
【发布时间】:2021-11-04 13:52:31
【问题描述】:

我有一个统一游戏,对象子项被隐藏。如何通过按下鼠标按钮取消隐藏所有对象子项?

【问题讨论】:

  • “隐藏”到底是什么意思?不活跃?还是实际上隐藏在 HierarchyView 中?
  • 被gameObject.SetActive(false)隐藏在游戏中;
  • 我对 Unity 一无所知,但您是否考虑过遍历它们并在每一个上调用 SetActive(true)?只是猜测
  • 没有。我只想让玩家点击父母,父母不活动的孩子将是活动的
  • 如果您没有将孩子专门标记为处于活动状态。只有上一个。您只需打开最上面的一个

标签: c# .net unity3d


【解决方案1】:

您可以递归地获取所有孩子并使用GetComponentsInChildren 和一个每个孩子保证拥有的组件激活它们:Transform like

// Given parent object
GameObject yourParentObject;

// simply get any Transform component including the parent one itself 
// and anywhere nested below it
// (important!) pass in "true" in order to include inactive ones
foreach(var child in yourParentObject.GetComponentsInChildren<Transform>(true))
{
    // then set them all active
    child.gameObject.SetActive(true);
}

或者如果你不需要递归,因为孩子们是直接孩子们,只需这样做

// This iterates only through all direct children
foreach(Transform child in yourParentObject.transform)
{
    child.gameObject.SetActive(true);
}

【讨论】:

  • 我都喜欢你,什么也没发生
  • 那么我的父对象上方的一个对象似乎仍然处于非活动状态?我还建议Debugging your code 以查看是否实际执行了相应的代码
  • 父母有3个孩子。
  • 代码:using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CatOnClicked : MonoBehaviour { GameObject catthack; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnMouseDown() { foreach(var child in catthack.GetComponentsInChildren&lt;Transform&gt;(true)) { // then set them all active child.gameObject.SetActive(true); } } }
  • 我不知道如何将命名空间放入stackoverflow
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2020-12-22
  • 2014-10-24
  • 2012-12-15
相关资源
最近更新 更多