简体   繁体   English

如何在Unity Inspector中隐藏组件

[英]How to hide components in Unity Inspector

I have a game object in my inspector with a lot of components. 我的检查器中有一个包含很多组件的游戏对象。 Using a custom editor, I have been able to access and collect all of the components in an array. 使用自定义编辑器,我已经能够访问和收集数组中的所有组件。 I want to be able to hide those components named "blahblah". 我希望能够隐藏名为“ blahblah”的那些组件。 This is my code: 这是我的代码:

arryCom = GetComponents(typeof(Component));
    for (int i = 0; i < arryCom.Length; i++)
    {
        if (arryCom [i].GetType ().ToString () == "blahblah")
              arryCom [i].hideFlags = HideFlags.HideInInspector;
    }

It doesn't work, and all of components are still visible in the Inspector. 它不起作用,并且所有组件在Inspector中仍然可见。 What do you think is wrong? 你觉得错什么?

UPDATE: The game object is a Flowchart (created by an external asset called Fungus). 更新:游戏对象是流程图(由称为真菌的外部资产创建)。 I added the FlowchartManager to it: 我添加了FlowchartManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlowchartManager : MonoBehaviour 
{
    private Component[] arryCom;

    public void HideSays()
    {
        arryCom = GetComponents(typeof(Component));
        for (int i = 0; i < arryCom.Length; i++)
        {
            if (arryCom [i].GetType ().ToString () == "blahblah")
                arryCom [i].hideFlags = HideFlags.HideInInspector;
        }
    }
}

And this is the custom editor: 这是自定义编辑器:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(FlowchartManager))]
public class FlowchartEditor : Editor
{
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();
        FlowchartManager fm = (FlowchartManager)target;
        if (GUILayout.Button ("Hide Says"))
            fm.HideSays ();
    }
}

And this screenshot shows that the FlowchartManager script is attached to the game object without any problem: enter image description here 此屏幕快照显示FlowchartManager脚本已附加到游戏对象上,没有任何问题: 在此处输入图像描述

I don't get any compiler errors, btw. 我没有任何编译器错误,顺便说一句。

The thing that got me, and I imagine it might have gotten you, is that changing the hideflags doesn't repaint the editor. 让我着迷的是,我想它可能会让您着迷的是,更改hideflags不会重新绘制编辑器。 Components will only hide after you change the selection (or force the panel recreation another way). 仅在更改选择(或以其他方式强制面板重新创建)后,组件才会隐藏。

Also, instead of 另外,代替

foo.GetType().ToString()=="Bar" 

its better to use 更好用

foo.GetType()==typeof(Bar)

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

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