【发布时间】:2019-06-16 21:29:10
【问题描述】:
我的检查器中有一个包含很多组件的游戏对象。使用自定义编辑器,我已经能够访问和收集数组中的所有组件。我希望能够隐藏那些名为“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
顺便说一句,我没有收到任何编译器错误。
【问题讨论】:
-
您的
if声明搞砸了。那应该是if (arryCom[i].GetType().ToString() == "blahblah")。您如何期望无法编译的代码执行? -
感谢@Programmer。我纠正了它。还是不行。
-
在这种情况下,请包含 complete 脚本。既然你犯了那个错误,我怀疑还有更多,你的脚本没有编译。另外,显示
blahblah脚本的完整代码。最后,附上它的 GameObject 的截图,以证明它实际上是附在一个 Object 上的。当我说 *complete 脚本时,我指的是该文件中的每个代码。复制粘贴 -
你为什么不用
arryCom = GetComponents(typeof(blahblah));????它做同样的事情,但你必须删除if (arryCom [i].GetType ().ToString () == "blahblah") -
看来我无法很好地解释自己,因为英语不是我的母语。我会再试一次:我有一个 GameObject。它的名字是“流程图”。流程图有 50 个组件。在这 50 个组件中,有 10 个属于“blahblah”类型。我希望检查员隐藏所有这 10 个废话,只显示 40 个其他组件。