本文介绍了通过脚本在 Unity3D 中隐藏/取消隐藏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一切开始之前将我的场景的一部分设置为隐藏,因此,假设我有一些 isHidden(我正在尝试查找)属性,我之前或在场景的第一帧将其设置为 true.

i want to set part of my scene to be hidden before everything starts,so lets say i have some isHidden (which im trying to find) property that i set to true before, or at the first frame of my scene.

在某些时候我想取消隐藏它并触发动画,我知道如何触发动画.我发现隐藏对象的唯一方法是将其设置为:

at some point i want to unhide it and trigger the animation, i know how to trigger the animation.and the only way i found to hid object is to set it like :

SetActive(false);

它确实隐藏了,但是当我稍后在脚本中尝试使用

it does hide is, but when i am trying to access it later in the script with

GameObject.Find("gameObjectName");

它返回空值.那么,通过脚本隐藏或隐藏 GameObject 的正确方法是什么?这不是 Mesh 所以它没有网格渲染器.

it returns null.so, what is the correct approach to hide or hide GameObject via script.this is not a Mesh so it dont have a mesh renderer.

推荐答案

use Renderer.enabled

bool enabled;
Description

Makes the rendered 3D object visible if enabled.

示例代码:

public class ExampleClass : MonoBehaviour {
    void Show() {
        renderer.enabled = true;
    }
    void Hide() {
        renderer.enabled = false;
    }
    void HideChildren()
    {
         Renderer[] lChildRenderers=gameObject.GetComponentsInChildren<Renderer>();
         foreach ( Renderer lRenderer in lChildRenderers)
         {
              lRenderer.enabled=false;
         }
    }
    void ShowChildren()
    {
         Renderer[] lChildRenderers=gameObject.GetComponentsInChildren<Renderer>();
         foreach ( Renderer lRenderer in lChildRenderers)
         {
              lRenderer.enabled=true;
         }
    }


}

这篇关于通过脚本在 Unity3D 中隐藏/取消隐藏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:58