1
- using MLAPI ;
2
- using MLAPI . Attributes ;
1
+ using MLAPI . Attributes ;
3
2
using MLAPI . Data ;
4
3
using MLAPI . MonoBehaviours . Core ;
5
4
using System ;
@@ -26,8 +25,13 @@ private void Init(MonoScript script)
26
25
{
27
26
initialized = true ;
28
27
28
+ syncedVarNames . Clear ( ) ;
29
+ networkedVarNames . Clear ( ) ;
30
+ networkedVarFields . Clear ( ) ;
31
+ networkedVarObjects . Clear ( ) ;
32
+
29
33
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 ." ) ;
34
+ networkedVarLabelGuiContent = new GUIContent ( "NetworkedVar" , "This variable is a NetworkedVar. It can not be serialized and can only be changed during runtime ." ) ;
31
35
32
36
FieldInfo [ ] fields = script . GetClass ( ) . GetFields ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
33
37
for ( int i = 0 ; i < fields . Length ; i ++ )
@@ -47,6 +51,16 @@ private void Init(MonoScript script)
47
51
48
52
void RenderNetworkedVar ( int index )
49
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
+ }
50
64
Type type = networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) . GetType ( ) ;
51
65
Type genericType = type . GetGenericArguments ( ) [ 0 ] ;
52
66
@@ -74,37 +88,30 @@ void RenderNetworkedVarValueType<T>(int index) where T : struct
74
88
{
75
89
NetworkedVar < T > var = ( NetworkedVar < T > ) networkedVarFields [ networkedVarNames [ index ] ] . GetValue ( target ) ;
76
90
Type type = typeof ( T ) ;
77
- ValueType val = var . Value ;
91
+ object val = var . Value ;
78
92
string name = networkedVarNames [ index ] ;
79
93
if ( type == typeof ( int ) )
80
- val = EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
94
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
81
95
else if ( type == typeof ( uint ) )
82
- val = ( uint ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
96
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
83
97
else if ( type == typeof ( short ) )
84
- val = ( short ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
98
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
85
99
else if ( type == typeof ( ushort ) )
86
- val = ( ushort ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
100
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
87
101
else if ( type == typeof ( sbyte ) )
88
- val = ( sbyte ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
102
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
89
103
else if ( type == typeof ( byte ) )
90
- val = ( byte ) EditorGUILayout . IntField ( name , Convert . ToInt32 ( val ) ) ;
104
+ val = EditorGUILayout . IntField ( name , ( int ) val ) ;
91
105
else if ( type == typeof ( long ) )
92
- val = EditorGUILayout . LongField ( name , Convert . ToInt64 ( val ) ) ;
106
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
93
107
else if ( type == typeof ( ulong ) )
94
- val = ( ulong ) EditorGUILayout . LongField ( name , Convert . ToInt64 ( val ) ) ;
108
+ val = EditorGUILayout . LongField ( name , ( long ) val ) ;
95
109
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
110
+ val = EditorGUILayout . Toggle ( name , ( bool ) val ) ;
111
+ else if ( type == typeof ( string ) )
112
+ val = EditorGUILayout . TextField ( name , ( string ) val ) ;
104
113
else
105
- {
106
114
EditorGUILayout . LabelField ( "Type not renderable" ) ;
107
- }
108
115
109
116
var . Value = ( T ) val ;
110
117
}
@@ -162,4 +169,4 @@ public override void OnInspectorGUI()
162
169
EditorGUI . EndChangeCheck ( ) ;
163
170
}
164
171
}
165
- }
172
+ }
0 commit comments