mirror of
https://github.com/maxartz15/Validator.git
synced 2025-07-04 14:36:05 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
5b70e24c32 | |||
15808ce035 | |||
8399d043f5 | |||
b4dd8744df | |||
b13cf47fbf | |||
9c01bed215 | |||
74fd067220 |
@ -1,4 +1,12 @@
|
||||
# Change Log:
|
||||
## 0.1.4
|
||||
- Fix attribute validator..?
|
||||
## 0.1.3
|
||||
- Fix attribute validator..?
|
||||
## 0.1.2
|
||||
- ...
|
||||
## 0.1.1
|
||||
- First prototype of Attribute validator.
|
||||
## 0.1.1
|
||||
- Search input.
|
||||
- GUI toolbar.
|
||||
|
BIN
Documentation~/Images/ValidatorWindow_01.png
Normal file
BIN
Documentation~/Images/ValidatorWindow_01.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 42 KiB |
@ -2,6 +2,7 @@ namespace Validator.Editor
|
||||
{
|
||||
public interface IValidator
|
||||
{
|
||||
public string MenuName { get; }
|
||||
public Report Validate();
|
||||
}
|
||||
}
|
@ -156,7 +156,7 @@ namespace Validator.Editor
|
||||
validatorOptionsMenu.AddSeparator("");
|
||||
foreach (ValidatorInfo validatorInfo in validators)
|
||||
{
|
||||
validatorOptionsMenu.AddItem(new GUIContent($"{validatorInfo.validator.GetType().Name}"), validatorInfo.isEnabled, OnValidatorInfoVisibilityChangedEvent, validatorInfo);
|
||||
validatorOptionsMenu.AddItem(new GUIContent($"{validatorInfo.validator.MenuName}"), validatorInfo.isEnabled, OnValidatorInfoVisibilityChangedEvent, validatorInfo);
|
||||
}
|
||||
validatorOptionsMenu.ShowAsContext();
|
||||
}
|
||||
|
79
Editor/Validators/RequiredAttributeValidator.cs
Normal file
79
Editor/Validators/RequiredAttributeValidator.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Validator.Editor
|
||||
{
|
||||
public class RequiredAttributeAssetValidator : IValidator
|
||||
{
|
||||
public string MenuName => nameof(RequiredAttributeAssetValidator);
|
||||
|
||||
private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||
|
||||
public Report Validate()
|
||||
{
|
||||
Report report = new Report(nameof(RequiredAttributeAssetValidator));
|
||||
|
||||
List<Object> objects = ValidatableAssetValidator.FindAssetsByType<Object>();
|
||||
|
||||
for (int i = 0; i < objects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("RequiredAttributeAssetValidator", "RequiredAttribute...", (float)i / objects.Count);
|
||||
|
||||
IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields(flags)
|
||||
let attr = fi.GetCustomAttributes(typeof(RequiredAttribute), true)
|
||||
where attr.Length == 1
|
||||
select (FieldInfo: fi, Attribute: attr.First() as RequiredAttribute);
|
||||
|
||||
foreach ((FieldInfo FieldInfo, RequiredAttribute Attribute) field in fieldsWithRequiredAttribute)
|
||||
{
|
||||
object o = field.FieldInfo.GetValue(objects[i]);
|
||||
if (o == null || o.Equals(null))
|
||||
{
|
||||
report.Log(objects[i], field.Attribute.WarningType, field.Attribute.Category, $"{field.FieldInfo.Name} is null", $"Assign {field.FieldInfo.FieldType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return report;
|
||||
}
|
||||
}
|
||||
|
||||
public class RequiredAttributeSceneValidator : IValidator
|
||||
{
|
||||
public string MenuName => nameof(RequiredAttributeSceneValidator);
|
||||
|
||||
private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||
|
||||
public Report Validate()
|
||||
{
|
||||
Report report = new Report(nameof(RequiredAttributeSceneValidator));
|
||||
|
||||
List<MonoBehaviour> objects = ValidatableSceneValidator.FindAllObjectsOfType<MonoBehaviour>();
|
||||
|
||||
for (int i = 0; i < objects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("RequiredAttributeSceneValidator", "RequiredAttribute...", (float)i / objects.Count);
|
||||
IEnumerable<(FieldInfo FieldInfo, RequiredAttribute Attribute)> fieldsWithRequiredAttribute = from fi in objects[i].GetType().GetFields(flags)
|
||||
let attr = fi.GetCustomAttributes(typeof(RequiredAttribute), true)
|
||||
where attr.Length == 1
|
||||
select (FieldInfo: fi, Attribute: attr.First() as RequiredAttribute);
|
||||
|
||||
foreach ((FieldInfo FieldInfo, RequiredAttribute Attribute) field in fieldsWithRequiredAttribute)
|
||||
{
|
||||
object o = field.FieldInfo.GetValue(objects[i]);
|
||||
if (o == null || o.Equals(null))
|
||||
{
|
||||
report.Log(objects[i], field.Attribute.WarningType, field.Attribute.Category, $"{field.FieldInfo.Name} is null", $"Assign {field.FieldInfo.FieldType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return report;
|
||||
}
|
||||
}
|
||||
}
|
11
Editor/Validators/RequiredAttributeValidator.cs.meta
Normal file
11
Editor/Validators/RequiredAttributeValidator.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90d5c1b8964451644b0aa09257ab0bc3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,55 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Validator.Editor
|
||||
{
|
||||
public class SceneValidator : IValidator
|
||||
{
|
||||
public Report Validate()
|
||||
{
|
||||
Report report = new Report("SceneValidator");
|
||||
|
||||
List<IValidatable> objects = FindAllObjectsOfType<IValidatable>();
|
||||
for (int i = 0; i < objects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "Validate...", (float)i / objects.Count);
|
||||
|
||||
objects[i].Validate(report);
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
private static List<GameObject> GetAllRootGameObjects()
|
||||
{
|
||||
List<GameObject> gameObjects = new List<GameObject>();
|
||||
|
||||
for (int i = 0; i < SceneManager.sceneCount; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "GetAllRootGameObjects...", (float)i / SceneManager.sceneCount);
|
||||
gameObjects.AddRange(SceneManager.GetSceneAt(i).GetRootGameObjects());
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return gameObjects;
|
||||
}
|
||||
|
||||
private static List<T> FindAllObjectsOfType<T>()
|
||||
{
|
||||
List<T> objects = new List<T>();
|
||||
|
||||
List<GameObject> gameObjects = GetAllRootGameObjects();
|
||||
for (int i = 0; i < gameObjects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "FindAllObjectsOfType...", (float)i / gameObjects.Count);
|
||||
objects.AddRange(gameObjects[i].GetComponentsInChildren<T>(true));
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return objects;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,11 +4,13 @@ using UnityEngine;
|
||||
|
||||
namespace Validator.Editor
|
||||
{
|
||||
public class AssetValidator : IValidator
|
||||
public class ValidatableAssetValidator : IValidator
|
||||
{
|
||||
public string MenuName => nameof(ValidatableAssetValidator);
|
||||
|
||||
public Report Validate()
|
||||
{
|
||||
Report report = new Report("AssetValidator");
|
||||
Report report = new Report(nameof(ValidatableAssetValidator));
|
||||
|
||||
List<Object> objects = FindAssetsByType<Object>();
|
||||
for (int i = 0; i < objects.Count; i++)
|
57
Editor/Validators/ValidatableSceneValidator.cs
Normal file
57
Editor/Validators/ValidatableSceneValidator.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Validator.Editor
|
||||
{
|
||||
public class ValidatableSceneValidator : IValidator
|
||||
{
|
||||
public string MenuName => nameof(ValidatableSceneValidator);
|
||||
|
||||
public Report Validate()
|
||||
{
|
||||
Report report = new Report(nameof(ValidatableSceneValidator));
|
||||
|
||||
List<IValidatable> objects = FindAllObjectsOfType<IValidatable>();
|
||||
for (int i = 0; i < objects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "Validate...", (float)i / objects.Count);
|
||||
|
||||
objects[i].Validate(report);
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
private static List<GameObject> GetAllRootGameObjects()
|
||||
{
|
||||
List<GameObject> gameObjects = new List<GameObject>();
|
||||
|
||||
for (int i = 0; i < SceneManager.sceneCount; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "GetAllRootGameObjects...", (float)i / SceneManager.sceneCount);
|
||||
gameObjects.AddRange(SceneManager.GetSceneAt(i).GetRootGameObjects());
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return gameObjects;
|
||||
}
|
||||
|
||||
public static List<T> FindAllObjectsOfType<T>()
|
||||
{
|
||||
List<T> objects = new List<T>();
|
||||
|
||||
List<GameObject> gameObjects = GetAllRootGameObjects();
|
||||
for (int i = 0; i < gameObjects.Count; i++)
|
||||
{
|
||||
EditorUtility.DisplayProgressBar("SceneValidator", "FindAllObjectsOfType...", (float)i / gameObjects.Count);
|
||||
objects.AddRange(gameObjects[i].GetComponentsInChildren<T>(true));
|
||||
}
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
return objects;
|
||||
}
|
||||
}
|
||||
}
|
21
README.md
21
README.md
@ -1,12 +1,11 @@
|
||||
# Validator
|
||||
|
||||

|
||||
|
||||
Unity project validator framework.
|
||||
|
||||
## Getting Started
|
||||
Open Validator Window:
|
||||
> Window -> General -> Validator
|
||||
|
||||
Add validatable:
|
||||
Add a custom validate check using the `IValidatable` interface:
|
||||
```C#
|
||||
using Validator;
|
||||
|
||||
@ -28,13 +27,21 @@ public class MyBehaviour : MonoBehaviour, IValidatable
|
||||
}
|
||||
```
|
||||
|
||||
Add a validate check using `[Required]` attribute:
|
||||
```C#
|
||||
[SerializeField, Required] private GameObject playerPrefab = null; // If someone forgets to assign it, it would be invalid.
|
||||
```
|
||||
|
||||
Open Validator Window:
|
||||
> Window -> General -> Validator
|
||||
|
||||
Run the validator:
|
||||
> Click the 'run/play' button and wait for the report to be generated.
|
||||
|
||||
## Install
|
||||
|
||||
[Installing from a Git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html)
|
||||
|
||||
[Unitypackage](https://github.com/COMPANYNAME/PACKAGENAME/releases)
|
||||
|
||||
|
||||
## LICENSE
|
||||
|
||||
Overall package is licensed under [MIT](/LICENSE.md), unless otherwise noted in the [3rd party licenses](/THIRD%20PARTY%20NOTICES.md) file and/or source code.
|
8
Runtime/Validators.meta
Normal file
8
Runtime/Validators.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cd25e23ad137e24ea0f469556831712
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Runtime/Validators/Attributes.meta
Normal file
8
Runtime/Validators/Attributes.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25705f09a21a10049aed213ea301db84
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
19
Runtime/Validators/Attributes/RequiredAttribute.cs
Normal file
19
Runtime/Validators/Attributes/RequiredAttribute.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
|
||||
namespace Validator
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
|
||||
public class RequiredAttribute : Attribute
|
||||
{
|
||||
public WarningType WarningType { get; private set; } = WarningType.Error;
|
||||
public string Category { get; private set; } = ReportCategories.Design;
|
||||
|
||||
public RequiredAttribute() { }
|
||||
|
||||
public RequiredAttribute(WarningType warningType = WarningType.Error, string category = ReportCategories.Design)
|
||||
{
|
||||
WarningType = warningType;
|
||||
Category = category;
|
||||
}
|
||||
}
|
||||
}
|
11
Runtime/Validators/Attributes/RequiredAttribute.cs.meta
Normal file
11
Runtime/Validators/Attributes/RequiredAttribute.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4dbbb107f0e849140b1132ac5dcca90f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "com.vertexcolor.validator",
|
||||
"displayName": "Validator",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.4",
|
||||
"unity": "2019.3",
|
||||
"description": "Unity project validator framework.",
|
||||
"category": "Tool",
|
||||
|
Reference in New Issue
Block a user