Add DrawWithUnity attribute

This commit is contained in:
VladV 2022-11-11 21:17:12 +04:00
parent 1327d8d544
commit 9182a6268c
11 changed files with 94 additions and 11 deletions

View File

@ -1,5 +1,6 @@
using System;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEngine;
namespace TriInspector.Editor.Integrations.Odin
@ -77,6 +78,11 @@ namespace TriInspector.Editor.Integrations.Odin
{
_propertyTree.Draw();
}
if (_propertyTree.RepaintRequired)
{
GUIHelper.RequestRepaint();
}
}
private class LabelOverrideContext : TriPropertyOverrideContext

View File

@ -1,6 +1,7 @@
using System;
using Sirenix.Utilities;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using TriInspector.Utilities;
using UnityEngine;
@ -74,6 +75,11 @@ namespace TriInspector.Editor.Integrations.Odin
{
_propertyTree.Draw();
}
if (_propertyTree.RepaintRequired)
{
GUIHelper.RequestRepaint();
}
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using Object = UnityEngine.Object;
@ -88,13 +87,6 @@ namespace TriInspector.Editor.Integrations.Odin
_odinProperty.RecordForUndo(forceCompleteObjectUndo: true);
}
public override void RequestRepaint()
{
base.RequestRepaint();
GUIHelper.RequestRepaint();
}
private void OnPropertyChanged(TriProperty changedProperty)
{
ApplyEmittedScriptableObject();

View File

@ -29,6 +29,13 @@ namespace TriInspector.Elements
{
EditorGUI.BeginChangeCheck();
if (_property.IsArrayElement &&
_serializedProperty.propertyType == SerializedPropertyType.Generic &&
_serializedProperty.hasVisibleChildren)
{
position.xMin += 12;
}
_propertyHandler.OnGUI(position, _serializedProperty, _property.DisplayNameContent, true);
if (EditorGUI.EndChangeCheck())

View File

@ -1,4 +1,5 @@
using System;
using TriInspector.Utilities;
using TriInspectorUnityInternalBridge;
using UnityEditor;
using UnityEngine;
@ -75,7 +76,10 @@ namespace TriInspector.Elements
? ScriptAttributeUtilityProxy.GetHandler(serializedProperty)
: default(PropertyHandlerProxy?);
if (!handler.HasValue || !handler.Value.hasPropertyDrawer)
var drawWithUnity = handler.HasValue && handler.Value.hasPropertyDrawer ||
handler.HasValue && TriUnityInspectorUtilities.MustDrawWithUnity(property);
if (!drawWithUnity)
{
var propertyType = property.PropertyType;

View File

@ -113,7 +113,14 @@ namespace TriInspector
}
}
if (!IsArrayElement)
if (IsArrayElement)
{
if (TriUnityInspectorUtilities.TryGetSpecialArrayElementName(this, out var specialName))
{
_displayNameBackingField.text = specialName;
}
}
else
{
if (_definition.CustomLabel != null)
{

View File

@ -85,7 +85,7 @@ namespace TriInspector
RootProperty.EnumerateValidationResults(call);
}
public virtual void RequestRepaint()
public void RequestRepaint()
{
RepaintRequired = true;
}

View File

@ -0,0 +1,44 @@
using System.Reflection;
using UnityEngine;
namespace TriInspector.Utilities
{
public class TriUnityInspectorUtilities
{
private static readonly FieldInfo GUIStyleNameBackingField = typeof(GUIStyle)
.GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic);
public static bool MustDrawWithUnity(TriProperty property)
{
if (property.FieldType == typeof(GUIStyle))
{
return true;
}
return !property.IsArray && property.TryGetAttribute(out DrawWithUnityAttribute _);
}
public static bool TryGetSpecialArrayElementName(TriProperty property, out string name)
{
if (property.FieldType == typeof(GUIStyle) && property.Value is GUIStyle guiStyle)
{
GUIStyleNameBackingField?.SetValue(guiStyle, null);
name = guiStyle.name;
return true;
}
if (property.PropertyType == TriPropertyType.Generic &&
property.ChildrenProperties.Count > 0 &&
property.ChildrenProperties[0] is var firstChild &&
firstChild.ValueType == typeof(string) &&
firstChild.Value is string firstChildValueStr)
{
name = firstChildValueStr;
return true;
}
name = default;
return false;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 239f16a6fc1543b8b131493585c954b3
timeCreated: 1668183919

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public class DrawWithUnityAttribute : Attribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 72a4f37291e74452877df4701818e180
timeCreated: 1668181199