如何在Unity Inspector中隐藏组件

时间:2018-03-26 08:33:30

标签: c# unity3d

我的检查器中有一个带有很多组件的游戏对象。使用自定义编辑器,我能够访问和收集数组中的所有组件。我希望能够隐藏名为“blahblah”的组件。这是我的代码:

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

它不起作用,并且所有组件在Inspector中仍然可见。你觉得怎么回事?

更新: 游戏对象是流程图(由称为Fungus的外部资产创建)。我在其中添加了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;
        }
    }
}

这是自定义编辑器:

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 ();
    }
}

此截图显示FlowchartManager脚本附加到游戏对象没有任何问题:enter image description here

我没有得到任何编译器错误,顺便说一句。

1 个答案:

答案 0 :(得分:0)

让我着迷的是,我想可能是让您着迷的是,更改hideflags不会重新绘制编辑器。更改选择(或以其他方式强制面板重新创建)后,组件才会隐藏。

也可以代替

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

更好用

foo.GetType()==typeof(Bar)
相关问题