Added support to scan prefabs

This commit is contained in:
Anders Ejlersen 2026-05-19 20:15:30 +02:00
parent 269789b36f
commit 591693da1d
11 changed files with 159 additions and 62 deletions

View file

@ -0,0 +1,43 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
namespace Module.ProjectValidator.Editor
{
internal sealed class GameObjectValidatorDuplicateComponents : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
using var _ = ListPool<Component>.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"));
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b27a4e96523d4d3d97c11b32814f29d3
timeCreated: 1779213834

View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Pool;
namespace Module.ProjectValidator.Editor
{
internal sealed class GameObjectValidatorObsoleteComponents : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
using var _ = ListPool<Component>.Get(out var list);
gameObject.GetComponents(list);
for (var i = 0; i < list.Count; i++)
{
var type = list[i].GetType();
if (type.GetCustomAttribute(typeof(ObsoleteAttribute)) != null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"GameObject has obsolete '{type.Name}' component"));
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ca678bbf72fa4c8f8c9b945535aacf44
timeCreated: 1779214145