1
0
mirror of https://github.com/maxartz15/Validator.git synced 2024-11-13 00:05:30 +01:00
Validator/Editor/Validators/SceneValidator.cs
max 4b9b5a8c1d Base validator
SceneValidator & AssetValidator of objects that implement IValidatable.
2021-12-27 00:52:50 +01:00

55 lines
1.7 KiB
C#

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;
}
}
}