1+ using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+ using System . Reflection ;
6+ using System . Text . RegularExpressions ;
7+ using UnityEditor ;
8+ using UnityEngine ;
9+
10+ namespace AnySerialize . Editor
11+ {
12+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , > ) ) ]
13+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , > ) ) ]
14+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , > ) ) ]
15+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , > ) ) ]
16+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , > ) ) ]
17+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , > ) ) ]
18+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , > ) ) ]
19+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , > ) ) ]
20+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , > ) ) ]
21+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , > ) ) ]
22+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
23+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
24+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
25+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
26+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
27+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
28+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
29+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
30+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
31+ [ CustomPropertyDrawer ( typeof ( ReadOnlyAnyClass < , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , > ) ) ]
32+ public class ReadOnlyAnyClassDrawer : PropertyDrawer
33+ {
34+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
35+ {
36+ return EditorGUI . GetPropertyHeight ( property , label , includeChildren : true ) ;
37+ }
38+
39+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
40+ {
41+ position . height = EditorGUIUtility . singleLineHeight ;
42+ EditorGUI . PropertyField ( position , property , label , includeChildren : false ) ;
43+ if ( ! property . isExpanded ) return ;
44+
45+ property . serializedObject . Update ( ) ;
46+
47+ var classType = property . GetFieldsByPath ( ) . Last ( ) . field . GetType ( ) . GenericTypeArguments [ 0 ] ;
48+ var fields = classType . GetFields ( AnyClassUtility . DefaultBindingFlags ) ;
49+ var fieldIndex = 0 ;
50+ var end = property . GetEndProperty ( includeInvisible : false ) ;
51+ property . NextVisible ( enterChildren : true ) ;
52+ position . y += EditorGUIUtility . singleLineHeight ;
53+
54+ using ( new EditorGUI . IndentLevelScope ( ) )
55+ {
56+ do
57+ {
58+ var fieldName = fields [ fieldIndex ] . Name ;
59+ if ( fieldName . EndsWith ( "k__BackingField" ) ) fieldName = fieldName . Substring ( 1 , fieldName . Length - "k__BackingField" . Length - 2 ) ;
60+ var fieldLabel = new GUIContent ( fieldName ) ;
61+ EditorGUI . PropertyField ( position , property , fieldLabel , includeChildren : true ) ;
62+ position . y += EditorGUI . GetPropertyHeight ( property , fieldLabel , includeChildren : true ) ;
63+ fieldIndex ++ ;
64+ } while ( property . NextVisible ( enterChildren : false ) && ! SerializedProperty . EqualContents ( property , end ) ) ;
65+ }
66+
67+ property . serializedObject . ApplyModifiedProperties ( ) ;
68+ }
69+ }
70+
71+ static class Extension
72+ {
73+ private static Regex _arrayData = new Regex ( @"^data\[(\d+)\]$" ) ;
74+
75+ public static IEnumerable < ( object field , FieldInfo fi ) > GetFieldsByPath ( this SerializedProperty property )
76+ {
77+ var obj = ( object ) property . serializedObject . targetObject ;
78+ FieldInfo fi = null ;
79+ yield return ( obj , fi ) ;
80+ var pathList = property . propertyPath . Split ( '.' ) ;
81+ for ( var i = 0 ; i < pathList . Length ; i ++ )
82+ {
83+ var fieldName = pathList [ i ] ;
84+ if ( fieldName == "Array" && i + 1 < pathList . Length && _arrayData . IsMatch ( pathList [ i + 1 ] ) )
85+ {
86+ i ++ ;
87+ var itemIndex = int . Parse ( _arrayData . Match ( pathList [ i ] ) . Groups [ 1 ] . Value ) ;
88+ var array = ( IList ) obj ;
89+ obj = array != null && itemIndex < array . Count ? array [ itemIndex ] : null ;
90+ yield return ( obj , fi ) ;
91+ }
92+ else
93+ {
94+ var t = Field ( obj , obj ? . GetType ( ) ?? fi . FieldType , fieldName ) ;
95+ obj = t . field ;
96+ fi = t . fi ;
97+ yield return t ;
98+ }
99+ }
100+
101+ ( object field , FieldInfo fi ) Field ( object declaringObject , Type declaringType , string fieldName )
102+ {
103+ var fieldInfo = declaringType . GetField ( fieldName , BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
104+ var fieldValue = declaringObject == null ? null : fieldInfo . GetValue ( declaringObject ) ;
105+ return ( fieldValue , fieldInfo ) ;
106+ }
107+ }
108+ }
109+ }
0 commit comments