mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Access to unity internal with InternalsVisibleTo instead of reflection
This commit is contained in:
parent
28cae2b146
commit
0841d5e9dd
@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections;
|
||||
using TriInspectorUnityInternalBridge;
|
||||
using TriInspector.Utilities;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
@ -11,28 +11,12 @@ namespace TriInspector.Elements
|
||||
{
|
||||
private static readonly GUIContent ListIsNullContent = new GUIContent("List is null");
|
||||
|
||||
private static readonly Action<object, Rect> ReorderableListDrawHeaderMethod;
|
||||
private static readonly Action<object> ReorderableListClearCacheRecursiveMethod;
|
||||
|
||||
private readonly TriProperty _property;
|
||||
private readonly ReorderableList _reorderableListGui;
|
||||
private readonly bool _alwaysExpanded;
|
||||
|
||||
private float _lastContentWidth;
|
||||
|
||||
static TriListElement()
|
||||
{
|
||||
ReorderableListDrawHeaderMethod = typeof(ReorderableList)
|
||||
.CompileVoidInstanceMethod<Rect>("DoListHeader");
|
||||
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
ReorderableListClearCacheRecursiveMethod = typeof(ReorderableList)
|
||||
.CompileVoidInstanceMethod("ClearCacheRecursive");
|
||||
#else
|
||||
ReorderableListClearCacheRecursiveMethod = delegate { };
|
||||
#endif
|
||||
}
|
||||
|
||||
public TriListElement(TriProperty property)
|
||||
{
|
||||
property.TryGetAttribute(out ListDrawerSettings settings);
|
||||
@ -86,7 +70,7 @@ namespace TriInspector.Elements
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
ReorderableListClearCacheRecursiveMethod(_reorderableListGui);
|
||||
ReorderableListProxy.ClearCacheRecursive(_reorderableListGui);
|
||||
}
|
||||
|
||||
return dirty;
|
||||
@ -121,7 +105,7 @@ namespace TriInspector.Elements
|
||||
|
||||
if (!_property.IsExpanded)
|
||||
{
|
||||
ReorderableListDrawHeaderMethod(_reorderableListGui, new Rect(position)
|
||||
ReorderableListProxy.DoListHeader(_reorderableListGui, new Rect(position)
|
||||
{
|
||||
yMax = position.yMax - 4,
|
||||
});
|
||||
|
@ -2,14 +2,15 @@
|
||||
"name": "TriInspector.Editor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"TriInspector"
|
||||
"TriInspector",
|
||||
"Unity.InternalAPIEditorBridge.012"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
|
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
public class EditorGUIUtilityProxy
|
||||
{
|
||||
private static Func<MessageType, Texture2D> _getHelpIcon;
|
||||
|
||||
public static Texture2D GetHelpIcon(MessageType type)
|
||||
{
|
||||
if (_getHelpIcon == null)
|
||||
{
|
||||
_getHelpIcon = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditor.EditorGUIUtility")
|
||||
.CompileStaticMethod<MessageType, Texture2D>("GetHelpIcon");
|
||||
}
|
||||
|
||||
return _getHelpIcon(type);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class InternalEditorUtilityProxy
|
||||
{
|
||||
private static Func<UnityEngine.Object, bool> _getIsInspectorExpanded;
|
||||
private static Action<UnityEngine.Object, bool> _setIsInspectorExpanded;
|
||||
|
||||
public static bool GetIsInspectorExpanded(UnityEngine.Object obj)
|
||||
{
|
||||
if (_getIsInspectorExpanded == null)
|
||||
{
|
||||
_getIsInspectorExpanded = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditorInternal.InternalEditorUtility")
|
||||
.CompileStaticMethod<UnityEngine.Object, bool>("GetIsInspectorExpanded");
|
||||
}
|
||||
|
||||
return _getIsInspectorExpanded.Invoke(obj);
|
||||
}
|
||||
|
||||
public static void SetIsInspectorExpanded(UnityEngine.Object obj, bool isExpanded)
|
||||
{
|
||||
if (_setIsInspectorExpanded == null)
|
||||
{
|
||||
_setIsInspectorExpanded = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditorInternal.InternalEditorUtility")
|
||||
.CompileStaticVoidMethod<UnityEngine.Object, bool>("SetIsInspectorExpanded");
|
||||
}
|
||||
|
||||
_setIsInspectorExpanded.Invoke(obj, isExpanded);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class ScriptAttributeUtilityProxy
|
||||
{
|
||||
private static Func<SerializedProperty, object> _getHandler;
|
||||
|
||||
public static PropertyHandlerProxy GetHandler(SerializedProperty property)
|
||||
{
|
||||
if (_getHandler == null)
|
||||
{
|
||||
_getHandler = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditor.ScriptAttributeUtility")
|
||||
.CompileStaticMethod<SerializedProperty, object>("GetHandler");
|
||||
}
|
||||
|
||||
return new PropertyHandlerProxy(_getHandler(property));
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct PropertyHandlerProxy
|
||||
{
|
||||
private static Func<object, bool> _hasPropertyDrawerProperty;
|
||||
private static Func<object, SerializedProperty, GUIContent, bool, float> _getHeightMethod;
|
||||
private static Func<object, Rect, SerializedProperty, GUIContent, bool, bool> _onGuiMethod;
|
||||
|
||||
private readonly object _self;
|
||||
|
||||
internal PropertyHandlerProxy(object self)
|
||||
{
|
||||
_self = self;
|
||||
}
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public bool hasPropertyDrawer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_hasPropertyDrawerProperty == null)
|
||||
{
|
||||
_hasPropertyDrawerProperty = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditor.PropertyHandler")
|
||||
.CompileInstanceProperty<bool>("hasPropertyDrawer");
|
||||
}
|
||||
|
||||
return _hasPropertyDrawerProperty(_self);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetHeight(
|
||||
SerializedProperty property,
|
||||
GUIContent label,
|
||||
bool includeChildren)
|
||||
{
|
||||
if (_getHeightMethod == null)
|
||||
{
|
||||
_getHeightMethod = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditor.PropertyHandler")
|
||||
.CompileInstanceMethod<SerializedProperty, GUIContent, bool, float>("GetHeight");
|
||||
}
|
||||
|
||||
return _getHeightMethod(_self, property, label, includeChildren);
|
||||
}
|
||||
|
||||
public bool OnGUI(
|
||||
Rect position,
|
||||
SerializedProperty property,
|
||||
GUIContent label,
|
||||
bool includeChildren)
|
||||
{
|
||||
if (_onGuiMethod == null)
|
||||
{
|
||||
_onGuiMethod = TriReflectionUtilities
|
||||
.GetUnityEditorTypeByFullName("UnityEditor.PropertyHandler")
|
||||
.CompileInstanceMethod<Rect, SerializedProperty, GUIContent, bool, bool>("OnGUI");
|
||||
}
|
||||
|
||||
return _onGuiMethod(_self, position, property, label, includeChildren);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class TriReflectionCompileExtensions
|
||||
{
|
||||
public static Func<object, TResult> CompileInstanceProperty<TResult>(this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var property = GetProperty(type, name, flags);
|
||||
|
||||
var target = Expression.Parameter(typeof(object));
|
||||
var body = Expression.Call(Expression.Convert(target, type), property.GetMethod);
|
||||
var lambda = Expression.Lambda<Func<object, TResult>>(body, target);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Action<object> CompileVoidInstanceMethod(this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 0, flags);
|
||||
|
||||
var target = Expression.Parameter(typeof(object));
|
||||
var body = Expression.Call(Expression.Convert(target, type), method);
|
||||
var lambda = Expression.Lambda<Action<object>>(body, target);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Action<object, T1> CompileVoidInstanceMethod<T1>(this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 1, flags);
|
||||
|
||||
var target = Expression.Parameter(typeof(object));
|
||||
var a1 = Expression.Parameter(typeof(T1));
|
||||
var body = Expression.Call(Expression.Convert(target, type), method, a1);
|
||||
var lambda = Expression.Lambda<Action<object, T1>>(body, target, a1);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Func<object, T1, T2, T3, TResult> CompileInstanceMethod<T1, T2, T3, TResult>(
|
||||
this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 3, flags);
|
||||
|
||||
var target = Expression.Parameter(typeof(object));
|
||||
var a1 = Expression.Parameter(typeof(T1));
|
||||
var a2 = Expression.Parameter(typeof(T2));
|
||||
var a3 = Expression.Parameter(typeof(T3));
|
||||
var body = Expression.Call(Expression.Convert(target, type), method, a1, a2, a3);
|
||||
var lambda = Expression.Lambda<Func<object, T1, T2, T3, TResult>>(body, target, a1, a2, a3);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Func<object, T1, T2, T3, T4, TResult> CompileInstanceMethod<T1, T2, T3, T4, TResult>(
|
||||
this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 4, flags);
|
||||
|
||||
var target = Expression.Parameter(typeof(object));
|
||||
var a1 = Expression.Parameter(typeof(T1));
|
||||
var a2 = Expression.Parameter(typeof(T2));
|
||||
var a3 = Expression.Parameter(typeof(T3));
|
||||
var a4 = Expression.Parameter(typeof(T4));
|
||||
var body = Expression.Call(Expression.Convert(target, type), method, a1, a2, a3, a4);
|
||||
var lambda = Expression.Lambda<Func<object, T1, T2, T3, T4, TResult>>(body, target, a1, a2, a3, a4);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Func<T1, TResult> CompileStaticMethod<T1, TResult>(
|
||||
this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 1, flags);
|
||||
|
||||
var a1 = Expression.Parameter(typeof(T1));
|
||||
var body = Expression.Call(method, a1);
|
||||
var lambda = Expression.Lambda<Func<T1, TResult>>(body, a1);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
public static Action<T1, T2> CompileStaticVoidMethod<T1, T2>(
|
||||
this Type type, string name)
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
var method = GetMethod(type, name, 2, flags);
|
||||
|
||||
var a1 = Expression.Parameter(typeof(T1));
|
||||
var a2 = Expression.Parameter(typeof(T2));
|
||||
var body = Expression.Call(method, a1, a2);
|
||||
var lambda = Expression.Lambda<Action<T1, T2>>(body, a1, a2);
|
||||
return lambda.Compile();
|
||||
}
|
||||
|
||||
private static PropertyInfo GetProperty(this Type type, string name, BindingFlags flags)
|
||||
{
|
||||
var property = type.GetProperties(flags).SingleOrDefault(it => it.Name == name);
|
||||
return property ?? throw new InvalidOperationException($"Property {name} of type {type} not found");
|
||||
}
|
||||
|
||||
private static MethodInfo GetMethod(Type type, string name, int parametersCount, BindingFlags flags)
|
||||
{
|
||||
var method = type.GetMethods(flags)
|
||||
.SingleOrDefault(it => it.Name == name && it.GetParameters().Length == parametersCount);
|
||||
|
||||
return method ?? throw new InvalidOperationException(
|
||||
$"Method {name} of type {type} with {parametersCount} args not found");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d558661b36c94968ac0e2f704da4f6cb
|
||||
timeCreated: 1639241554
|
BIN
Installer.unitypackage
Normal file
BIN
Installer.unitypackage
Normal file
Binary file not shown.
7
Installer.unitypackage.meta
Normal file
7
Installer.unitypackage.meta
Normal file
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45e08a87d3d7eab48b90b26af99f1631
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -266,6 +266,8 @@ Minimal Unity Version is 2020.3.
|
||||
Library distributed as git package ([How to install package from git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html))
|
||||
<br>Git URL: `https://github.com/codewriter-packages/Tri-Inspector.git`
|
||||
|
||||
After installing the package, you need to unpack the `Installer.unitypackage` that comes with the package
|
||||
|
||||
## License
|
||||
|
||||
Tri-Inspector is [MIT licensed](./LICENSE.md).
|
8
Unity.InternalAPIEditorBridge.012.meta
Normal file
8
Unity.InternalAPIEditorBridge.012.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17812d8658d186345af6f2e6bbc1c31b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
3
Unity.InternalAPIEditorBridge.012/AssemblyInfo.cs
Normal file
3
Unity.InternalAPIEditorBridge.012/AssemblyInfo.cs
Normal file
@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("TriInspector.Editor")]
|
3
Unity.InternalAPIEditorBridge.012/AssemblyInfo.cs.meta
Normal file
3
Unity.InternalAPIEditorBridge.012/AssemblyInfo.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ceaac34ef4d54edea2f5bd49bc1145a5
|
||||
timeCreated: 1652008241
|
13
Unity.InternalAPIEditorBridge.012/EditorGUIUtilityProxy.cs
Normal file
13
Unity.InternalAPIEditorBridge.012/EditorGUIUtilityProxy.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class EditorGUIUtilityProxy
|
||||
{
|
||||
public static Texture2D GetHelpIcon(MessageType type)
|
||||
{
|
||||
return EditorGUIUtility.GetHelpIcon(type);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using UnityEditorInternal;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class InternalEditorUtilityProxy
|
||||
{
|
||||
public static bool GetIsInspectorExpanded(UnityEngine.Object obj)
|
||||
{
|
||||
return InternalEditorUtility.GetIsInspectorExpanded(obj);
|
||||
}
|
||||
|
||||
public static void SetIsInspectorExpanded(UnityEngine.Object obj, bool isExpanded)
|
||||
{
|
||||
InternalEditorUtility.SetIsInspectorExpanded(obj, isExpanded);
|
||||
}
|
||||
}
|
||||
}
|
30
Unity.InternalAPIEditorBridge.012/ReorderableListProxy.cs
Normal file
30
Unity.InternalAPIEditorBridge.012/ReorderableListProxy.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspectorUnityInternalBridge
|
||||
{
|
||||
internal static class ReorderableListProxy
|
||||
{
|
||||
public static void DoListHeader(ReorderableList list, Rect headerRect)
|
||||
{
|
||||
if (list.showDefaultBackground && Event.current.type == EventType.Repaint)
|
||||
{
|
||||
ReorderableList.defaultBehaviours.DrawHeaderBackground(headerRect);
|
||||
}
|
||||
|
||||
headerRect.xMin += 6f;
|
||||
headerRect.xMax -= 6f;
|
||||
headerRect.height -= 2f;
|
||||
headerRect.y += 1;
|
||||
|
||||
list.drawHeaderCallback?.Invoke(headerRect);
|
||||
}
|
||||
|
||||
public static void ClearCacheRecursive(ReorderableList list)
|
||||
{
|
||||
#if UNITY_2020_2_OR_NEWER
|
||||
list.ClearCacheRecursive();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acf8923868a81dd41825ea3ce86c0455
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,37 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TriInspector.Utilities
|
||||
{
|
||||
internal static class ScriptAttributeUtilityProxy
|
||||
{
|
||||
public static PropertyHandlerProxy GetHandler(SerializedProperty property)
|
||||
{
|
||||
var handler = ScriptAttributeUtility.GetHandler(property);
|
||||
return new PropertyHandlerProxy(handler);
|
||||
}
|
||||
}
|
||||
|
||||
internal readonly struct PropertyHandlerProxy
|
||||
{
|
||||
private readonly PropertyHandler _handler;
|
||||
|
||||
internal PropertyHandlerProxy(PropertyHandler handler)
|
||||
{
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public bool hasPropertyDrawer => _handler.hasPropertyDrawer;
|
||||
|
||||
public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
|
||||
{
|
||||
return _handler.GetHeight(property, label, includeChildren);
|
||||
}
|
||||
|
||||
public bool OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
|
||||
{
|
||||
return _handler.OnGUI(position, property, label, includeChildren);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"reference": "Unity.InternalAPIEditorBridge.012"
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef298164c542ea34b917beb80251fa54
|
||||
AssemblyDefinitionReferenceImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user