using System.Collections.Generic; using UnityEngine; using UnityEngine.Pool; namespace Module.ProjectValidator.Editor { internal sealed class GameObjectValidatorDuplicateComponents : IGameObjectValidator { public void Validate(GameObject gameObject, List results) { using var _ = ListPool.Get(out var list); gameObject.GetComponents(list); list.Sort((c0, c1) => c0.GetType().GetHashCode().CompareTo(c1.GetType().GetHashCode())); if (list.Count == 0) return; var type = list[0].GetType(); var count = 1; for (var i = 1; i < list.Count; i++) { var t = list[i].GetType(); if (type == t) { count++; } else { if (count > 1) results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"GameObject has duplicate '{type.Name}' ({count}) components")); type = t; count = 1; } } if (count > 1) results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"GameObject has duplicate '{type.Name}' ({count}) components")); } } }