Проблема при использовании InitializeOnLoad постоянно делает его недействительным.
Тогда, даже если я создаю экземпляр один раз для списка в верхней части скрипта, он будет постоянно создавать экземпляр для него и добавлять все больше и больше элементов в список.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class CustomHierarchy
{
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<GameObject> gameobjectsHasAll = new List<GameObject>();
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)))
{
if (!gameobjectsHasAll.Contains(gameObj))
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
else
{
fontColor = prefabModFontColor;
}
break;
}
}
if (fontColor != prefabModFontColor)
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
else
{
fontColor = prefabOrgFontColor;
}
}
}
else
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
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;
}
}
Переменная List:
public static List<GameObject> gameobjectsHasAll = new List<GameObject>();
И я добавляю объекты в список в 3 местах кода в каждом месте, где HasAllComponents имеет значение true, например:
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
В Иерархии у меня есть 6 элементов, которые удовлетворяют условию HasAllComponents.
Но в первый раз список содержит 36 пунктов, он продолжает добавлять 6 пунктов 6 раз.
Затем следующие 120 предметов или около того, и они продолжают добавлять те же предметы без остановки.
Я вызываю этот список из другого сценария EditorWindow:
using System;
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
public class HierarchyEditor : EditorWindow
{
public static bool multipleComponents = false;
public static string multipleComponentsString = "";
private static SearchableEditorWindow hierarchy { get; set; }
private string filterText = "";
private string oldFilterText = "";
[MenuItem("Tools/Hierarchy Editor")]
public static void ShowWindow()
{
GetWindow<HierarchyEditor>("HierarchyEditor");
Test();
}
Где тест:
private static void Test()
{
var all = CustomHierarchy.gameobjectsHasAll;
}