1
1
using MLAPI . Attributes ;
2
+ using MLAPI . Data ;
2
3
using MLAPI . MonoBehaviours . Core ;
3
4
using System ;
4
5
using System . Collections . Generic ;
@@ -9,26 +10,110 @@ namespace UnityEditor
9
10
{
10
11
[ CustomEditor ( typeof ( NetworkedBehaviour ) , true ) ]
11
12
[ CanEditMultipleObjects ]
12
- public class NetworkedBehaviourInspector : Editor
13
+ public class NetworkedBehaviourEditor : Editor
13
14
{
14
15
private bool initialized ;
15
- protected List < string > syncedVarNames = new List < string > ( ) ;
16
+ private HashSet < string > syncedVarNames = new HashSet < string > ( ) ;
17
+ private List < string > networkedVarNames = new List < string > ( ) ;
18
+ private Dictionary < string , FieldInfo > networkedVarFields = new Dictionary < string , FieldInfo > ( ) ;
19
+ private Dictionary < string , object > networkedVarObjects = new Dictionary < string , object > ( ) ;
16
20
17
21
private GUIContent syncedVarLabelGuiContent ;
22
+ private GUIContent networkedVarLabelGuiContent ;
18
23
19
24
private void Init ( MonoScript script )
20
25
{
21
26
initialized = true ;
22
27
28
+ syncedVarNames . Clear ( ) ;
29
+ networkedVarNames . Clear ( ) ;
30
+ networkedVarFields . Clear ( ) ;
31
+ networkedVarObjects . Clear ( ) ;
32
+
23
33
syncedVarLabelGuiContent = new GUIContent ( "SyncedVar" , "This variable has been marked with the [SyncedVar] attribute." ) ;
34
+ networkedVarLabelGuiContent = new GUIContent ( "NetworkedVar" , "This variable is a NetworkedVar. It can not be serialized and can only be changed during runtime." ) ;
24
35
25
36
FieldInfo [ ] fields = script . GetClass ( ) . GetFields ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
26
37
for ( int i = 0 ; i < fields . Length ; i ++ )
27
38
{
28
39
Attribute [ ] attributes = ( Attribute [ ] ) fields [ i ] . GetCustomAttributes ( typeof ( SyncedVar ) , true ) ;
29
40
if ( attributes . Length > 0 )
30
41
syncedVarNames . Add ( fields [ i ] . Name ) ;
42
+
43
+ Type ft = fields [ i ] . FieldType ;
44
+ if ( ft . IsGenericType && ft . GetGenericTypeDefinition ( ) == typeof ( NetworkedVar < > ) )
45
+ {
46
+ networkedVarNames . Add ( fields [ i ] . Name ) ;
47
+ networkedVarFields . Add ( fields [ i ] . Name , fields [ i ] ) ;
48
+ }
49
+ }
50
+ }
51
+
52
+ void RenderNetworkedVar ( int index )
53
+ {
54
+ if ( ! networkedVarFields . ContainsKey ( networkedVarNames [ index ] ) )
55
+ {
56
+ serializedObject . Update ( ) ;
57
+ SerializedProperty scriptProperty = serializedObject . FindProperty ( "m_Script" ) ;
58
+ if ( scriptProperty == null )
59
+ return ;
60
+
61
+ MonoScript targetScript = scriptProperty . objectReferenceValue as MonoScript ;
62
+ Init ( targetScript ) ;
63
+ }
64
+ Type type = networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) . GetType ( ) ;
65
+ Type genericType = type . GetGenericArguments ( ) [ 0 ] ;
66
+
67
+ EditorGUILayout . BeginHorizontal ( ) ;
68
+ if ( genericType == typeof ( string ) )
69
+ {
70
+ NetworkedVar < string > var = ( NetworkedVar < string > ) networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) ;
71
+ var . Value = EditorGUILayout . TextField ( networkedVarNames [ index ] , var . Value ) ;
72
+ }
73
+ else if ( genericType . IsValueType )
74
+ {
75
+ MethodInfo method = typeof ( NetworkedBehaviourEditor ) . GetMethod ( "RenderNetworkedVarValueType" , BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
76
+ MethodInfo genericMethod = method . MakeGenericMethod ( genericType ) ;
77
+ genericMethod . Invoke ( this , new object [ ] { ( object ) index } ) ;
31
78
}
79
+ else
80
+ {
81
+ EditorGUILayout . LabelField ( "Type not renderable" ) ;
82
+ }
83
+ GUILayout . Label ( networkedVarLabelGuiContent , EditorStyles . miniLabel , GUILayout . Width ( EditorStyles . miniLabel . CalcSize ( networkedVarLabelGuiContent ) . x ) ) ;
84
+ EditorGUILayout . EndHorizontal ( ) ;
85
+ }
86
+
87
+ void RenderNetworkedVarValueType < T > ( int index ) where T : struct
88
+ {
89
+ NetworkedVar < T > var = ( NetworkedVar < T > ) networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) ;
90
+ Type type = typeof ( T ) ;
91
+ object val = var . Value ;
92
+ string name = networkedVarNames [ index ] ;
93
+ if ( type == typeof ( int ) )
94
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
95
+ else if ( type == typeof ( uint ) )
96
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
97
+ else if ( type == typeof ( short ) )
98
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
99
+ else if ( type == typeof ( ushort ) )
100
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
101
+ else if ( type == typeof ( sbyte ) )
102
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
103
+ else if ( type == typeof ( byte ) )
104
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
105
+ else if ( type == typeof ( long ) )
106
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
107
+ else if ( type == typeof ( ulong ) )
108
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
109
+ else if ( type == typeof ( bool ) )
110
+ val = EditorGUILayout . Toggle ( name , ( bool ) val ) ;
111
+ else if ( type == typeof ( string ) )
112
+ val = EditorGUILayout . TextField ( name , ( string ) val ) ;
113
+ else
114
+ EditorGUILayout . LabelField ( "Type not renderable" ) ;
115
+
116
+ var . Value = ( T ) val ;
32
117
}
33
118
34
119
public override void OnInspectorGUI ( )
@@ -47,6 +132,9 @@ public override void OnInspectorGUI()
47
132
EditorGUI . BeginChangeCheck ( ) ;
48
133
serializedObject . Update ( ) ;
49
134
135
+ for ( int i = 0 ; i < networkedVarNames . Count ; i ++ )
136
+ RenderNetworkedVar ( i ) ;
137
+
50
138
SerializedProperty property = serializedObject . GetIterator ( ) ;
51
139
bool expanded = true ;
52
140
while ( property . NextVisible ( expanded ) )
@@ -78,7 +166,7 @@ public override void OnInspectorGUI()
78
166
expanded = false ;
79
167
}
80
168
serializedObject . ApplyModifiedProperties ( ) ;
81
- EditorGUI . EndChangeCheck ( ) ;
169
+ EditorGUI . EndChangeCheck ( ) ;
82
170
}
83
171
}
84
172
}
0 commit comments