Allows ShowInInspector usage on fields

This commit is contained in:
VladV 2022-12-09 19:59:33 +04:00
parent b969569ae7
commit 9f63461715
5 changed files with 41 additions and 1 deletions

View File

@ -5,6 +5,9 @@ public class Misc_ShowInInspectorSample : ScriptableObject
{
private float _field;
[ShowInInspector]
private bool _myToggle;
[ShowInInspector]
public float ReadOnlyProperty => _field;

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TriInspector;
using TriInspector.TypeProcessors;
using TriInspector.Utilities;
[assembly: RegisterTriTypeProcessor(typeof(TriRegisterShownByTriFieldsTypeProcessor), 1)]
namespace TriInspector.TypeProcessors
{
public class TriRegisterShownByTriFieldsTypeProcessor : TriTypeProcessor
{
public override void ProcessType(Type type, List<TriPropertyDefinition> properties)
{
const int fieldsOffset = 5001;
properties.AddRange(TriReflectionUtilities
.GetAllInstanceFieldsInDeclarationOrder(type)
.Where(IsSerialized)
.Select((it, ind) => TriPropertyDefinition.CreateForFieldInfo(ind + fieldsOffset, it)));
}
private static bool IsSerialized(FieldInfo fieldInfo)
{
return fieldInfo.GetCustomAttribute<ShowInInspector>() != null &&
TriUnitySerializationUtilities.IsSerializableByUnity(fieldInfo) == false;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d5a485127f534bee963f620f86287c34
timeCreated: 1670601276

View File

@ -52,6 +52,9 @@ Shows non-serialized property in the inspector.
```csharp
private float _field;
[ShowInInspector]
private bool _myToggle;
[ShowInInspector]
public float ReadOnlyProperty => _field;

View File

@ -3,7 +3,7 @@ using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
[Conditional("UNITY_EDITOR")]
public class ShowInInspector : Attribute
{