Ejemplo n.º 1
0
        /// <summary>
        /// 反序列化图的变量,并返回结果
        /// </summary>
        /// <param name="graphInfo"></param>
        /// <returns></returns>
        public static Dictionary <int, NodeVariable> DeserializeGraphVariableInfo(GraphInfo graphInfo)
        {
            Dictionary <int, NodeVariable> resultDictionary = new Dictionary <int, NodeVariable>();

            int graphVariableCount = graphInfo.GraphVariableInfosLength;

            for (int i = 0; i < graphVariableCount; i++)
            {
                GraphVariableInfo graphVariableInfo = graphInfo.GraphVariableInfos(i).Value;
                int    id          = graphVariableInfo.Id;
                string typeName    = graphVariableInfo.TypeName;
                string valueString = graphVariableInfo.ValueString;

                if (resultDictionary.ContainsKey(id))
                {
                    continue;
                }

                Type variableType = Type.GetType(typeName);
                if (variableType == null)
                {
                    variableType = Type.GetType(typeName + ",UnityEngine");
                    if (variableType == null)
                    {
                        variableType = Type.GetType(typeName + ",Assembly-CSharp");
                    }
                }

                Type baseType    = typeof(NodeVariable <>);
                Type genericType = baseType.MakeGenericType(variableType);

                NodeVariable instanceVariable = Activator.CreateInstance(genericType) as NodeVariable;

                //支持以下类型初始化值
                if (variableType == typeof(int))
                {
                    NodeVariable <int> variable = instanceVariable as NodeVariable <int>;
                    int intValue;
                    if (int.TryParse(valueString, out intValue))
                    {
                        variable.value = intValue;
                    }
                }
                else if (variableType == typeof(float))
                {
                    NodeVariable <float> variable = instanceVariable as NodeVariable <float>;

                    float floatValue;
                    if (float.TryParse(valueString, out floatValue))
                    {
                        variable.value = floatValue;
                    }
                }
                else if (variableType == typeof(bool))
                {
                    NodeVariable <bool> variable = instanceVariable as NodeVariable <bool>;

                    bool boolValue;
                    if (bool.TryParse(valueString, out boolValue))
                    {
                        variable.value = boolValue;
                    }
                }
                else if (variableType == typeof(string))
                {
                    NodeVariable <string> variable = instanceVariable as NodeVariable <string>;

                    variable.value = valueString;
                }

                //其他类型初始时均为null或默认值

                resultDictionary.Add(id, instanceVariable);
            }

            return(resultDictionary);
        }