C# 如何过滤和隐藏不包含多个组件的对象?
但这会隐藏层次结构中的所有对象。C# 如何过滤和隐藏不包含多个组件的对象?,c#,unity3d,C#,Unity3d,但这会隐藏层次结构中的所有对象。 我想进行某种筛选,只在层次结构中显示包含多个组件的对象。确保HasAllComponents功能正常工作。调用此函数时,将Debug.Log(gameObj.name)放在else语句中。它应该打印没有多个组件的对象。这是真的吗?它与Deubg.Log(gameObj)一起工作但是也要让它工作得非常慢,因为它打印了很多对象,但它使用的是HasAllComponents方法,在使用HideFlags之前,我看到它工作得很好,因为它将所有对象的多个组件着色为黄色。问
我想进行某种筛选,只在层次结构中显示包含多个组件的对象。确保
HasAllComponents
功能正常工作。调用此函数时,将Debug.Log(gameObj.name)
放在else
语句中。它应该打印没有多个组件的对象。这是真的吗?它与Deubg.Log(gameObj)一起工作但是也要让它工作得非常慢,因为它打印了很多对象,但它使用的是HasAllComponents方法,在使用HideFlags之前,我看到它工作得很好,因为它将所有对象的多个组件着色为黄色。问题开始于我出于某种原因使用HideFlags时,它隐藏了所有对象,甚至当我删除了带有隐藏标签的线条所有对象都隐藏了我必须删除并添加回场景才能再次看到所有对象(我有场景的备份)。也许我不应该使用HideFlags,而是以某种方式启用/禁用else?中的对象。请注意,这些对象仍然存在于场景中。它们只是从“层次”选项卡中隐藏。如果要再次看到它们,请将标志设置为“无”gameObj.hideFlags=hideFlags.None
。若要使对象从场景中消失,请使用SetActive
函数。@程序员我用17小时前你回答我的问题时所做的编辑和更新了我的问题,现在我看到了。我不能同时使用BoxCollider和MeshRenderer使用SceneModeUtility.SearchForType和SceneModeUtility.SearchBar
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
//Adapted from Unity3DCollege YouTube Video Tutorial https://www.youtube.com/watch?v=pdDrY8Mc2lU
[InitializeOnLoad]
public class CustomHierarchy : MonoBehaviour
{
private static Vector2 offset = new Vector2(0, 2);
public static Color gameObjectFontColor = Color.black;
public static Color prefabOrgFontColor = Color.black;
public static Color prefabModFontColor = Color.white;
public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
public static Color meshRendererColor = Color.yellow;
public static List<string> componentsList = new List<string>();
static CustomHierarchy()
{
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
}
private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
Color fontColor = gameObjectFontColor;
Color backgroundColor = new Color(.76f, .76f, .76f);
FontStyle styleFont = FontStyle.Normal;
var obj = EditorUtility.InstanceIDToObject(instanceID);
GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (Selection.instanceIDs.Contains(instanceID))
{
backgroundColor = new Color(0.24f, 0.48f, 0.90f);
}
if (obj != null)
{
var prefabType = PrefabUtility.GetPrefabType(obj);
if (gameObj.activeInHierarchy == false)
{
backgroundColor = inActiveColor;
}
if (prefabType == PrefabType.PrefabInstance)
{
styleFont = FontStyle.Bold;
PropertyModification[] prefabMods = PrefabUtility.GetPropertyModifications(obj);
foreach (PropertyModification prefabMod in prefabMods)
{
if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
fontColor = meshRendererColor;
}
else
{
gameObj.hideFlags = gameObj.hideFlags | HideFlags.HideInHierarchy;
fontColor = prefabModFontColor;
}
break;
}
}
if (fontColor != prefabModFontColor)
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
fontColor = meshRendererColor;
}
else
{
gameObj.hideFlags = gameObj.hideFlags | HideFlags.HideInHierarchy;
fontColor = prefabOrgFontColor;
}
}
}
else
{
if(HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
fontColor = meshRendererColor;
}
Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
EditorGUI.DrawRect(selectionRect, backgroundColor);
EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
{
normal = new GUIStyleState() { textColor = fontColor },
fontStyle = styleFont
}
);
}
}
public static bool HasAllComponents(GameObject gameObject, params System.Type[] types)
{
for (int i = 0; i < types.Length; i++)
{
if (gameObject.GetComponent(types[i]) == null)
return false;
}
return true;
}
}
gameObj.hideFlags = gameObj.hideFlags | HideFlags.HideInHierarchy;