1- using MLAPI . Attributes ;
1+ using MLAPI ;
2+ using MLAPI . Attributes ;
3+ using MLAPI . Data ;
24using MLAPI . MonoBehaviours . Core ;
35using System ;
46using System . Collections . Generic ;
@@ -9,26 +11,102 @@ namespace UnityEditor
911{
1012 [ CustomEditor ( typeof ( NetworkedBehaviour ) , true ) ]
1113 [ CanEditMultipleObjects ]
12- public class NetworkedBehaviourInspector : Editor
14+ public class NetworkedBehaviourEditor : Editor
1315 {
1416 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 > ( ) ;
1621
1722 private GUIContent syncedVarLabelGuiContent ;
23+ private GUIContent networkedVarLabelGuiContent ;
1824
1925 private void Init ( MonoScript script )
2026 {
2127 initialized = true ;
2228
2329 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." ) ;
2431
2532 FieldInfo [ ] fields = script . GetClass ( ) . GetFields ( BindingFlags . Public | BindingFlags . Instance | BindingFlags . FlattenHierarchy | BindingFlags . NonPublic ) ;
2633 for ( int i = 0 ; i < fields . Length ; i ++ )
2734 {
2835 Attribute [ ] attributes = ( Attribute [ ] ) fields [ i ] . GetCustomAttributes ( typeof ( SyncedVar ) , true ) ;
2936 if ( attributes . Length > 0 )
3037 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 } ) ;
3164 }
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 ;
32110 }
33111
34112 public override void OnInspectorGUI ( )
@@ -47,6 +125,9 @@ public override void OnInspectorGUI()
47125 EditorGUI . BeginChangeCheck ( ) ;
48126 serializedObject . Update ( ) ;
49127
128+ for ( int i = 0 ; i < networkedVarNames . Count ; i ++ )
129+ RenderNetworkedVar ( i ) ;
130+
50131 SerializedProperty property = serializedObject . GetIterator ( ) ;
51132 bool expanded = true ;
52133 while ( property . NextVisible ( expanded ) )
@@ -78,7 +159,7 @@ public override void OnInspectorGUI()
78159 expanded = false ;
79160 }
80161 serializedObject . ApplyModifiedProperties ( ) ;
81- EditorGUI . EndChangeCheck ( ) ;
162+ EditorGUI . EndChangeCheck ( ) ;
82163 }
83164 }
84- }
165+ }
0 commit comments