Writing Scene Validator for Tri-Inspector without Odin #132
-
Hello together, I would like to ask what the best approach would be to write a own scene validator? I think of something like: which does give me all the validation results. I also investigated the integration for Odin but somehow dont get it to work. Currently I am doing following: foreach (Component componentToInspect in componentsToInspect)
{
TriPropertyTreeForSerializedObject propertyTree = new TriPropertyTreeForSerializedObject(new SerializedObject(componentToInspect), false);
propertyTree.Update();
propertyTree.RunValidation();
propertyTree.EnumerateValidationResults((property, validationResult) =>
{
result.Add(new SceneValidationResult(scene, componentToInspect, validationResult));
});
} But there are no validation results altough I am using there the [Require] attribute. Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I made basic validator implementation, it's work for me using System;
using System.Collections.Generic;
using TriInspector;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TriSceneValidator : EditorWindow
{
[MenuItem("Tools/Tri Scene Validator")]
public static void Open()
{
var window = GetWindow<TriSceneValidator>();
window.titleContent = new GUIContent("Tri Validator");
window.Show();
}
[NonSerialized]
private readonly List<ValidationResult> _results = new List<ValidationResult>();
private void OnEnable()
{
EditorSceneManager.sceneOpened += OnSceneOpened;
EditorSceneManager.sceneClosed += OnSceneClosed;
EditorSceneManager.sceneSaved += OnSceneSaved;
ReValidate();
}
private void OnDisable()
{
EditorSceneManager.sceneOpened -= OnSceneOpened;
EditorSceneManager.sceneClosed -= OnSceneClosed;
EditorSceneManager.sceneSaved -= OnSceneSaved;
}
private void OnSceneSaved(Scene scene) => ReValidate();
private void OnSceneClosed(Scene scene) => ReValidate();
private void OnSceneOpened(Scene scene, OpenSceneMode mode) => ReValidate();
private void OnGUI()
{
GUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.ExpandWidth(true));
if (GUILayout.Button("Validate", EditorStyles.toolbarButton, GUILayout.Width(100)))
{
ReValidate();
}
GUILayout.EndHorizontal();
foreach (var result in _results)
{
GUILayout.BeginHorizontal();
GUILayout.Label(result.result.MessageType.ToString(), GUILayout.Width(60));
GUILayout.Label(result.result.Message, GUILayout.Width(300));
GUILayout.Label(result.property.PropertyPath);
if (GUILayout.Button("Select", EditorStyles.miniButton, GUILayout.Width(60)))
{
EditorGUIUtility.PingObject(result.component);
Selection.activeObject = result.component;
}
GUILayout.EndHorizontal();
}
}
private void ReValidate()
{
_results.Clear();
for (var sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
{
var scene = SceneManager.GetSceneAt(sceneIndex);
foreach (var root in scene.GetRootGameObjects())
{
ValidateGameObjectRecursive(root);
}
}
}
private void ValidateGameObjectRecursive(GameObject go)
{
foreach (var behaviour in go.GetComponents<MonoBehaviour>())
{
using var serializedObject = new SerializedObject(behaviour);
using var propertyTree = new TriPropertyTreeForSerializedObject(serializedObject);
propertyTree.Update(forceUpdate: true);
propertyTree.RunValidation();
propertyTree.EnumerateValidationResults((property, validationResult) =>
{
_results.Add(new ValidationResult
{
component = behaviour,
property = property,
result = validationResult,
});
});
}
for (var childIndex = 0; childIndex < go.transform.childCount; childIndex++)
{
ValidateGameObjectRecursive(go.transform.GetChild(childIndex).gameObject);
}
}
private struct ValidationResult
{
public Component component;
public TriProperty property;
public TriValidationResult result;
}
} |
Beta Was this translation helpful? Give feedback.
I made basic validator implementation, it's work for me