@@ -9,7 +9,7 @@ namespace XNodeEditor.Internal {
9
9
/// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
10
10
public class NodeEditorBase < T , A , K > where A : Attribute , NodeEditorBase < T , A , K > . INodeEditorAttrib where T : NodeEditorBase < T , A , K > where K : ScriptableObject {
11
11
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
12
- private static Dictionary < Type , T > editorTemplates ;
12
+ private static Dictionary < Type , Type > editorTypes ;
13
13
private static Dictionary < K , T > editors = new Dictionary < K , T > ( ) ;
14
14
public K target ;
15
15
public SerializedObject serializedObject ;
@@ -18,33 +18,36 @@ public static T GetEditor(K target) {
18
18
if ( target == null ) return null ;
19
19
if ( ! editors . ContainsKey ( target ) ) {
20
20
Type type = target . GetType ( ) ;
21
- T editor = GetEditor ( type ) ;
22
- editors . Add ( target , Activator . CreateInstance ( editor . GetType ( ) ) as T ) ;
21
+ Type editorType = GetEditorType ( type ) ;
22
+ editors . Add ( target , Activator . CreateInstance ( editorType ) as T ) ;
23
23
editors [ target ] . target = target ;
24
24
editors [ target ] . serializedObject = new SerializedObject ( target ) ;
25
25
}
26
- return editors [ target ] ;
26
+ T editor = editors [ target ] ;
27
+ if ( editor . target == null ) editor . target = target ;
28
+ if ( editor . serializedObject == null ) editor . serializedObject = new SerializedObject ( target ) ;
29
+ return editor ;
27
30
}
28
31
29
- private static T GetEditor ( Type type ) {
32
+ private static Type GetEditorType ( Type type ) {
30
33
if ( type == null ) return null ;
31
- if ( editorTemplates == null ) CacheCustomEditors ( ) ;
32
- if ( editorTemplates . ContainsKey ( type ) ) return editorTemplates [ type ] ;
34
+ if ( editorTypes == null ) CacheCustomEditors ( ) ;
35
+ if ( editorTypes . ContainsKey ( type ) ) return editorTypes [ type ] ;
33
36
//If type isn't found, try base type
34
- return GetEditor ( type . BaseType ) ;
37
+ return GetEditorType ( type . BaseType ) ;
35
38
}
36
39
37
40
private static void CacheCustomEditors ( ) {
38
- editorTemplates = new Dictionary < Type , T > ( ) ;
41
+ editorTypes = new Dictionary < Type , Type > ( ) ;
39
42
40
43
//Get all classes deriving from NodeEditor via reflection
41
44
Type [ ] nodeEditors = XNodeEditor . NodeEditorWindow . GetDerivedTypes ( typeof ( T ) ) ;
42
45
for ( int i = 0 ; i < nodeEditors . Length ; i ++ ) {
46
+ if ( nodeEditors [ i ] . IsAbstract ) continue ;
43
47
var attribs = nodeEditors [ i ] . GetCustomAttributes ( typeof ( A ) , false ) ;
44
48
if ( attribs == null || attribs . Length == 0 ) continue ;
45
- if ( nodeEditors [ i ] . IsAbstract ) continue ;
46
49
A attrib = attribs [ 0 ] as A ;
47
- editorTemplates . Add ( attrib . GetInspectedType ( ) , Activator . CreateInstance ( nodeEditors [ i ] ) as T ) ;
50
+ editorTypes . Add ( attrib . GetInspectedType ( ) , nodeEditors [ i ] ) ;
48
51
}
49
52
}
50
53
0 commit comments