Skip to content

Commit

Permalink
- Added more stable method to retrieve all attributes for a serialize…
Browse files Browse the repository at this point in the history
…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
Doppelkeks committed Apr 18, 2023
1 parent 28f57aa commit 2e0ea3a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 44 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Graphs can now be created in a scene context and use scene references. To create a graph add the MonoGraphModel component to a gameobject.
- Added ability to rename the GraphWindow [#11](https://github.com/Gentlymad-Studios/NewGraph/issues/11)
### Fixed
- Major refactoring and minor bugfixes
- Major refactoring and minor bugfixes

## [0.3.3] - 2023-04-18
### Added
- Added more stable method to retrieve all attributes for a serialized property. [#14](https://github.com/Gentlymad-Studios/NewGraph/issues/14)

### Fixed
- Fixed wrong window size for all SearchWindows [#10](https://github.com/Gentlymad-Studios/NewGraph/issues/10)
- wrapped UnityEditor in #if UNITY_EDITOR preprocessor directives in all graph models
76 changes: 37 additions & 39 deletions Editor/Serialization/AttributesBag.cs
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);
}
}
}
4 changes: 3 additions & 1 deletion Editor/Serialization/PropertyBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private void DoVisibilityRecursive(GraphPropertyInfo graphProperty, GroupInfo pa
}
}


/// <summary>
/// Retrieve attributes for the currently active property
/// </summary>
Expand All @@ -212,7 +213,8 @@ private int RetrieveCurrentAttributes() {
currentGraphDisplayAttribute = null;

// retrieve all attributes
attributeBag.GetAttributes(nodeType, currentRelativePropertyPath, true);
//attributeBag.GetAttributes(nodeType, currentRelativePropertyPath, true);
attributeBag.GetAttributes(currentProperty, true);

// remember: we are only on ONE property!
// and every property can have SEVERAL attributes!
Expand Down
5 changes: 3 additions & 2 deletions Editor/Views/SearchWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private void RebuildSearch() {
bool didMatchAll = true;
bool didMatchStart = false;

// See if we match ALL the seaarch words.
// See if we match ALL the search words.
for (int w = 0; w < searchWords.Length; w++) {
string search = searchWords[w];
if (name.Contains(search)) {
Expand Down Expand Up @@ -463,7 +463,7 @@ private void ListGUI(SearchTreeEntry[] tree, float anim, SearchTreeGroupEntry pa
animRect.y = k_HeaderHeight;
//animRect.height -= k_HeaderHeight;
animRect.width -= 2;

animRect.height -= k_HeaderHeight;
// Start of animated area (the part that moves left and right)
GUILayout.BeginArea(animRect);

Expand Down Expand Up @@ -491,6 +491,7 @@ private void ListGUI(SearchTreeEntry[] tree, float anim, SearchTreeGroupEntry pa
ListGUI(tree, parent);

GUILayout.EndArea();

}

private void SelectEntry(SearchTreeEntry e, bool shouldInvokeCallback) {
Expand Down
2 changes: 2 additions & 0 deletions Models/MonoGraphModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace NewGraph {
Expand Down
2 changes: 2 additions & 0 deletions Models/ScriptableGraphModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace NewGraph {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.gentlymad.newgraph",
"version": "0.3.2",
"version": "0.3.3",
"displayName": "NewGraph",
"description": "A general data oriented node graph solution. This is build upon the idea to visualize complex data structures as graph networks without having to modify already established data classes, except adding [Node], [Port] and [SerializeReference] attributes to call classes that should show in the Graph View.",
"unity": "2022.2",
Expand Down

0 comments on commit 2e0ea3a

Please sign in to comment.