简体   繁体   English

Unity 3d 控制对象可见性

[英]Unity 3d controlling object visibility

I have two game objects namely objA and objB where the later is a child of former.我有两个游戏对象,即objAobjB ,后者是前者的孩子。 ObjA is active and B is disabled. ObjA处于活动状态, B处于禁用状态。 I wanted objB to appear when pointer enters and to disappear when pointer exits.我希望objB在指针进入时出现并在指针退出时消失。 I want this not just once but forever.我想要这不仅仅是一次,而是永远。 I have tried following code attached with objA to control visibility of objB but it's unsuccessful.我尝试使用objA附带的代码来控制objB可见性,但没有成功。 Could someone help me plz:有人可以帮我吗?

using UnityEngine;
using UnityEngine.EventSystems;

public class eventvisbtoggle : MonoBehaviour
{
    public void PointerEnter(BaseEventData eventData)
    {
        ObjB.gameobject.SetActive(true);
    }

    public void PointerExit(BaseEventData eventData)
    {
        ObjB.gameobject.SetActive(false);
    }
}

There is a difference between the enabled state of a GameObject and whether or not its is visible.游戏对象的启用状态与其是否可见之间存在差异。 active determines whether or not scripts and other components attached to the object function. active 确定脚本和其他组件是否附加到对象函数。 A component called the Renderer is what actually draws the GameObject in the scene.称为渲染器的组件实际在场景中绘制游戏对象。 What you want to do is disable the Renderers that are attached to the object您要做的是禁用附加到对象的渲染器

foreach(var r in GetComponentsInChildren<Renderer>()){
    r.enabled = false;
}

Note that this is very slow code, you should assign the appropriate renderers to your script using the editor, but this is just an example.请注意,这是非常慢的代码,您应该使用编辑器为您的脚本分配适当的渲染器,但这只是一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM