Esempio n. 1
0
        // We need this method because since 5.3.0 unity no longer
        // supports JsonUtility.FromJson() for ScriptableObjects
        public void ApplyRemoteData(string json)
        {
            float    t         = Time.realtimeSinceStartup;
            JsonData jasonData = JsonMapper.ToObject(json);

            FieldInfo[] fields = GetFields(this);
            foreach (FieldInfo field in fields)
            {
                if (jasonData.Keys.Contains(field.Name))
                {
                    if (field.FieldType.GetInterface("IList") != null)
                    {
                        DataTable dataField = GetTableAttributeForField(field);
                        if (dataField != null)
                        {
                            IList list = field.GetValue(this) as IList;

                            if (list != null)
                            {
                                for (int i = 0; i < jasonData[field.Name].Count; i++)
                                {
                                    try
                                    {
                                        IGameDataModel model = JsonUtility.FromJson(jasonData[field.Name][i].ToJson(), dataField.Type) as IGameDataModel;
                                        if (model != null)
                                        {
                                            bool modelFound = false;
                                            foreach (IGameDataModel currentModel in list)
                                            {
                                                if (currentModel.ID.Equals(model.ID))
                                                {
                                                    modelFound = true;
                                                    currentModel.OverrideValuesFrom(jasonData[field.Name][i].ToJson());
                                                    break;
                                                }
                                            }

                                            if (!modelFound)
                                            {
                                                list.Add(model);
                                            }

                                                                                        #if UNITY_EDITOR
                                            model.SetDataReference(this);
                                                                                        #endif
                                        }
                                    }
                                    catch (System.Exception e)
                                    {
                                        Debug.Log(e.ToString());
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            IGameDataModel currentModel = field.GetValue(this) as IGameDataModel;
                            currentModel.OverrideValuesFrom(jasonData[field.Name].ToJson());
                                                        #if UNITY_EDITOR
                            currentModel.SetDataReference(this);
                                                        #endif
                        }
                        catch (System.Exception e)
                        {
                            Debug.Log(e.ToString());
                        }
                    }
                }
            }

            Debug.Log("Merge Time Elapsed: " + (Time.realtimeSinceStartup - t));
        }
Esempio n. 2
0
        protected virtual void OverrideField(object target, FieldInfo[] fields, string fieldname, JsonData value)
        {
            foreach (FieldInfo field in fields)
            {
                if (field.Name.Equals(fieldname))
                {
                    try
                    {
                        if (value.IsInt || value.IsLong)
                        {
                            field.SetValue(target, int.Parse(value.ToString()));
                        }
                        else if (value.IsBoolean)
                        {
                            field.SetValue(target, bool.Parse(value.ToString()));
                        }
                        else if (value.IsDouble)
                        {
                            field.SetValue(target, float.Parse(value.ToString()));
                        }
                        else if (value.IsString)
                        {
                            field.SetValue(target, value.ToString());
                        }
                        else if (value.IsArray)
                        {
                            System.Array array = System.Array.CreateInstance(field.FieldType.GetElementType(), value.Count);

                            for (int i = 0; i < value.Count; i++)
                            {
                                object      newObject       = System.Activator.CreateInstance(field.FieldType.GetElementType());
                                FieldInfo[] subobjectFields = GetFields(newObject as IGameDataModel);
                                JsonData    subobjectData   = JsonMapper.ToObject(value[i].ToJson());

                                foreach (string key in subobjectData.Keys)
                                {
                                    OverrideField(newObject, subobjectFields, key, subobjectData[key]);
                                }

                                array.SetValue(newObject, i);
                            }

                            field.SetValue(target, array);
                        }
                        else if (value.IsObject)
                        {
                            if (field.FieldType.GetInterface(typeof(IGameDataModel).Name) != null)
                            {
                                IGameDataModel newObject = System.Activator.CreateInstance(field.FieldType) as IGameDataModel;
                                newObject.OverrideValuesFrom(value.ToJson());

                                field.SetValue(target, newObject);
                            }
                            else if (value.Keys.Contains("m_Curve"))
                            {
                                AnimationCurve curve = new AnimationCurve();

                                JsonData keys = value["m_Curve"];

                                for (int i = 0; i < keys.Count; i++)
                                {
                                    Keyframe frame = new Keyframe();
                                    frame.time        = float.Parse(keys[i]["time"].ToString());
                                    frame.value       = float.Parse(keys[i]["value"].ToString());
                                    frame.inTangent   = float.Parse(keys[i]["inSlope"].ToString());
                                    frame.outTangent  = float.Parse(keys[i]["outSlope"].ToString());
                                    frame.tangentMode = int.Parse(keys[i]["tangentMode"].ToString());

                                    curve.AddKey(frame);
                                }

                                field.SetValue(target, curve);
                            }
                        }
                    }
                    catch (System.Exception e)
                    {
                        Debug.Log(e.ToString());
                    }
                    break;
                }
            }
        }