Optimize odin integration

This commit is contained in:
VladV 2022-08-28 15:29:49 +03:00
parent 858da9e174
commit c1cecb4285
6 changed files with 51 additions and 17 deletions

View File

@ -1,6 +1,5 @@
using System;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using UnityEngine;
namespace TriInspector.Editor.Integrations.Odin
@ -19,13 +18,12 @@ namespace TriInspector.Editor.Integrations.Odin
return false;
}
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}
if (!type.IsDefined<DrawWithTriInspectorAttribute>() &&
!type.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return false;
}
@ -43,8 +41,7 @@ namespace TriInspector.Editor.Integrations.Odin
for (var parent = property.Parent; parent != null; parent = parent.Parent)
{
var parentType = parent.ValueEntry.TypeOfValue;
if (parentType.IsDefined<DrawWithTriInspectorAttribute>() ||
parentType.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (TriOdinUtility.IsDrawnByTri(parentType))
{
return false;
}

View File

@ -1,5 +1,4 @@
using System;
using Sirenix.Utilities;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor.Validation;
using TriInspector.Editor.Integrations.Odin;
@ -30,13 +29,12 @@ namespace TriInspector.Editor.Integrations.Odin
return false;
}
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}
if (!type.IsDefined<DrawWithTriInspectorAttribute>() &&
!type.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return false;
}
@ -44,8 +42,7 @@ namespace TriInspector.Editor.Integrations.Odin
for (var parent = property.Parent; parent != null; parent = parent.Parent)
{
var parentType = parent.Info.TypeOfValue;
if (parentType.IsDefined<DrawWithTriInspectorAttribute>() ||
parentType.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (TriOdinUtility.IsDrawnByTri(parentType))
{
return false;
}

View File

@ -20,8 +20,7 @@ namespace TriInspector.Editor.Integrations.Odin
return false;
}
if (!type.IsDefined<DrawWithTriInspectorAttribute>() &&
!type.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}

View File

@ -1,5 +1,4 @@
using System;
using Sirenix.Utilities;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor.Validation;
using TriInspector.Editor.Integrations.Odin;
@ -33,8 +32,7 @@ namespace TriInspector.Editor.Integrations.Odin
return false;
}
if (!type.IsDefined<DrawWithTriInspectorAttribute>() &&
!type.Assembly.IsDefined<DrawWithTriInspectorAttribute>())
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Sirenix.Utilities;
using UnityEditor;
namespace TriInspector.Editor.Integrations.Odin
{
public static class TriOdinUtility
{
private static readonly Dictionary<Assembly, bool> TriDrawnAssemblies = new Dictionary<Assembly, bool>();
private static HashSet<Type> _triDrawnTypes;
public static bool IsDrawnByTri(Type type)
{
if (_triDrawnTypes == null)
{
var list = TypeCache.GetTypesWithAttribute<DrawWithTriInspectorAttribute>();
var array = new Type[list.Count];
list.CopyTo(array, 0);
_triDrawnTypes = new HashSet<Type>(array);
}
if (_triDrawnTypes.Contains(type))
{
return true;
}
var asm = type.Assembly;
if (TriDrawnAssemblies.TryGetValue(asm, out var assemblyDrawnByTri))
{
return assemblyDrawnByTri;
}
assemblyDrawnByTri = asm.IsDefined<DrawWithTriInspectorAttribute>(false);
TriDrawnAssemblies[asm] = assemblyDrawnByTri;
return assemblyDrawnByTri;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c2be8b6a4b4d44c19156b0f1c1d40fc6
timeCreated: 1661688784