Compare commits

...

14 commits
0.2.0 ... main

Author SHA1 Message Date
eb46c22ffc Validator: Skipping render pipeline check on shader, fi no RenderPipeline tag is found
Validator: Skipping textures in material, if marked as PerRendererData, HideInInspector or NonModifiableTextureData
Validator: Fixed issue, where arrays weren't iterated correctly, which resulted in an exception
Window: Added warning and error count
2026-05-27 21:50:04 +02:00
9dd45f536e Validator: Fixed issue, where scene wouldn't unload, if an exception is thrown 2026-05-27 19:24:07 +02:00
3802ca557c Validator: Duplicate component validator is now only run on types without DisallowMultipleComponent attribute 2026-05-27 19:17:55 +02:00
ed3c02a0aa Bumped version to 1.0.2 2026-05-26 18:05:54 +02:00
2ec61871c2 Fixed issue, where GUID was moved between Unity versions from UnityEditor (6.3) to UnityEngine (6.4) 2026-05-26 18:05:38 +02:00
58acb2979d Fixed issue, where the validator enabled state couldn't be loaded correctly, if in another assembly 2026-05-24 23:09:47 +02:00
ddb4fc25ea Documentation: Updated editor window 2026-05-24 18:09:03 +02:00
dd55a87740 - Validator: Added asset validators with material texture and shader validation
- Validator: Added option to enable/disable certain validators
- Project Settings: Fixed issue, where changes weren't always saved
- Unity: Removed deprecated warnings in Unity 6.4
2026-05-24 18:06:56 +02:00
01ac17a078 Removed prefixes on paths 2026-05-19 22:11:31 +02:00
47c9c53819 Added attribute validators for min and range attributes 2026-05-19 20:24:16 +02:00
591693da1d Added support to scan prefabs 2026-05-19 20:15:30 +02:00
269789b36f Added GameObject Validators 2026-05-18 21:59:59 +02:00
c8a6815316 Updated readme 2026-05-18 20:53:00 +02:00
5f108eee50 Updated Readme file 2026-05-18 20:43:33 +02:00
50 changed files with 1312 additions and 121 deletions

View file

@ -10,19 +10,32 @@ namespace Module.ProjectValidator.Editor
{ {
static EditorProjectValidatorHierarchy() static EditorProjectValidatorHierarchy()
{ {
#if UNITY_6000_4_OR_NEWER
EditorApplication.hierarchyWindowItemByEntityIdOnGUI -= OnHierarchyWindowItemByEntityIdOnGUI;
EditorApplication.hierarchyWindowItemByEntityIdOnGUI += OnHierarchyWindowItemByEntityIdOnGUI;
#else
EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyWindowItemOnGUI; EditorApplication.hierarchyWindowItemOnGUI -= OnHierarchyWindowItemOnGUI;
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI; EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI;
#endif
EditorSceneManager.sceneOpened -= OnSceneOpened; EditorSceneManager.sceneOpened -= OnSceneOpened;
EditorSceneManager.sceneOpened += OnSceneOpened; EditorSceneManager.sceneOpened += OnSceneOpened;
} }
#if UNITY_6000_4_OR_NEWER
private static void OnHierarchyWindowItemByEntityIdOnGUI(EntityId entityId, Rect selectionRect)
{
if (Report.HasActive && Report.Active.TryGetSeverityFor(entityId, out var instance) && instance.Severity != EValidatorSeverity.Valid)
EditorIconUtility.Draw(new Rect(selectionRect.x, selectionRect.y, selectionRect.height, selectionRect.height), instance.Severity, instance.IsRedirect);
}
#else
private static void OnHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect) private static void OnHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{ {
if (Report.HasActive && Report.Active.TryGetSeverityFor(instanceID, out var instance) && instance.Severity != EValidatorSeverity.Valid) if (Report.HasActive && Report.Active.TryGetSeverityFor(instanceID, out var instance) && instance.Severity != EValidatorSeverity.Valid)
EditorIconUtility.Draw(new Rect(selectionRect.x, selectionRect.y, selectionRect.height, selectionRect.height), instance.Severity, instance.IsRedirect); EditorIconUtility.Draw(new Rect(selectionRect.x, selectionRect.y, selectionRect.height, selectionRect.height), instance.Severity, instance.IsRedirect);
} }
#endif
private static void OnSceneOpened(Scene scene, OpenSceneMode mode) private static void OnSceneOpened(Scene scene, OpenSceneMode mode)
{ {
if (Report.HasActive && ProjectValidatorUtility.IsValidForRun()) if (Report.HasActive && ProjectValidatorUtility.IsValidForRun())

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

View file

@ -0,0 +1,117 @@
fileFormatVersion: 2
guid: a6c61d5fbd310894d8159ba6af32d7e3
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 0
wrapV: 0
wrapW: 0
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 4
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
customData:
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spriteCustomMetadata:
entries: []
nameFileIdTable: {}
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace Module.ProjectValidator.Editor namespace Module.ProjectValidator.Editor
{ {
@ -11,23 +12,28 @@ namespace Module.ProjectValidator.Editor
public readonly List<Entry> Entries = new(); public readonly List<Entry> Entries = new();
private readonly Dictionary<GUID, MappingEntry> _assetToSeverityMapping = new(); private readonly Dictionary<GUID, MappingEntry> _assetToSeverityMapping = new();
#if UNITY_6000_4_OR_NEWER
private readonly Dictionary<EntityId, MappingEntry> _instanceToSeverityMapping = new();
#else
private readonly Dictionary<int, MappingEntry> _instanceToSeverityMapping = new(); private readonly Dictionary<int, MappingEntry> _instanceToSeverityMapping = new();
#endif
public void Add(GUID assetGuid, string scenePath, string fieldPath, Attribute attribute, EValidatorSeverity severity, string message) public void Add(GUID assetGuid, string relativePath, string fieldPath, Attribute attribute, EValidatorSeverity severity, string message)
{ {
var type = ProjectValidatorUtility.GetAttributeShortName(attribute); var type = ProjectValidatorUtility.GetAttributeShortName(attribute);
Add(assetGuid, scenePath, fieldPath, type, severity, message); Add(assetGuid, relativePath, fieldPath, type, severity, message);
} }
public void Add(GUID assetGuid, string scenePath, string fieldPath, string type, EValidatorSeverity severity, string message) public void Add(GUID assetGuid, string relativePath, string fieldPath, string type, EValidatorSeverity severity, string message)
{ {
Entries.Add(new Entry Entries.Add(new Entry
{ {
AssetGuid = assetGuid, AssetGuid = assetGuid,
AssetName = EditorAssetUtility.GetAssetName(assetGuid), AssetName = EditorAssetUtility.GetAssetName(assetGuid),
ScenePath = scenePath, RelativePath = relativePath,
FieldPath = fieldPath, FieldPath = fieldPath,
ScenePathRichText = ProjectValidatorUtility.ApplyRichTextToScenePath(scenePath), RelativePathRichText = ProjectValidatorUtility.ApplyRichTextToRelativePath(relativePath),
FieldPathRichText = ProjectValidatorUtility.ApplyRichTextToFieldPath(fieldPath), FieldPathRichText = ProjectValidatorUtility.ApplyRichTextToFieldPath(fieldPath),
Type = type, Type = type,
Severity = severity, Severity = severity,
@ -64,7 +70,17 @@ namespace Module.ProjectValidator.Editor
mapping = new MappingEntry(); mapping = new MappingEntry();
return false; return false;
} }
#if UNITY_6000_4_OR_NEWER
public bool TryGetSeverityFor(EntityId entityId, out MappingEntry mapping)
{
if (_instanceToSeverityMapping.TryGetValue(entityId, out mapping))
return true;
mapping = new MappingEntry();
return false;
}
#else
public bool TryGetSeverityFor(int instanceId, out MappingEntry mapping) public bool TryGetSeverityFor(int instanceId, out MappingEntry mapping)
{ {
if (_instanceToSeverityMapping.TryGetValue(instanceId, out mapping)) if (_instanceToSeverityMapping.TryGetValue(instanceId, out mapping))
@ -73,8 +89,9 @@ namespace Module.ProjectValidator.Editor
mapping = new MappingEntry(); mapping = new MappingEntry();
return false; return false;
} }
#endif
public bool TryGetSeverityFor(GUID assetGuid, string scenePath, out MappingEntry mapping) public bool TryGetSeverityFor(GUID assetGuid, string relativePath, out MappingEntry mapping)
{ {
if (!_assetToSeverityMapping.TryGetValue(assetGuid, out mapping)) if (!_assetToSeverityMapping.TryGetValue(assetGuid, out mapping))
return false; return false;
@ -83,7 +100,7 @@ namespace Module.ProjectValidator.Editor
for (var i = 0; i < Entries.Count; i++) for (var i = 0; i < Entries.Count; i++)
{ {
if (Entries[i].AssetGuid != assetGuid || Entries[i].ScenePath != scenePath || Entries[i].Severity <= mapping.Severity) if (Entries[i].AssetGuid != assetGuid || Entries[i].RelativePath != relativePath || Entries[i].Severity <= mapping.Severity)
continue; continue;
mapping = new MappingEntry(Entries[i].Severity, false); mapping = new MappingEntry(Entries[i].Severity, false);
@ -110,10 +127,10 @@ namespace Module.ProjectValidator.Editor
public GUID AssetGuid; public GUID AssetGuid;
public string AssetName; public string AssetName;
public string ScenePath; public string RelativePath;
public string FieldPath; public string FieldPath;
public string ScenePathRichText; public string RelativePathRichText;
public string FieldPathRichText; public string FieldPathRichText;
public string Type; public string Type;
@ -124,7 +141,7 @@ namespace Module.ProjectValidator.Editor
public bool Filter(string filter) public bool Filter(string filter)
{ {
return AssetName.Contains(filter, StringComparison.InvariantCultureIgnoreCase) || return AssetName.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
ScenePath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) || RelativePath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
FieldPath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) || FieldPath.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
Type.Contains(filter, StringComparison.InvariantCultureIgnoreCase) || Type.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||
SeverityResult.Contains(filter, StringComparison.InvariantCultureIgnoreCase) || SeverityResult.Contains(filter, StringComparison.InvariantCultureIgnoreCase) ||

View file

@ -31,9 +31,16 @@ namespace Module.ProjectValidator.Editor
if (!IsFieldSerializable(fi)) if (!IsFieldSerializable(fi))
continue; continue;
var attributes = fi.GetCustomAttributes(); var fieldAttributes = fi.GetCustomAttributes();
var fieldTypeAttributes = fi.FieldType.GetCustomAttributes();
foreach (var attribute in attributes) foreach (var attribute in fieldAttributes)
{
if (validatorList.TryGetAttributeValidator(attribute.GetType(), out var validator))
entry.AddField(fi, attribute, validator);
}
foreach (var attribute in fieldTypeAttributes)
{ {
if (validatorList.TryGetAttributeValidator(attribute.GetType(), out var validator)) if (validatorList.TryGetAttributeValidator(attribute.GetType(), out var validator))
entry.AddField(fi, attribute, validator); entry.AddField(fi, attribute, validator);
@ -164,7 +171,7 @@ namespace Module.ProjectValidator.Editor
return Components == null && Fields == null && Entries == null; return Components == null && Fields == null && Entries == null;
} }
} }
public sealed class ValidatorField public sealed class ValidatorField
{ {
public readonly FieldInfo FieldInfo; public readonly FieldInfo FieldInfo;

View file

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using UnityEngine; using UnityEngine;
@ -10,6 +11,8 @@ namespace Module.ProjectValidator.Editor
{ {
private readonly Dictionary<Type, object> _attributeValidators = new(); private readonly Dictionary<Type, object> _attributeValidators = new();
private readonly Dictionary<Type, List<object>> _componentValidators = new(); private readonly Dictionary<Type, List<object>> _componentValidators = new();
public readonly List<IGameObjectValidator> GameObjectValidators = new();
public readonly Dictionary<Type, List<AssetValidator>> AssetValidators = new();
public void AddAttribute(Type type) public void AddAttribute(Type type)
{ {
@ -59,6 +62,49 @@ namespace Module.ProjectValidator.Editor
} }
} }
public void AddGameObject(Type type)
{
if (type.IsInterface || type.IsAbstract)
return;
try
{
var instance = (IGameObjectValidator)FormatterServices.GetUninitializedObject(type);
GameObjectValidators.Add(instance);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
public void AddAsset(Type type)
{
if (type.IsInterface || type.IsAbstract)
return;
var typeValidator = type.GetInterfaces().FirstOrDefault(typeInterface => typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IAssetValidator<>));
var componentType = typeValidator?.GetGenericArguments()[0];
if (componentType == null)
return;
try
{
var instance = FormatterServices.GetUninitializedObject(type);
var validator = new AssetValidator(instance);
if (AssetValidators.TryGetValue(componentType, out var list))
list.Add(validator);
else
AssetValidators.Add(componentType, new List<AssetValidator> { validator });
}
catch (Exception e)
{
Debug.LogException(e);
}
}
public bool TryGetAttributeValidator(Type type, out object validatorInstance) public bool TryGetAttributeValidator(Type type, out object validatorInstance)
{ {
return _attributeValidators.TryGetValue(type, out validatorInstance); return _attributeValidators.TryGetValue(type, out validatorInstance);
@ -68,5 +114,17 @@ namespace Module.ProjectValidator.Editor
{ {
return _componentValidators.TryGetValue(type, out validatorInstances); return _componentValidators.TryGetValue(type, out validatorInstances);
} }
public sealed class AssetValidator
{
public readonly object Validator;
public readonly MethodInfo ValidatorMethod;
public AssetValidator(object validator)
{
Validator = validator;
ValidatorMethod = validator.GetType().GetMethod("Validate");
}
}
} }
} }

View file

@ -1,27 +1,36 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using UnityEditor.UIElements; using UnityEditor.UIElements;
using UnityEditorInternal; using UnityEditorInternal;
using UnityEngine.Pool;
using Object = UnityEngine.Object;
namespace Module.ProjectValidator.Editor namespace Module.ProjectValidator.Editor
{ {
internal sealed class ProjectValidatorSettings : ScriptableObject internal sealed class ProjectValidatorSettings : ScriptableObject
{ {
public List<string> assemblies = new(); public List<string> assemblies = new();
[NonReorderable]
public List<ValidatorEnabled> validators = new();
public const string MenuPath = "Project/Project Validator";
private const string AssetPath = "ProjectSettings/ProjectValidatorSettings.asset"; private const string AssetPath = "ProjectSettings/ProjectValidatorSettings.asset";
private const string StyleSheetName = "StyleSheetProjectValidatorSettings"; private const string StyleSheetName = "StyleSheetProjectValidatorSettings";
internal static ProjectValidatorSettings GetOrCreate() internal static ProjectValidatorSettings GetOrCreate()
{ {
var objects = InternalEditorUtility.LoadSerializedFileAndForget(AssetPath); var objects = InternalEditorUtility.LoadSerializedFileAndForget(AssetPath);
ProjectValidatorSettings settings;
if (objects.Length != 0) if (objects.Length != 0)
return (ProjectValidatorSettings)objects[0]; settings = (ProjectValidatorSettings)objects[0];
else
settings = CreateInstance<ProjectValidatorSettings>();
var settings = CreateInstance<ProjectValidatorSettings>(); PopulateValidatorList(settings);
InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { settings }, AssetPath, true); InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { settings }, AssetPath, true);
return settings; return settings;
} }
@ -29,7 +38,7 @@ namespace Module.ProjectValidator.Editor
[SettingsProvider] [SettingsProvider]
public static SettingsProvider CreateProvider() public static SettingsProvider CreateProvider()
{ {
return new SettingsProvider("Project/Project Validator", SettingsScope.Project) return new SettingsProvider(MenuPath, SettingsScope.Project)
{ {
label = "Project Validator", label = "Project Validator",
activateHandler = (_, root) => activateHandler = (_, root) =>
@ -37,16 +46,79 @@ namespace Module.ProjectValidator.Editor
var settings = GetOrCreate(); var settings = GetOrCreate();
var serializedObject = new SerializedObject(settings); var serializedObject = new SerializedObject(settings);
var container = new VisualElement { style = { flexDirection = FlexDirection.Column } }; var container = new VisualElement { style = { flexDirection = FlexDirection.Column } };
var propertyField = new PropertyField(serializedObject.FindProperty("assemblies"), "Assemblies");
propertyField.RegisterCallback<ChangeEvent<string>>(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true)); var assemblyField = new PropertyField(serializedObject.FindProperty(nameof(assemblies)), "Assemblies");
propertyField.RegisterValueChangeCallback(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true)); container.Add(assemblyField);
container.Add(propertyField);
var enabledField = new PropertyField(serializedObject.FindProperty(nameof(validators)), "Validators");
container.Add(enabledField);
root.Add(container); root.Add(container);
root.Bind(serializedObject); root.Bind(serializedObject);
root.RegisterCallback<SerializedObjectChangeEvent>(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true));
root.RegisterCallback<SerializedPropertyChangeEvent>(_ => InternalEditorUtility.SaveToSerializedFileAndForget(new[] { serializedObject.targetObject }, AssetPath, true));
root.styleSheets.Add(EditorAssetUtility.LoadFirstAsset<StyleSheet>(StyleSheetName)); root.styleSheets.Add(EditorAssetUtility.LoadFirstAsset<StyleSheet>(StyleSheetName));
}, },
keywords = new HashSet<string>(new[] { "Project", "Validator", "Assemblies" }) keywords = new HashSet<string>(new[] { "Project", "Validator", "Assemblies" })
}; };
} }
private static void PopulateValidatorList(ProjectValidatorSettings settings)
{
using var pool0 = ListPool<string>.Get(out var list);
using var pool1 = ListPool<string>.Get(out var temp);
FetchValidatorsOfType(typeof(IAssetValidator<>), list);
FetchValidatorsOfType(typeof(IAttributeValidator<>), list);
FetchValidatorsOfType(typeof(IComponentValidator<>), list);
FetchValidatorsOfType(typeof(IGameObjectValidator), list);
for (var i = 0; i < settings.validators.Count; i++)
{
temp.Add(settings.validators[i].assemblyQualifiedName);
}
for (var i = 0; i < list.Count; i++)
{
if (temp.Contains(list[i]))
continue;
var type = Type.GetType(list[i]);
var name = type != null ? type.Name : "Unknown Type";
settings.validators.Add(new ValidatorEnabled(name, list[i], true));
}
for (var i = temp.Count - 1; i >= 0; i--)
{
if (!list.Contains(temp[i]))
settings.validators.RemoveAt(i);
}
}
private static void FetchValidatorsOfType(Type type, List<string> typeNames)
{
var types = TypeCache.GetTypesDerivedFrom(type);
for (var i = 0; i < types.Count; i++)
{
if (!types[i].IsInterface && !types[i].IsAbstract)
typeNames.Add(types[i].AssemblyQualifiedName);
}
}
[Serializable]
public sealed class ValidatorEnabled
{
public string name;
public string assemblyQualifiedName;
public bool enabled;
public ValidatorEnabled(string name, string assemblyQualifiedName, bool enabled)
{
this.name = name;
this.assemblyQualifiedName = assemblyQualifiedName;
this.enabled = enabled;
}
}
} }
} }

View file

@ -1,7 +1,9 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object;
namespace Module.ProjectValidator.Editor namespace Module.ProjectValidator.Editor
{ {
@ -29,22 +31,39 @@ namespace Module.ProjectValidator.Editor
return list.ToArray(); return list.ToArray();
} }
public static Object[] LoadAllAssets(Type type)
{
var guids = AssetDatabase.FindAssetGUIDs($"a:assets t:{type.Name}");
var list = new List<Object>(guids.Length);
foreach (var guid in guids)
{
var asset = AssetDatabase.LoadAssetByGUID(guid, type);
if (asset != null)
list.Add(asset);
}
return list.ToArray();
}
internal static GUID GetAssetGuid(Object obj) internal static GUID GetAssetGuid(Object obj)
{ {
var assetGuid = new GUID(); var assetPath = string.Empty;
if (obj is GameObject gameObject) if (obj is GameObject gameObject)
{ {
if (gameObject.scene.isLoaded) if (gameObject.scene.isLoaded)
GUID.TryParse(AssetDatabase.AssetPathToGUID(gameObject.scene.path), out assetGuid); assetPath = gameObject.scene.path;
else if (PrefabUtility.IsPartOfPrefabAsset(gameObject)) else if (PrefabUtility.IsPartOfPrefabAsset(gameObject))
GUID.TryParse(AssetDatabase.AssetPathToGUID(gameObject.scene.path), out assetGuid); assetPath = AssetDatabase.GetAssetPath(gameObject);
} }
else else
{ {
GUID.TryParse(AssetDatabase.GetAssetPath(obj), out assetGuid); assetPath = AssetDatabase.GetAssetPath(obj);
} }
GUID.TryParse(AssetDatabase.AssetPathToGUID(assetPath), out var assetGuid);
return assetGuid; return assetGuid;
} }

View file

@ -37,6 +37,30 @@ namespace Module.ProjectValidator.Editor
window.titleContent = new GUIContent("Project Validator"); window.titleContent = new GUIContent("Project Validator");
return window; return window;
} }
public static string GetAssetValidatorName(object validator)
{
var str = validator.GetType().Name;
str = str.Replace("AssetValidator", string.Empty);
str = ObjectNames.NicifyVariableName(str);
return str;
}
internal static string GetGameObjectValidatorName(IGameObjectValidator validator)
{
var str = validator.GetType().Name;
str = str.Replace("GameObjectValidator", string.Empty);
str = ObjectNames.NicifyVariableName(str);
return str;
}
internal static string GetComponentValidatorShortName(object obj)
{
var str = obj.GetType().Name;
str = str.Replace("ComponentValidator", string.Empty);
str = ObjectNames.NicifyVariableName(str);
return str;
}
internal static string GetAttributeShortName(Attribute attribute) internal static string GetAttributeShortName(Attribute attribute)
{ {
@ -45,23 +69,18 @@ namespace Module.ProjectValidator.Editor
str = ObjectNames.NicifyVariableName(str); str = ObjectNames.NicifyVariableName(str);
return str; return str;
} }
internal static string GetComponentValidatorShortName(object obj) internal static void AppendToRelativePath(GameObject gameObject, ref string relativePath, bool initial)
{ {
var str = obj.GetType().Name; if (string.IsNullOrEmpty(relativePath))
str = str.Replace("ComponentValidator", string.Empty); relativePath = gameObject.name;
str = ObjectNames.NicifyVariableName(str); else
return str; relativePath = initial ? $"{relativePath}{gameObject.name}" : $"{relativePath}/{gameObject.name}";
}
internal static void AppendToScenePath(GameObject gameObject, ref string scenePath)
{
scenePath = string.IsNullOrEmpty(scenePath) ? gameObject.name : $"{scenePath}/{gameObject.name}";
} }
internal static string ApplyRichTextToScenePath(string scenePath) internal static string ApplyRichTextToRelativePath(string relativePath)
{ {
return scenePath.Replace("/", "<color=#00ff00><b>/</b></color>"); return relativePath.Replace("/", "<color=#00ff00><b>/</b></color>");
} }
public static void AppendToFieldPath(FieldInfo fieldInfo, ref string fieldPath) public static void AppendToFieldPath(FieldInfo fieldInfo, ref string fieldPath)
@ -97,7 +116,7 @@ namespace Module.ProjectValidator.Editor
{ {
var scene = SceneManager.GetSceneByPath(assetPath); var scene = SceneManager.GetSceneByPath(assetPath);
if (scene.isLoaded && TryFindSceneObjectByPath(scene, entry.ScenePath, out var gameObject)) if (scene.isLoaded && TryFindSceneObjectByPath(scene, entry.RelativePath, out var gameObject))
EditorGUIUtility.PingObject(gameObject); EditorGUIUtility.PingObject(gameObject);
else else
EditorGUIUtility.PingObject(asset); EditorGUIUtility.PingObject(asset);
@ -108,14 +127,14 @@ namespace Module.ProjectValidator.Editor
} }
} }
private static bool TryFindSceneObjectByPath(Scene scene, string scenePath, out GameObject gameObject) private static bool TryFindSceneObjectByPath(Scene scene, string relativePath, out GameObject gameObject)
{ {
using var _ = ListPool<GameObject>.Get(out var rootObjects); using var _ = ListPool<GameObject>.Get(out var rootObjects);
scene.GetRootGameObjects(rootObjects); scene.GetRootGameObjects(rootObjects);
var index = scenePath.IndexOf('/'); var index = relativePath.IndexOf('/');
var rootName = index != -1 ? scenePath[..index] : scenePath; var rootName = index != -1 ? relativePath[..index] : relativePath;
var childPath = index != -1 ? scenePath[(index + 1)..] : string.Empty; var childPath = index != -1 ? relativePath[(index + 1)..] : string.Empty;
for (var i = 0; i < rootObjects.Count; i++) for (var i = 0; i < rootObjects.Count; i++)
{ {
@ -185,6 +204,93 @@ namespace Module.ProjectValidator.Editor
} }
} }
#if UNITY_6000_4_OR_NEWER
internal static void RebuildSceneInstanceMapping(Report report, Dictionary<EntityId, Report.MappingEntry> dictMapping)
{
dictMapping.Clear();
using var _ = ListPool<GameObject>.Get(out var rootObjects);
for (var i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
if (!scene.isLoaded)
continue;
var strAssetGuid = AssetDatabase.AssetPathToGUID(scene.path);
GUID.TryParse(strAssetGuid, out var assetGuid);
scene.GetRootGameObjects(rootObjects);
for (var j = 0; j < rootObjects.Count; j++)
{
var rootObject = rootObjects[j];
var relativePath = string.Empty;
RebuildSceneInstanceMapping(report, dictMapping, rootObject, assetGuid, relativePath, true);
}
}
RebuildForAllParents(dictMapping);
}
private static void RebuildSceneInstanceMapping(Report report, Dictionary<EntityId, Report.MappingEntry> dictMapping, GameObject gameObject, GUID assetGuid, string relativePath, bool initial)
{
var transform = gameObject.transform;
AppendToRelativePath(gameObject, ref relativePath, initial);
if (report.TryGetSeverityFor(assetGuid, relativePath, out var mapping))
dictMapping.Add(gameObject.GetEntityId(), new Report.MappingEntry(mapping.Severity, false));
for (var i = 0; i < transform.childCount; i++)
{
RebuildSceneInstanceMapping(report, dictMapping, transform.GetChild(i).gameObject, assetGuid, relativePath, false);
}
}
private static void RebuildForAllParents(Dictionary<EntityId, Report.MappingEntry> dictMapping)
{
using var _ = DictionaryPool<EntityId, Report.MappingEntry>.Get(out var newMappings);
foreach (var pair in dictMapping)
{
var obj = EditorUtility.EntityIdToObject(pair.Key);
if (obj is not GameObject gameObject)
continue;
var severity = pair.Value.Severity;
var transform = gameObject.transform.parent;
while (transform != null)
{
gameObject = transform.gameObject;
var entityId = gameObject.GetEntityId();
if (dictMapping.TryGetValue(entityId, out var parentMapping))
{
if (severity < parentMapping.Severity)
severity = parentMapping.Severity;
}
else if (newMappings.TryGetValue(entityId, out var currentMapping))
{
if (currentMapping.Severity < severity)
newMappings[entityId] = new Report.MappingEntry(severity, true);
}
else
{
newMappings.Add(entityId, new Report.MappingEntry(severity, true));
}
transform = transform.parent;
}
}
foreach (var pair in newMappings)
{
dictMapping.Add(pair.Key, pair.Value);
}
}
#else
internal static void RebuildSceneInstanceMapping(Report report, Dictionary<int, Report.MappingEntry> dictMapping) internal static void RebuildSceneInstanceMapping(Report report, Dictionary<int, Report.MappingEntry> dictMapping)
{ {
dictMapping.Clear(); dictMapping.Clear();
@ -204,25 +310,26 @@ namespace Module.ProjectValidator.Editor
for (var j = 0; j < rootObjects.Count; j++) for (var j = 0; j < rootObjects.Count; j++)
{ {
var rootObject = rootObjects[j]; var rootObject = rootObjects[j];
var scenePath = string.Empty; var relativePath = string.Empty;
RebuildSceneInstanceMapping(report, dictMapping, rootObject, assetGuid, scenePath); RebuildSceneInstanceMapping(report, dictMapping, rootObject, assetGuid, relativePath, true);
} }
} }
RebuildForAllParents(dictMapping); RebuildForAllParents(dictMapping);
} }
private static void RebuildSceneInstanceMapping(Report report, Dictionary<int, Report.MappingEntry> dictMapping, GameObject gameObject, GUID assetGuid, string scenePath)
private static void RebuildSceneInstanceMapping(Report report, Dictionary<int, Report.MappingEntry> dictMapping, GameObject gameObject, GUID assetGuid, string relativePath, bool initial)
{ {
var transform = gameObject.transform; var transform = gameObject.transform;
AppendToScenePath(gameObject, ref scenePath); AppendToRelativePath(gameObject, ref relativePath, initial);
if (report.TryGetSeverityFor(assetGuid, scenePath, out var mapping)) if (report.TryGetSeverityFor(assetGuid, relativePath, out var mapping))
dictMapping.Add(gameObject.GetInstanceID(), new Report.MappingEntry(mapping.Severity, false)); dictMapping.Add(gameObject.GetInstanceID(), new Report.MappingEntry(mapping.Severity, false));
for (var i = 0; i < transform.childCount; i++) for (var i = 0; i < transform.childCount; i++)
{ {
RebuildSceneInstanceMapping(report, dictMapping, transform.GetChild(i).gameObject, assetGuid, scenePath); RebuildSceneInstanceMapping(report, dictMapping, transform.GetChild(i).gameObject, assetGuid, relativePath, false);
} }
} }
@ -269,6 +376,7 @@ namespace Module.ProjectValidator.Editor
dictMapping.Add(pair.Key, pair.Value); dictMapping.Add(pair.Key, pair.Value);
} }
} }
#endif
internal static void RefreshUnityWindows() internal static void RefreshUnityWindows()
{ {

View file

@ -13,7 +13,6 @@ namespace Module.ProjectValidator.Editor
{ {
internal static class ValidatorRunner internal static class ValidatorRunner
{ {
private static bool _initialized;
private static ValidatorList _validatorList; private static ValidatorList _validatorList;
private static TypeTree _typeTree; private static TypeTree _typeTree;
@ -30,6 +29,7 @@ namespace Module.ProjectValidator.Editor
var report = new Report(); var report = new Report();
ValidateAllScenes(report); ValidateAllScenes(report);
ValidateAllAssets(report); ValidateAllAssets(report);
ValidateAllPrefabs(report);
report.RebuildAssetMapping(); report.RebuildAssetMapping();
report.RebuildInstanceMapping(); report.RebuildInstanceMapping();
report.SetAsActive(); report.SetAsActive();
@ -40,7 +40,7 @@ namespace Module.ProjectValidator.Editor
ProjectValidatorUtility.OpenWindow(); ProjectValidatorUtility.OpenWindow();
stopwatch.Stop(); stopwatch.Stop();
Debug.Log(stopwatch.Elapsed.TotalMilliseconds + "ms"); Debug.Log($"Validator took {stopwatch.Elapsed.TotalMilliseconds}ms");
return true; return true;
} }
@ -55,20 +55,19 @@ namespace Module.ProjectValidator.Editor
private static void Initialize() private static void Initialize()
{ {
if (_initialized)
return;
var settings = ProjectValidatorSettings.GetOrCreate(); var settings = ProjectValidatorSettings.GetOrCreate();
var assemblies = GetAssembliesFrom(settings); var assemblies = GetAssembliesFrom(settings);
var enabled = GetEnabledValidators(settings);
_validatorList = new ValidatorList(); _validatorList = new ValidatorList();
_typeTree = new TypeTree(); _typeTree = new TypeTree();
FetchAllAttributeValidators(); FetchAllGameObjectValidators(enabled);
FetchAllComponentValidators(); FetchAllComponentValidators(enabled);
FetchAllAttributeValidators(enabled);
FetchAllAssetValidators(enabled);
FetchAllTypesWithValidators<Component>(assemblies); FetchAllTypesWithValidators<Component>(assemblies);
FetchAllTypesWithValidators<ScriptableObject>(assemblies); FetchAllTypesWithValidators<ScriptableObject>(assemblies);
_initialized = true;
} }
private static Assembly[] GetAssembliesFrom(ProjectValidatorSettings settings) private static Assembly[] GetAssembliesFrom(ProjectValidatorSettings settings)
@ -91,23 +90,72 @@ namespace Module.ProjectValidator.Editor
return assemblies.ToArray(); return assemblies.ToArray();
} }
private static void FetchAllAttributeValidators() private static HashSet<Type> GetEnabledValidators(ProjectValidatorSettings settings)
{
var enabled = new HashSet<Type>(settings.validators.Count);
for (var i = 0; i < settings.validators.Count; i++)
{
try
{
if (!settings.validators[i].enabled)
continue;
var type = Type.GetType(settings.validators[i].assemblyQualifiedName);
if (type != null)
enabled.Add(type);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return enabled;
}
private static void FetchAllAttributeValidators(HashSet<Type> enabled)
{ {
var types = TypeCache.GetTypesDerivedFrom(typeof(IAttributeValidator<>)); var types = TypeCache.GetTypesDerivedFrom(typeof(IAttributeValidator<>));
for (var i = 0; i < types.Count; i++) for (var i = 0; i < types.Count; i++)
{ {
_validatorList.AddAttribute(types[i]); if (enabled.Contains(types[i]))
_validatorList.AddAttribute(types[i]);
}
}
private static void FetchAllGameObjectValidators(HashSet<Type> enabled)
{
var types = TypeCache.GetTypesDerivedFrom(typeof(IGameObjectValidator));
for (var i = 0; i < types.Count; i++)
{
if (enabled.Contains(types[i]))
_validatorList.AddGameObject(types[i]);
} }
} }
private static void FetchAllComponentValidators() private static void FetchAllComponentValidators(HashSet<Type> enabled)
{ {
var types = TypeCache.GetTypesDerivedFrom(typeof(IComponentValidator<>)); var types = TypeCache.GetTypesDerivedFrom(typeof(IComponentValidator<>));
for (var i = 0; i < types.Count; i++) for (var i = 0; i < types.Count; i++)
{ {
_validatorList.AddComponent(types[i]); if (enabled.Contains(types[i]))
_validatorList.AddComponent(types[i]);
}
}
private static void FetchAllAssetValidators(HashSet<Type> enabled)
{
var types = TypeCache.GetTypesDerivedFrom(typeof(IAssetValidator<>));
for (var i = 0; i < types.Count; i++)
{
if (enabled.Contains(types[i]))
_validatorList.AddAsset(types[i]);
} }
} }
@ -131,12 +179,12 @@ namespace Module.ProjectValidator.Editor
for (var i = 0; i < assets.Length; i++) for (var i = 0; i < assets.Length; i++)
{ {
var assetPath = AssetDatabase.GetAssetPath(assets[i]);
var scene = SceneManager.GetSceneByPath(assetPath);
var isLoaded = scene.isLoaded;
try try
{ {
var assetPath = AssetDatabase.GetAssetPath(assets[i]);
var scene = SceneManager.GetSceneByPath(assetPath);
var isLoaded = scene.isLoaded;
if (!isLoaded) if (!isLoaded)
scene = EditorSceneManager.OpenScene(assetPath, OpenSceneMode.Additive); scene = EditorSceneManager.OpenScene(assetPath, OpenSceneMode.Additive);
@ -144,22 +192,60 @@ namespace Module.ProjectValidator.Editor
for (var j = 0; j < rootObjects.Count; j++) for (var j = 0; j < rootObjects.Count; j++)
{ {
ValidateGameObject(rootObjects[j], string.Empty, report); ValidateGameObject(rootObjects[j], string.Empty, report, true);
} }
if (!isLoaded)
EditorSceneManager.CloseScene(scene, true);
} }
catch (Exception e) catch (Exception e)
{ {
Debug.LogException(e); Debug.LogException(e);
} }
finally
{
if (!isLoaded && scene.isLoaded)
EditorSceneManager.CloseScene(scene, true);
}
} }
} }
private static void ValidateAllAssets(Report report) private static void ValidateAllAssets(Report report)
{ {
ValidateAssetsBytype<ScriptableObject>(report); ValidateAssetsBytype<ScriptableObject>(report);
foreach (var pair in _validatorList.AssetValidators)
{
var assets = EditorAssetUtility.LoadAllAssets(pair.Key);
for (var i = 0; i < assets.Length; i++)
{
try
{
var assetPath = AssetDatabase.GetAssetPath(assets[i]);
var assetGuid = AssetDatabase.GUIDFromAssetPath(assetPath);
ValidateAsset(assets[i], assetGuid, assetPath, pair.Value, report);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
}
private static void ValidateAllPrefabs(Report report)
{
var assets = EditorAssetUtility.LoadAllAssets<GameObject>();
for (var i = 0; i < assets.Length; i++)
{
try
{
ValidateGameObject(assets[i], string.Empty, report, true);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
} }
private static void ValidateAssetsBytype<T>(Report report) where T : UnityEngine.Object private static void ValidateAssetsBytype<T>(Report report) where T : UnityEngine.Object
@ -182,39 +268,83 @@ namespace Module.ProjectValidator.Editor
private static void ValidateUnityObject(UnityEngine.Object obj, Report report) private static void ValidateUnityObject(UnityEngine.Object obj, Report report)
{ {
var assetGuid = EditorAssetUtility.ObjectToAssetGuid(obj); var assetGuid = EditorAssetUtility.ObjectToAssetGuid(obj);
Validate(assetGuid, string.Empty, obj, report); var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
Validate(assetGuid, assetPath, obj, report);
} }
private static void ValidateGameObject(GameObject gameObject, string scenePath, Report report) private static void ValidateGameObject(GameObject gameObject, string relativePath, Report report, bool initial)
{ {
ProjectValidatorUtility.AppendToScenePath(gameObject, ref scenePath); ProjectValidatorUtility.AppendToRelativePath(gameObject, ref relativePath, initial);
ValidateComponents(gameObject, scenePath, report);
ValidateChildren(gameObject, scenePath, report); var assetGuid = EditorAssetUtility.GetAssetGuid(gameObject);
using var _ = ListPool<ValidatorResult>.Get(out var results);
for (var i = 0; i < _validatorList.GameObjectValidators.Count; i++)
{
results.Clear();
_validatorList.GameObjectValidators[i].Validate(gameObject, results);
var type = ProjectValidatorUtility.GetGameObjectValidatorName(_validatorList.GameObjectValidators[i]);
for (var j = 0; j < results.Count; j++)
{
var result = results[j];
if (result.Severity != EValidatorSeverity.Valid)
report.Add(assetGuid, relativePath, string.Empty, type, result.Severity, result.Message);
}
}
ValidateComponents(gameObject, assetGuid, relativePath, report);
ValidateChildren(gameObject, relativePath, report);
} }
private static void ValidateComponents(GameObject gameObject, string scenePath, Report report) private static void ValidateComponents(GameObject gameObject, GUID assetGuid, string relativePath, Report report)
{ {
using var _ = ListPool<Component>.Get(out var components); using var _ = ListPool<Component>.Get(out var components);
var assetGuid = EditorAssetUtility.GetAssetGuid(gameObject);
gameObject.GetComponents(components); gameObject.GetComponents(components);
for (var i = 0; i < components.Count; i++) for (var i = 0; i < components.Count; i++)
{ {
Validate(assetGuid, scenePath, components[i], report); if (components[i] != null)
Validate(assetGuid, relativePath, components[i], report);
}
}
private static void ValidateAsset(UnityEngine.Object obj, GUID assetGuid, string relativePath, List<ValidatorList.AssetValidator> validators, Report report)
{
using var _ = ListPool<ValidatorResult>.Get(out var results);
for (var i = 0; i < validators.Count; i++)
{
results.Clear();
var validator = validators[i];
validator.ValidatorMethod.Invoke(validator.Validator, new object[] { obj, results });
for (var j = 0; j < results.Count; j++)
{
var result = results[j];
if (result.Severity == EValidatorSeverity.Valid)
continue;
var validatorName = ProjectValidatorUtility.GetAssetValidatorName(validator.Validator);
report.Add(assetGuid, relativePath, string.Empty, validatorName, result.Severity, result.Message);
}
} }
} }
private static void ValidateChildren(GameObject gameObject, string scenePath, Report report) private static void ValidateChildren(GameObject gameObject, string relativePath, Report report)
{ {
var transform = gameObject.transform; var transform = gameObject.transform;
for (var i = 0; i < transform.childCount; i++) for (var i = 0; i < transform.childCount; i++)
{ {
ValidateGameObject(transform.GetChild(i).gameObject, scenePath, report); ValidateGameObject(transform.GetChild(i).gameObject, relativePath, report, false);
} }
} }
private static void Validate(GUID assetGuid, string scenePath, object obj, Report report) private static void Validate(GUID assetGuid, string relativePath, object obj, Report report)
{ {
var type = obj.GetType(); var type = obj.GetType();
@ -222,10 +352,10 @@ namespace Module.ProjectValidator.Editor
return; return;
var fieldPath = obj.GetType().Name; var fieldPath = obj.GetType().Name;
Validate(assetGuid, scenePath, fieldPath, obj, entry, report); Validate(assetGuid, relativePath, fieldPath, obj, entry, report);
} }
private static void Validate(GUID assetGuid, string scenePath, string parentFieldPath, object obj, TypeTree.Entry entry, Report report) private static void Validate(GUID assetGuid, string relativePath, string parentFieldPath, object obj, TypeTree.Entry entry, Report report)
{ {
if (obj == null) if (obj == null)
return; return;
@ -237,7 +367,7 @@ namespace Module.ProjectValidator.Editor
try try
{ {
var component = entry.Components[i]; var component = entry.Components[i];
ValidateComponent(component, obj, assetGuid, scenePath, report); ValidateComponent(component, obj, assetGuid, relativePath, report);
} }
catch (Exception e) catch (Exception e)
{ {
@ -266,13 +396,13 @@ namespace Module.ProjectValidator.Editor
{ {
var fieldPathArrElement = fieldPath; var fieldPathArrElement = fieldPath;
ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement); ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement);
ValidateField(field, eObj, assetGuid, scenePath, fieldPathArrElement, report); ValidateField(field, eObj, assetGuid, relativePath, fieldPathArrElement, report);
idx++; idx++;
} }
} }
else else
{ {
ValidateField(field, value, assetGuid, scenePath, fieldPath, report); ValidateField(field, value, assetGuid, relativePath, fieldPath, report);
} }
} }
catch (Exception e) catch (Exception e)
@ -293,8 +423,18 @@ namespace Module.ProjectValidator.Editor
var fieldPath = parentFieldPath; var fieldPath = parentFieldPath;
ProjectValidatorUtility.AppendToFieldPath(e.FieldInfo, ref fieldPath); ProjectValidatorUtility.AppendToFieldPath(e.FieldInfo, ref fieldPath);
if (value is IEnumerable<object> ie) if (value is Array arr)
{
for (var j = 0; j < arr.Length; j++)
{
var eObj = arr.GetValue(j);
var fieldPathArrElement = fieldPath;
ProjectValidatorUtility.AppendToFieldPath(j, ref fieldPathArrElement);
Validate(assetGuid, relativePath, fieldPathArrElement, eObj, e.Entry, report);
}
}
else if (value is IEnumerable<object> ie)
{ {
var idx = 0; var idx = 0;
@ -302,13 +442,13 @@ namespace Module.ProjectValidator.Editor
{ {
var fieldPathArrElement = fieldPath; var fieldPathArrElement = fieldPath;
ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement); ProjectValidatorUtility.AppendToFieldPath(idx, ref fieldPathArrElement);
Validate(assetGuid, scenePath, fieldPathArrElement, eObj, e.Entry, report); Validate(assetGuid, relativePath, fieldPathArrElement, eObj, e.Entry, report);
idx++; idx++;
} }
} }
else else
{ {
Validate(assetGuid, scenePath, fieldPath, value, e.Entry, report); Validate(assetGuid, relativePath, fieldPath, value, e.Entry, report);
} }
} }
catch (Exception e) catch (Exception e)
@ -319,15 +459,15 @@ namespace Module.ProjectValidator.Editor
} }
} }
private static void ValidateField(TypeTree.ValidatorField field, object value, GUID assetGuid, string scenePath, string fieldPath, Report report) private static void ValidateField(TypeTree.ValidatorField field, object value, GUID assetGuid, string relativePath, string fieldPath, Report report)
{ {
var result = (ValidatorResult)field.ValidatorMethod.Invoke(field.Validator, new[] { field.Attribute, value }); var result = (ValidatorResult)field.ValidatorMethod.Invoke(field.Validator, new[] { field.Attribute, value });
if (result.Severity != EValidatorSeverity.Valid) if (result.Severity != EValidatorSeverity.Valid)
report.Add(assetGuid, scenePath, fieldPath, field.Attribute, result.Severity, result.Message); report.Add(assetGuid, relativePath, fieldPath, field.Attribute, result.Severity, result.Message);
} }
private static void ValidateComponent(TypeTree.ValidatorComponent component, object value, GUID assetGuid, string scenePath, Report report) private static void ValidateComponent(TypeTree.ValidatorComponent component, object value, GUID assetGuid, string relativePath, Report report)
{ {
using var _ = ListPool<ValidatorResult>.Get(out var results); using var _ = ListPool<ValidatorResult>.Get(out var results);
component.ValidatorMethod.Invoke(component.Validator, new[] { value, results }); component.ValidatorMethod.Invoke(component.Validator, new[] { value, results });
@ -338,7 +478,7 @@ namespace Module.ProjectValidator.Editor
var result = results[i]; var result = results[i];
if (result.Severity != EValidatorSeverity.Valid) if (result.Severity != EValidatorSeverity.Valid)
report.Add(assetGuid, scenePath, string.Empty, type, result.Severity, result.Message); report.Add(assetGuid, relativePath, string.Empty, type, result.Severity, result.Message);
} }
} }
} }

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ddbf30cd5a674751be0c125c1f4e917b
timeCreated: 1779623970

View file

@ -0,0 +1,47 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace Module.ProjectValidator.Editor
{
internal sealed class AssetValidatorMaterialShader : IAssetValidator<Material>
{
public void Validate(Material obj, List<ValidatorResult> results)
{
if (obj.shader == null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "Shader is Null"));
else if (!IsCompatible(obj.shader))
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"Shader '{obj.shader.name}' is not compatible with render pipeline"));
else if (!obj.shader.isSupported)
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"Shader '{obj.shader.name}' is not supported"));
else if (ShaderUtil.ShaderHasError(obj.shader))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Shader '{obj.shader.name}' has compile errors"));
}
private static bool IsCompatible(Shader shader)
{
var pipeline = GraphicsSettings.currentRenderPipeline;
if (pipeline == null)
return true;
var tagSearch = new ShaderTagId("RenderPipeline");
var tagPipeline = new ShaderTagId(pipeline.renderPipelineShaderTag);
var hasKeyword = false;
for (var i = 0; i < shader.passCount; i++)
{
var tagPass = shader.FindPassTagValue(i, tagSearch);
if (tagPass != ShaderTagId.none)
hasKeyword = true;
if (tagPass == tagPipeline)
return true;
}
return !hasKeyword;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2218d247daff44bf84629756b63ea650
timeCreated: 1779623986

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace Module.ProjectValidator.Editor
{
internal sealed class AssetValidatorMaterialTexture : IAssetValidator<Material>
{
public void Validate(Material obj, List<ValidatorResult> results)
{
if (obj.shader == null)
return;
var count = obj.shader.GetPropertyCount();
for (var i = 0; i < count; i++)
{
var propertyType = obj.shader.GetPropertyType(i);
var propertyFlags = obj.shader.GetPropertyFlags(i);
if (propertyType != ShaderPropertyType.Texture)
continue;
if ((propertyFlags & (ShaderPropertyFlags.PerRendererData | ShaderPropertyFlags.HideInInspector | ShaderPropertyFlags.NonModifiableTextureData)) != 0)
continue;
var propertyName = obj.shader.GetPropertyName(i);
var propertyValue = obj.GetTexture(propertyName);
if (propertyValue == null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"Texture property '{propertyName}' is Null"));
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cfeb4b29a9cd4ea79c1e9325b8122e17
timeCreated: 1779627240

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0aa1d741049f4074bb863b76ed604d07
timeCreated: 1779214748

View file

@ -0,0 +1,23 @@
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class AttributeValidatorMin : IAttributeValidator<MinAttribute>
{
public ValidatorResult Validate(MinAttribute attribute, object value)
{
if (value is int iValue)
{
if (iValue < attribute.min)
return ValidatorResult.Create(EValidatorSeverity.Error, $"Value {iValue} is less than minimum value of {attribute.min}");
}
else if (value is float fValue)
{
if (fValue < attribute.min)
return ValidatorResult.Create(EValidatorSeverity.Error, $"Value {fValue} is less than minimum value of {attribute.min}");
}
return ValidatorResult.Valid;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2015106b3c9b450e9efb1333e5239033
timeCreated: 1779214761

View file

@ -0,0 +1,12 @@
using System;
namespace Module.ProjectValidator.Editor
{
internal sealed class AttributeValidatorObsolete : IAttributeValidator<ObsoleteAttribute>
{
public ValidatorResult Validate(ObsoleteAttribute attribute, object value)
{
return ValidatorResult.Create(EValidatorSeverity.Error, "Obsolete");
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 982ac4e898cc2ca438e98a2f0034a8d3

View file

@ -0,0 +1,23 @@
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class AttributeValidatorRange : IAttributeValidator<RangeAttribute>
{
public ValidatorResult Validate(RangeAttribute attribute, object value)
{
if (value is int iValue)
{
if (iValue < attribute.min || iValue > attribute.max)
return ValidatorResult.Create(EValidatorSeverity.Error, $"Value {iValue} is not in the range [{attribute.min};{attribute.max}]");
}
else if (value is float fValue)
{
if (fValue < attribute.min || fValue > attribute.max)
return ValidatorResult.Create(EValidatorSeverity.Error, $"Value {fValue} is not in the range [{attribute.min};{attribute.max}]");
}
return ValidatorResult.Valid;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3115c26bccfe4b1e964f784a3f9fdd55
timeCreated: 1779214908

View file

@ -0,0 +1,22 @@
using System.Collections.Generic;
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class ComponentValidatorSkinnedMeshRenderer : IComponentValidator<SkinnedMeshRenderer>
{
public void Validate(SkinnedMeshRenderer component, List<ValidatorResult> results)
{
var materials = component.sharedMaterials;
for (var i = 0; i < materials.Length; i++)
{
if (materials[i] == null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Missing material in slot #{i}"));
}
if (component.sharedMesh == null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "Missing mesh"));
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1610561c53a0aa84aaa903e5dde29694

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 45fa253dcfa349d5b1e9bd56ebac7c98
timeCreated: 1779133809

View file

@ -0,0 +1,15 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class GameObjectValidatorBrokenPrefab : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
if (PrefabUtility.IsPrefabAssetMissing(gameObject))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "GameObject is missing prefab asset"));
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8fd1dfcd3d564622a918e2175499318d
timeCreated: 1779133864

View file

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Reflection;
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 && IsMultipleComponentsAllowed(type))
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"GameObject has duplicate '{type.Name}' ({count}) components"));
type = t;
count = 1;
}
}
if (count > 1 && IsMultipleComponentsAllowed(type))
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"GameObject has duplicate '{type.Name}' ({count}) components"));
}
private static bool IsMultipleComponentsAllowed(Type type)
{
return type.GetCustomAttribute<DisallowMultipleComponent>(true) == null;
}
}
}

View file

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

View file

@ -0,0 +1,17 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class GameObjectValidatorMissingComponents : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
var count = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(gameObject);
if (count != 0)
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"GameObject is missing {count} component(s)"));
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 66cbc43729ec4e81a9e353e537b3ccf0
timeCreated: 1779133947

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

View file

@ -0,0 +1,28 @@
using System.Collections.Generic;
using UnityEngine;
namespace Module.ProjectValidator.Editor
{
internal sealed class GameObjectValidatorTransform : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
var transform = gameObject.transform;
var lp = transform.localPosition;
var lr = transform.localRotation;
var ls = transform.localScale;
if (IsInvalid(lp.x) || IsInvalid(lp.y) || IsInvalid(lp.z))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Local position '{lp}' is invalid"));
if (IsInvalid(lr.x) || IsInvalid(lr.y) || IsInvalid(lr.z) || IsInvalid(lr.w))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Local rotation '{lr}' is invalid"));
if (IsInvalid(ls.x) || IsInvalid(ls.y) || IsInvalid(ls.z))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, $"Local scale '{ls}' is invalid"));
}
private static bool IsInvalid(float value)
{
return float.IsNaN(value) || float.IsInfinity(value);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 727d5de59b004deb8c192337bcee132e
timeCreated: 1779628852

View file

@ -8,12 +8,18 @@ namespace Module.ProjectValidator.Editor
{ {
internal sealed class EditorProjectValidatorWindow : EditorWindow internal sealed class EditorProjectValidatorWindow : EditorWindow
{ {
private VisualElement _groupWarnings;
private Label _labelWarnings;
private VisualElement _groupErrors;
private Label _labelErrors;
private MultiColumnTreeView _treeView; private MultiColumnTreeView _treeView;
private string _searchFilter; private string _searchFilter;
private readonly List<TreeViewItemData<Report.Entry>> _list = new(); private readonly List<TreeViewItemData<Report.Entry>> _list = new();
private readonly List<TreeViewItemData<Report.Entry>> _filteredList = new(); private readonly List<TreeViewItemData<Report.Entry>> _filteredList = new();
public void CreateGUI() public void CreateGUI()
{ {
var root = rootVisualElement; var root = rootVisualElement;
@ -21,23 +27,33 @@ namespace Module.ProjectValidator.Editor
root.styleSheets.Add(EditorAssetUtility.LoadFirstAsset<StyleSheet>("StyleSheetEditorProjectValidatorWindow")); root.styleSheets.Add(EditorAssetUtility.LoadFirstAsset<StyleSheet>("StyleSheetEditorProjectValidatorWindow"));
root.Add(asset.Instantiate()); root.Add(asset.Instantiate());
_groupWarnings = root.Q<VisualElement>("status-warnings");
_labelWarnings = _groupWarnings.Q<Label>("label-warnings");
_groupErrors = root.Q<VisualElement>("status-errors");
_labelErrors = _groupErrors.Q<Label>("label-errors");
_groupWarnings.style.display = DisplayStyle.None;
_groupErrors.style.display = DisplayStyle.None;
root.Q<ToolbarButton>("button-run").clicked += OnToolbarButtonRunClicked; root.Q<ToolbarButton>("button-run").clicked += OnToolbarButtonRunClicked;
root.Q<ToolbarButton>("button-clear").clicked += OnToolbarButtonClearClicked; root.Q<ToolbarButton>("button-clear").clicked += OnToolbarButtonClearClicked;
root.Q<ToolbarSearchField>().RegisterValueChangedCallback(OnToolbarSearchFieldChanged); root.Q<ToolbarSearchField>().RegisterValueChangedCallback(OnToolbarSearchFieldChanged);
root.Q<ToolbarButton>("button-settings").clicked += OnToolbarButtonSettingsClicked;
_treeView = root.Q<MultiColumnTreeView>(); _treeView = root.Q<MultiColumnTreeView>();
_treeView.columns["asset"].makeCell = CreateObjectField; _treeView.columns["asset"].makeCell = CreateObjectField;
_treeView.columns["type"].makeCell = CreateLabel; _treeView.columns["type"].makeCell = CreateLabel;
_treeView.columns["severity"].makeCell = CreateLabel; _treeView.columns["severity"].makeCell = CreateLabel;
_treeView.columns["severity-message"].makeCell = CreateLabel; _treeView.columns["severity-message"].makeCell = CreateLabel;
_treeView.columns["scene-path"].makeCell = CreateLabel; _treeView.columns["relative-path"].makeCell = CreateLabel;
_treeView.columns["field-path"].makeCell = CreateLabel; _treeView.columns["field-path"].makeCell = CreateLabel;
_treeView.columns["asset"].bindCell = OnTreeViewBindCellAsset; _treeView.columns["asset"].bindCell = OnTreeViewBindCellAsset;
_treeView.columns["type"].bindCell = OnTreeViewBindCellType; _treeView.columns["type"].bindCell = OnTreeViewBindCellType;
_treeView.columns["severity"].bindCell = OnTreeViewBindCellSeverity; _treeView.columns["severity"].bindCell = OnTreeViewBindCellSeverity;
_treeView.columns["severity-message"].bindCell = OnTreeViewBindCellSeverityMessage; _treeView.columns["severity-message"].bindCell = OnTreeViewBindCellSeverityMessage;
_treeView.columns["scene-path"].bindCell = OnTreeViewBindCellScenePath; _treeView.columns["relative-path"].bindCell = OnTreeViewBindCellRelativePath;
_treeView.columns["field-path"].bindCell = OnTreeViewBindCellFieldPath; _treeView.columns["field-path"].bindCell = OnTreeViewBindCellFieldPath;
_treeView.columns["severity"].unbindCell = OnTreeViewUnbindCellSeverity; _treeView.columns["severity"].unbindCell = OnTreeViewUnbindCellSeverity;
@ -46,7 +62,7 @@ namespace Module.ProjectValidator.Editor
_treeView.columns["type"].comparison = OnTreeViewComparisonCellType; _treeView.columns["type"].comparison = OnTreeViewComparisonCellType;
_treeView.columns["severity"].comparison = OnTreeViewComparisonCellSeverity; _treeView.columns["severity"].comparison = OnTreeViewComparisonCellSeverity;
_treeView.columns["severity-message"].comparison = OnTreeViewComparisonCellSeverityMessage; _treeView.columns["severity-message"].comparison = OnTreeViewComparisonCellSeverityMessage;
_treeView.columns["scene-path"].comparison = OnTreeViewComparisonCellScenePath; _treeView.columns["relative-path"].comparison = OnTreeViewComparisonCellRelativePath;
_treeView.columns["field-path"].comparison = OnTreeViewComparisonCellFieldPath; _treeView.columns["field-path"].comparison = OnTreeViewComparisonCellFieldPath;
_treeView.selectionChanged += OnTreeViewSelectionChanged; _treeView.selectionChanged += OnTreeViewSelectionChanged;
@ -60,11 +76,26 @@ namespace Module.ProjectValidator.Editor
_list.Clear(); _list.Clear();
for (var i = 0; i < Report.Active.Entries.Count; i++) var entries = Report.Active.Entries;
var warningCount = 0;
var errorCount = 0;
for (var i = 0; i < entries.Count; i++)
{ {
_list.Add(new TreeViewItemData<Report.Entry>(i, Report.Active.Entries[i])); _list.Add(new TreeViewItemData<Report.Entry>(i, entries[i]));
if (entries[i].Severity == EValidatorSeverity.Warning)
warningCount++;
if (entries[i].Severity == EValidatorSeverity.Error)
errorCount++;
} }
_groupWarnings.style.display = warningCount > 0 ? DisplayStyle.Flex : DisplayStyle.None;
_labelWarnings.text = warningCount.ToString();
_groupErrors.style.display = errorCount > 0 ? DisplayStyle.Flex : DisplayStyle.None;
_labelErrors.text = errorCount.ToString();
Filter(); Filter();
} }
@ -123,9 +154,16 @@ namespace Module.ProjectValidator.Editor
private void OnToolbarButtonClearClicked() private void OnToolbarButtonClearClicked()
{ {
_groupWarnings.style.display = DisplayStyle.None;
_groupErrors.style.display = DisplayStyle.None;
ValidatorRunner.Clear(); ValidatorRunner.Clear();
} }
private void OnToolbarButtonSettingsClicked()
{
SettingsService.OpenProjectSettings(ProjectValidatorSettings.MenuPath);
}
private void OnToolbarSearchFieldChanged(ChangeEvent<string> evt) private void OnToolbarSearchFieldChanged(ChangeEvent<string> evt)
{ {
_searchFilter = evt.newValue; _searchFilter = evt.newValue;
@ -170,11 +208,11 @@ namespace Module.ProjectValidator.Editor
label.text = entry.SeverityResult; label.text = entry.SeverityResult;
} }
private void OnTreeViewBindCellScenePath(VisualElement ve, int index) private void OnTreeViewBindCellRelativePath(VisualElement ve, int index)
{ {
var label = (Label)ve; var label = (Label)ve;
var entry = _treeView.GetItemDataForIndex<Report.Entry>(index); var entry = _treeView.GetItemDataForIndex<Report.Entry>(index);
label.text = entry.ScenePathRichText; label.text = entry.RelativePathRichText;
} }
private void OnTreeViewBindCellFieldPath(VisualElement ve, int index) private void OnTreeViewBindCellFieldPath(VisualElement ve, int index)
@ -222,11 +260,11 @@ namespace Module.ProjectValidator.Editor
return string.Compare(entry0.SeverityResult, entry1.SeverityResult, StringComparison.Ordinal); return string.Compare(entry0.SeverityResult, entry1.SeverityResult, StringComparison.Ordinal);
} }
private int OnTreeViewComparisonCellScenePath(int index0, int index1) private int OnTreeViewComparisonCellRelativePath(int index0, int index1)
{ {
var entry0 = _treeView.GetItemDataForIndex<Report.Entry>(index0); var entry0 = _treeView.GetItemDataForIndex<Report.Entry>(index0);
var entry1 = _treeView.GetItemDataForIndex<Report.Entry>(index1); var entry1 = _treeView.GetItemDataForIndex<Report.Entry>(index1);
return string.Compare(entry0.ScenePath, entry1.ScenePath, StringComparison.Ordinal); return string.Compare(entry0.RelativePath, entry1.RelativePath, StringComparison.Ordinal);
} }
private int OnTreeViewComparisonCellFieldPath(int index0, int index1) private int OnTreeViewComparisonCellFieldPath(int index0, int index1)

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a22633e08b3d4c0fbbea654a8a8cd287
timeCreated: 1779637991

View file

@ -0,0 +1,23 @@
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace Module.ProjectValidator.Editor
{
[CustomPropertyDrawer(typeof(ProjectValidatorSettings.ValidatorEnabled))]
internal sealed class EditorProjectValidatorEnabledPropertyDrawer : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var spType = property.FindPropertyRelative(nameof(ProjectValidatorSettings.ValidatorEnabled.name));
var spEnabled = property.FindPropertyRelative(nameof(ProjectValidatorSettings.ValidatorEnabled.enabled));
var root = new VisualElement { style = { flexDirection = FlexDirection.Row } };
var veType = new PropertyField(spType, string.Empty) { style = { flexGrow = 1f }, enabledSelf = false };
var veEnabled = new PropertyField(spEnabled, string.Empty);
root.Add(veEnabled);
root.Add(veType);
return root;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e6c8cb829f514028b705bf121373c67c
timeCreated: 1779638011

View file

@ -16,4 +16,39 @@
-unity-font-style: bold; -unity-font-style: bold;
} }
.toolbar-button-settings {
width: 24px;
min-width: 24px;
max-width: 24px;
padding-top: 0;
padding-right: 0;
padding-bottom: 0;
padding-left: 0;
background-image: url("project://database/Packages/com.module.project-validator/Editor/Icons/editor_project_validator_settings.png?fileID=2800000&guid=a6c61d5fbd310894d8159ba6af32d7e3&type=3#editor_project_validator_settings");
-unity-background-scale-mode: scale-to-fit;
margin-left: 0;
}
.warning-icon {
background-image: url("project://database/Packages/com.module.project-validator/Editor/Icons/editor_project_validator_warning.png?fileID=2800000&guid=5dce0f250980ffb459470fac33dfab59&type=3#editor_project_validator_warning");
width: 16px;
min-width: 16px;
max-width: 16px;
max-height: 16px;
min-height: 16px;
height: 16px;
-unity-background-scale-mode: scale-to-fit;
margin-left: 4px;
}
.error-icon {
background-image: url("project://database/Packages/com.module.project-validator/Editor/Icons/editor_project_validator_error.png?fileID=2800000&guid=7b6c61a2cf824b74c87cb49759531c79&type=3#editor_project_validator_error");
width: 16px;
min-width: 16px;
max-width: 16px;
max-height: 16px;
min-height: 16px;
height: 16px;
-unity-background-scale-mode: scale-to-fit;
margin-left: 4px;
}

View file

@ -2,18 +2,29 @@
<uie:Toolbar name="toolbar"> <uie:Toolbar name="toolbar">
<uie:ToolbarButton text="Run" name="button-run" style="margin-left: 2px;"/> <uie:ToolbarButton text="Run" name="button-run" style="margin-left: 2px;"/>
<uie:ToolbarButton text="Clear" name="button-clear"/> <uie:ToolbarButton text="Clear" name="button-clear"/>
<uie:ToolbarSpacer style="flex-grow: 1;"/> <uie:ToolbarSpacer name="spacing" style="flex-grow: 1;"/>
<ui:VisualElement name="status-errors" style="flex-grow: 0; flex-direction: row; align-self: center; margin-left: 4px;">
<ui:Label text="9999" name="label-errors" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" style="flex-grow: 1; -unity-text-align: middle-center;"/>
<ui:VisualElement name="icon" class="error-icon" style="flex-grow: 1;"/>
</ui:VisualElement>
<ui:VisualElement name="status-warnings" style="flex-grow: 0; flex-direction: row; align-self: center; margin-right: 4px;">
<ui:Label text="9999" name="label-warnings" double-click-selects-word="false" triple-click-selects-line="false" display-tooltip-when-elided="false" style="flex-grow: 1; -unity-text-align: middle-center;"/>
<ui:VisualElement name="icon" class="warning-icon" style="flex-grow: 1;"/>
</ui:VisualElement>
<uie:ToolbarSpacer name="spacing" style="flex-grow: 1;"/>
<uie:ToolbarSearchField name="search-field"/> <uie:ToolbarSearchField name="search-field"/>
<uie:ToolbarButton text="" name="button-settings" class="toolbar-button-settings"/>
</uie:Toolbar> </uie:Toolbar>
<ui:MultiColumnTreeView columns="" sort-column-descriptions="" name="tree-view" show-alternating-row-backgrounds="All" data-source-type="Module.ProjectValidator.Editor.Report+Entry, Module.ProjectValidator.Editor" sorting-mode="Default" class="tree-view"> <ui:MultiColumnTreeView columns="" sort-column-descriptions="" name="tree-view" show-alternating-row-backgrounds="All" data-source-type="Module.ProjectValidator.Editor.Report+Entry, Module.ProjectValidator.Editor" sorting-mode="Default" class="tree-view">
<ui:Columns reorderable="false" primary-column-name="severity"> <ui:Columns reorderable="false" primary-column-name="severity">
<ui:Column name="severity" title="Severity" min-width="80px" optional="true"/> <ui:Column name="severity" title="Severity" min-width="80px" optional="true"/>
<ui:Column name="type" title="Type" min-width="42px" width="120px"/> <ui:Column name="type" title="Type" min-width="42px" width="120px"/>
<ui:Column name="asset" title="Asset" optional="true" min-width="80px" width="200px"/> <ui:Column name="asset" title="Asset" optional="true" min-width="80px" width="200px"/>
<ui:Column optional="true" name="scene-path" title="Scene Path" min-width="40px" width="200px"/> <ui:Column optional="true" name="relative-path" title="Path" min-width="40px" width="200px"/>
<ui:Column name="field-path" title="Field Path" min-width="70px" width="200px"/> <ui:Column name="field-path" title="Field Path" min-width="70px" width="200px"/>
<ui:Column optional="true" name="severity-message" title="Message" stretchable="true"/> <ui:Column optional="true" name="severity-message" title="Message" stretchable="true"/>
</ui:Columns> </ui:Columns>
<ui:SortColumnDescriptions/> <ui:SortColumnDescriptions/>
</ui:MultiColumnTreeView> </ui:MultiColumnTreeView>
<ui:Image/>
</ui:UXML> </ui:UXML>

View file

@ -1,17 +1,65 @@
# Description # Description
A tool to help validate data across scenes, prefabs and scriptable objects. A tool to help validate data across scenes, prefabs, scriptable objects and assets.
![Editor Window](~Images/editor-window.png) ![Editor Window](~Images/editor-window.png)
### Unity Windows ## Unity Windows
![Hierachy Window](~Images/editor-hierarchy-window.png) ![Hierachy Window](~Images/editor-hierarchy-window.png)
![Project Window](~Images/editor-project-window.png) ![Project Window](~Images/editor-project-window.png)
## Component Validators ## Settings
![Project Settings](~Images/editor-project-settings.png)
## Validators
### Asset Validators
```csharp
public sealed class AssetValidatorMaterialTexture : IAssetValidator<Material>
{
public void Validate(Material obj, List<ValidatorResult> results)
{
if (obj.shader == null)
return;
var count = obj.shader.GetPropertyCount();
for (var i = 0; i < count; i++)
{
var propertyType = obj.shader.GetPropertyType(i);
if (propertyType != ShaderPropertyType.Texture)
continue;
var propertyName = obj.shader.GetPropertyName(i);
var propertyValue = obj.GetTexture(propertyName);
if (propertyValue == null)
results.Add(ValidatorResult.Create(EValidatorSeverity.Warning, $"Texture property '{propertyName}' is Null"));
}
}
}
``` ```
### Game Object Validators
```csharp
public sealed class GameObjectValidatorBrokenPrefab : IGameObjectValidator
{
public void Validate(GameObject gameObject, List<ValidatorResult> results)
{
if (PrefabUtility.IsPrefabAssetMissing(gameObject))
results.Add(ValidatorResult.Create(EValidatorSeverity.Error, "GameObject is missing prefab asset"));
}
}
```
### Component Validators
```csharp
public sealed class ComponentValidatorMeshCollider : IComponentValidator<MeshCollider> public sealed class ComponentValidatorMeshCollider : IComponentValidator<MeshCollider>
{ {
public void Validate(MeshCollider component, List<ValidatorResult> results) public void Validate(MeshCollider component, List<ValidatorResult> results)
@ -22,10 +70,10 @@ public sealed class ComponentValidatorMeshCollider : IComponentValidator<MeshCol
} }
``` ```
## Attribute Validators ### Attribute Validators
The field attrribute: The field attribute:
``` ```csharp
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public sealed class RequiredAttribute : Attribute public sealed class RequiredAttribute : Attribute
{ {
@ -45,7 +93,7 @@ public sealed class RequiredAttribute : Attribute
The validator implements `IAttributeValidator<T>`, where `T` is the attribute and will automatically be found by the validator. The validator implements `IAttributeValidator<T>`, where `T` is the attribute and will automatically be found by the validator.
``` ```csharp
public sealed class Validator : IAttributeValidator<RequiredAttribute> public sealed class Validator : IAttributeValidator<RequiredAttribute>
{ {
public ValidatorResult Validate(RequiredAttribute attribute, object value) public ValidatorResult Validate(RequiredAttribute attribute, object value)

View file

@ -0,0 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
namespace Module.ProjectValidator
{
public interface IAssetValidator<in T> where T : Object
{
void Validate(T obj, List<ValidatorResult> results);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 24ee414b7fa14a368a48cab1b608ee3b
timeCreated: 1779624012

View file

@ -0,0 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
namespace Module.ProjectValidator
{
public interface IGameObjectValidator
{
void Validate(GameObject gameObject, List<ValidatorResult> results);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f7513a02611842029edd675cb64989ad
timeCreated: 1779133097

View file

@ -1,6 +1,6 @@
{ {
"name": "com.module.project-validator", "name": "com.module.project-validator",
"version": "0.2.0", "version": "1.0.3",
"displayName": "Module.ProjectValidator", "displayName": "Module.ProjectValidator",
"description": "", "description": "",
"unity": "6000.3", "unity": "6000.3",

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View file

@ -0,0 +1,117 @@
fileFormatVersion: 2
guid: 094f9567dcf756b468ea41cd471b65d7
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 0
wrapV: 0
wrapW: 0
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 4
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
customData:
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spriteCustomMetadata:
entries: []
nameFileIdTable: {}
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 232 KiB

After

Width:  |  Height:  |  Size: 154 KiB

Before After
Before After