1
- using MLAPI . Attributes ;
1
+ using MLAPI ;
2
+ using MLAPI . Attributes ;
3
+ using MLAPI . Data ;
2
4
using MLAPI . MonoBehaviours . Core ;
3
5
using System ;
4
6
using System . Collections . Generic ;
@@ -9,26 +11,102 @@ namespace UnityEditor
9
11
{
10
12
[ CustomEditor ( typeof ( NetworkedBehaviour ) , true ) ]
11
13
[ CanEditMultipleObjects ]
12
- public class NetworkedBehaviourInspector : Editor
14
+ public class NetworkedBehaviourEditor : Editor
13
15
{
14
16
private bool initialized ;
15
- protected List < string > syncedVarNames = new List < string > ( ) ;
17
+ private HashSet < string > syncedVarNames = new HashSet < string > ( ) ;
18
+ private List < string > networkedVarNames = new List < string > ( ) ;
19
+ private Dictionary < string , FieldInfo > networkedVarFields = new Dictionary < string , FieldInfo > ( ) ;
20
+ private Dictionary < string , object > networkedVarObjects = new Dictionary < string , object > ( ) ;
16
21
17
22
private GUIContent syncedVarLabelGuiContent ;
23
+ private GUIContent networkedVarLabelGuiContent ;
18
24
19
25
private void Init ( MonoScript script )
20
26
{
21
27
initialized = true ;
22
28
23
29
syncedVarLabelGuiContent = new GUIContent ( "SyncedVar" , "This variable has been marked with the [SyncedVar] attribute." ) ;
30
+ networkedVarLabelGuiContent = new GUIContent ( "[NetworkedVar]" , "This variable has been marked with the [SyncedVar] attribute." ) ;
24
31
25
32
FieldInfo [ ] fields = script . GetClass ( ) . GetFields ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
26
33
for ( int i = 0 ; i < fields . Length ; i ++ )
27
34
{
28
35
Attribute [ ] attributes = ( Attribute [ ] ) fields [ i ] . GetCustomAttributes ( typeof ( SyncedVar ) , true ) ;
29
36
if ( attributes . Length > 0 )
30
37
syncedVarNames . Add ( fields [ i ] . Name ) ;
38
+
39
+ Type ft = fields [ i ] . FieldType ;
40
+ if ( ft . IsGenericType && ft . GetGenericTypeDefinition ( ) == typeof ( NetworkedVar < > ) )
41
+ {
42
+ networkedVarNames . Add ( fields [ i ] . Name ) ;
43
+ networkedVarFields . Add ( fields [ i ] . Name , fields [ i ] ) ;
44
+ }
45
+ }
46
+ }
47
+
48
+ void RenderNetworkedVar ( int index )
49
+ {
50
+ Type type = networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) . GetType ( ) ;
51
+ Type genericType = type . GetGenericArguments ( ) [ 0 ] ;
52
+
53
+ EditorGUILayout . BeginHorizontal ( ) ;
54
+ if ( genericType == typeof ( string ) )
55
+ {
56
+ NetworkedVar < string > var = ( NetworkedVar < string > ) networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) ;
57
+ var . Value = EditorGUILayout . TextField ( networkedVarNames [ index ] , var . Value ) ;
58
+ }
59
+ else if ( genericType . IsValueType )
60
+ {
61
+ MethodInfo method = typeof ( NetworkedBehaviourEditor ) . GetMethod ( "RenderNetworkedVarValueType" , BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
62
+ MethodInfo genericMethod = method . MakeGenericMethod ( genericType ) ;
63
+ genericMethod . Invoke ( this , new object [ ] { ( object ) index } ) ;
31
64
}
65
+ else
66
+ {
67
+ EditorGUILayout . LabelField ( "Type not renderable" ) ;
68
+ }
69
+ GUILayout . Label ( networkedVarLabelGuiContent , EditorStyles . miniLabel , GUILayout . Width ( EditorStyles . miniLabel . CalcSize ( networkedVarLabelGuiContent ) . x ) ) ;
70
+ EditorGUILayout . EndHorizontal ( ) ;
71
+ }
72
+
73
+ void RenderNetworkedVarValueType < T > ( int index ) where T : struct
74
+ {
75
+ NetworkedVar < T > var = ( NetworkedVar < T > ) networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) ;
76
+ Type type = typeof ( T ) ;
77
+ ValueType val = var . Value ;
78
+ string name = networkedVarNames [ index ] ;
79
+ if ( type == typeof ( int ) )
80
+ val = EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
81
+ else if ( type == typeof ( uint ) )
82
+ val = ( uint ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
83
+ else if ( type == typeof ( short ) )
84
+ val = ( short ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
85
+ else if ( type == typeof ( ushort ) )
86
+ val = ( ushort ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
87
+ else if ( type == typeof ( sbyte ) )
88
+ val = ( sbyte ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
89
+ else if ( type == typeof ( byte ) )
90
+ val = ( byte ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
91
+ else if ( type == typeof ( long ) )
92
+ val = EditorGUILayout . LongField ( name , Convert . ToInt64 ( val ) ) ;
93
+ else if ( type == typeof ( ulong ) )
94
+ val = ( ulong ) EditorGUILayout . LongField ( name , Convert . ToInt64 ( val ) ) ;
95
+ else if ( type == typeof ( bool ) )
96
+ val = EditorGUILayout . Toggle ( name , Convert . ToBoolean ( val ) ) ;
97
+ else if ( type == typeof ( char ) )
98
+ {
99
+ char [ ] chars = EditorGUILayout . TextField ( name , Convert . ToString ( val ) ) . ToCharArray ( ) ;
100
+ if ( chars . Length > 0 )
101
+ val = chars [ 0 ] ;
102
+ }
103
+ // TODO - more value types here
104
+ else
105
+ {
106
+ EditorGUILayout . LabelField ( "Type not renderable" ) ;
107
+ }
108
+
109
+ var . Value = ( T ) val ;
32
110
}
33
111
34
112
public override void OnInspectorGUI ( )
@@ -47,6 +125,9 @@ public override void OnInspectorGUI()
47
125
EditorGUI . BeginChangeCheck ( ) ;
48
126
serializedObject . Update ( ) ;
49
127
128
+ for ( int i = 0 ; i < networkedVarNames . Count ; i ++ )
129
+ RenderNetworkedVar ( i ) ;
130
+
50
131
SerializedProperty property = serializedObject . GetIterator ( ) ;
51
132
bool expanded = true ;
52
133
while ( property . NextVisible ( expanded ) )
@@ -78,7 +159,7 @@ public override void OnInspectorGUI()
78
159
expanded = false ;
79
160
}
80
161
serializedObject . ApplyModifiedProperties ( ) ;
81
- EditorGUI . EndChangeCheck ( ) ;
162
+ EditorGUI . EndChangeCheck ( ) ;
82
163
}
83
164
}
84
- }
165
+ }
0 commit comments