-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added more stable method to retrieve all attributes for a serialize…
…d property. [#14](#14) - Fixed wrong window size for all SearchWindows [#10](#10) - wrapped UnityEditor in #if UNITY_EDITOR preprocessor directives in all graph models
- Loading branch information
1 parent
28f57aa
commit 2e0ea3a
Showing
7 changed files
with
57 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,56 @@ | ||
using OdinSerializer.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using UnityEditor; | ||
|
||
namespace NewGraph { | ||
public class AttributesBag { | ||
private const BindingFlags AllBindingFlags = (BindingFlags)(-1); | ||
|
||
private delegate FieldInfo GetFieldInfoAndStaticTypeFromProperty(SerializedProperty aProperty, out Type aType); | ||
private static GetFieldInfoAndStaticTypeFromProperty m_GetFieldInfoAndStaticTypeFromProperty; | ||
|
||
public object[] attributes = new object[] { }; | ||
public Type type = null; | ||
|
||
/// <summary> | ||
/// Retrieve all attributes by a given propertyPath string (from a SerializedProperty) | ||
/// get attributes directly by the property | ||
/// </summary> | ||
/// <param name="targetObjectType"></param> | ||
/// <param name="relativePropPath"></param> | ||
/// <param name="property"></param> | ||
/// <param name="inherit"></param> | ||
public void GetAttributes(Type targetObjectType, string relativePropPath, bool inherit) { | ||
attributes = new object[] { }; | ||
type = null; | ||
|
||
FieldInfo fieldInfo = null; | ||
System.Reflection.PropertyInfo propertyInfo = null; | ||
IEnumerable<Type> allTypes; | ||
|
||
foreach (var name in relativePropPath.Split('.')) { | ||
|
||
allTypes = targetObjectType.GetBaseTypes(true); | ||
foreach (Type type in allTypes) { | ||
fieldInfo = type.GetField(name, AllBindingFlags); | ||
if (fieldInfo != null) { | ||
break; | ||
} | ||
} | ||
|
||
if (fieldInfo == null) { | ||
propertyInfo = targetObjectType.GetProperty(name, AllBindingFlags); | ||
if (propertyInfo == null) { | ||
return; | ||
} | ||
targetObjectType = propertyInfo.PropertyType; | ||
} else { | ||
targetObjectType = fieldInfo.FieldType; | ||
} | ||
} | ||
|
||
public void GetAttributes(SerializedProperty property, bool inherit) { | ||
FieldInfo fieldInfo = GetFieldInfoAndStaticType(property, out _); | ||
if (fieldInfo != null) { | ||
type = fieldInfo.FieldType; | ||
|
||
attributes = fieldInfo.GetCustomAttributes(inherit); | ||
} | ||
} | ||
|
||
} else if (propertyInfo != null) { | ||
type = propertyInfo.PropertyType; | ||
attributes = propertyInfo.GetCustomAttributes(inherit); | ||
/// <summary> | ||
/// Get the field info and type directly by the property. | ||
/// Uses an internal unity method. | ||
/// Based on: https://forum.unity.com/threads/get-a-general-object-value-from-serializedproperty.327098/#post-6432620 | ||
/// </summary> | ||
/// <param name="prop"></param> | ||
/// <param name="type"></param> | ||
/// <returns></returns> | ||
private static FieldInfo GetFieldInfoAndStaticType(SerializedProperty prop, out Type type) { | ||
if (m_GetFieldInfoAndStaticTypeFromProperty == null) { | ||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { | ||
foreach (var t in assembly.GetTypes()) { | ||
if (t.Name == "ScriptAttributeUtility") { | ||
MethodInfo mi = t.GetMethod(nameof(GetFieldInfoAndStaticTypeFromProperty), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | ||
m_GetFieldInfoAndStaticTypeFromProperty = (GetFieldInfoAndStaticTypeFromProperty)Delegate.CreateDelegate(typeof(GetFieldInfoAndStaticTypeFromProperty), mi); | ||
break; | ||
} | ||
} | ||
if (m_GetFieldInfoAndStaticTypeFromProperty != null) break; | ||
} | ||
if (m_GetFieldInfoAndStaticTypeFromProperty == null) { | ||
UnityEngine.Debug.LogError("GetFieldInfoAndStaticType::Reflection failed!"); | ||
type = null; | ||
return null; | ||
} | ||
} | ||
return m_GetFieldInfoAndStaticTypeFromProperty(prop, out type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters