Ejemplo n.º 1
0
        internal List <INetworkedVar> GetDummyNetworkedVars()
        {
            List <INetworkedVar> networkedVars = new List <INetworkedVar>();

            FieldInfo[] sortedFields = GetFieldInfoForType(GetType());
            for (int i = 0; i < sortedFields.Length; i++)
            {
                Type fieldType = sortedFields[i].FieldType;
                if (fieldType.HasInterface(typeof(INetworkedVar)))
                {
                    INetworkedVar instance = null;
                    if (fieldType.IsGenericTypeDefinition)
                    {
                        Type genericType = fieldType.MakeGenericType(fieldType.GetGenericArguments());
                        instance = (INetworkedVar)Activator.CreateInstance(genericType, true);
                    }
                    else
                    {
                        instance = (INetworkedVar)Activator.CreateInstance(fieldType, true);
                    }
                    instance.SetNetworkedBehaviour(this);
                    networkedVars.Add(instance);
                }
            }
            return(networkedVars);
        }
        void RenderNetworkedVar(int index)
        {
            if (!networkedVarFields.ContainsKey(networkedVarNames[index]))
            {
                serializedObject.Update();
                SerializedProperty scriptProperty = serializedObject.FindProperty("m_Script");
                if (scriptProperty == null)
                {
                    return;
                }

                MonoScript targetScript = scriptProperty.objectReferenceValue as MonoScript;
                Init(targetScript);
            }

            object value = networkedVarFields[networkedVarNames[index]].GetValue(target);

            if (value == null)
            {
                Type          fieldType = networkedVarFields[networkedVarNames[index]].FieldType;
                INetworkedVar var       = (INetworkedVar)Activator.CreateInstance(fieldType, true);
                networkedVarFields[networkedVarNames[index]].SetValue(target, var);
            }

            Type type        = networkedVarFields[networkedVarNames[index]].GetValue(target).GetType();
            Type genericType = type.GetGenericArguments()[0];

            EditorGUILayout.BeginHorizontal();
            if (genericType == typeof(string))
            {
                NetworkedVar <string> var = (NetworkedVar <string>)networkedVarFields[networkedVarNames[index]].GetValue(target);
                var.Value = EditorGUILayout.TextField(networkedVarNames[index], var.Value);
            }
            else if (genericType.IsValueType)
            {
                MethodInfo method        = typeof(NetworkedBehaviourEditor).GetMethod("RenderNetworkedVarValueType", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
                MethodInfo genericMethod = method.MakeGenericMethod(genericType);
                genericMethod.Invoke(this, new object[] { (object)index });
            }
            else
            {
                EditorGUILayout.LabelField("Type not renderable");
            }
            GUILayout.Label(networkedVarLabelGuiContent, EditorStyles.miniLabel, GUILayout.Width(EditorStyles.miniLabel.CalcSize(networkedVarLabelGuiContent).x));
            EditorGUILayout.EndHorizontal();
        }
Ejemplo n.º 3
0
        internal void NetworkedVarInit()
        {
            if (networkedVarInit)
            {
                return;
            }
            networkedVarInit = true;

            FieldInfo[] sortedFields = GetFieldInfoForType(GetType());
            for (int i = 0; i < sortedFields.Length; i++)
            {
                Type fieldType = sortedFields[i].FieldType;
                if (fieldType.HasInterface(typeof(INetworkedVar)))
                {
                    INetworkedVar instance = (INetworkedVar)sortedFields[i].GetValue(this);
                    if (instance == null)
                    {
                        instance = (INetworkedVar)Activator.CreateInstance(fieldType, true);
                        sortedFields[i].SetValue(this, instance);
                    }

                    instance.SetNetworkedBehaviour(this);
                    networkedVarFields.Add(instance);
                }
            }

            //Create index map for channels
            Dictionary <string, int> firstLevelIndex = new Dictionary <string, int>();
            int secondLevelCounter = 0;

            for (int i = 0; i < networkedVarFields.Count; i++)
            {
                string channel = networkedVarFields[i].GetChannel(); //Cache this here. Some developers are stupid. You don't know what shit they will do in their methods
                if (!firstLevelIndex.ContainsKey(channel))
                {
                    firstLevelIndex.Add(channel, secondLevelCounter);
                    channelsForVarGroups.Add(channel);
                    secondLevelCounter++;
                }
                if (firstLevelIndex[channel] >= channelMappedVarIndexes.Count)
                {
                    channelMappedVarIndexes.Add(new HashSet <int>());
                }
                channelMappedVarIndexes[firstLevelIndex[channel]].Add(i);
            }
        }