/// <summary> /// Reads the data into the value. /// </summary> /// <param name="value">Value.</param> public override void ReadInto(object value) { if (value == null || string.IsNullOrEmpty(m_Json)) { return; } else if (m_Json[m_Position] == 'n' && PeekString() == "null") { ReadString(); return; } Type type = value.GetType(); bool isGeneric = false; #if (UNITY_WSA || UNITY_WINRT) && !UNITY_EDITOR TypeInfo info = type.GetTypeInfo(); isGeneric = info.IsGenericType; #else isGeneric = type.IsGenericType; #endif if (type == typeof(UnityEngine.GameObject)) { // Skip object start m_Position++; m_IsFirstProperty = true; int layer = 0; bool isStatic = false; string tag = ""; string name = ""; UnityEngine.HideFlags hideFlags = UnityEngine.HideFlags.None; foreach (string property in Properties) { switch (property) { case "layer": layer = ReadProperty <int>(); break; case "isStatic": isStatic = ReadProperty <bool>(); break; case "tag": tag = ReadProperty <string>(); break; case "name": name = ReadProperty <string>(); break; case "hideFlags": hideFlags = ReadProperty <UnityEngine.HideFlags>(); break; } } UnityEngine.GameObject gameObject = value as UnityEngine.GameObject; gameObject.layer = layer; gameObject.isStatic = isStatic; gameObject.tag = tag; gameObject.name = name; gameObject.hideFlags = hideFlags; // Skip comma m_Position++; ReadQoutedString(); // Skip colon and array start m_Position += 2; int length = GetArrayLength(); bool isFirst = true; for (int i = 0; i < length; i++) { if (isFirst) { isFirst = false; } else { // Skip comma m_Position++; } // Skip object start m_Position++; ReadQoutedString(); // Skip colon m_Position++; string typeFullName = ReadQoutedString(); Type componentType = Type.GetType(typeFullName); UnityEngine.Component component = gameObject.GetComponent(componentType); if (component == null) { component = gameObject.AddComponent(componentType); } // Skip comma m_Position++; ReadQoutedString(); // Skip colon m_Position++; ReadInto(component); // Skip object end m_Position++; } // Skip comma and array end m_Position += 2; ReadQoutedString(); // Skip colon and array start m_Position += 2; length = GetArrayLength(); isFirst = true; for (int i = 0; i < length; i++) { if (isFirst) { isFirst = false; } else { // Skip comma m_Position++; } if (gameObject.transform.childCount > i) { UnityEngine.Transform childTransform = gameObject.transform.GetChild(i); ReadInto <UnityEngine.GameObject>(childTransform.gameObject); } else { ReadChild(gameObject); } } // Skip object end m_Position++; } else if (type.IsArray) { // Skip array start m_Position++; Type elementType = type.GetElementType(); int length = GetArrayLength(); Array array = value as Array; if (array.Length < length) { array = Array.CreateInstance(elementType, length); } bool isFirst = true; for (int i = 0; i < array.Length; i++) { if (isFirst) { isFirst = false; } else { // Skip comma m_Position++; } object arrayValue = array.GetValue(i); if (arrayValue == null) { array.SetValue(Read(elementType), i); } else { ReadInto(array.GetValue(i)); } } // Skip array end m_Position++; } else if (isGeneric && type.GetGenericTypeDefinition() == typeof(List <>)) { // Skip array start m_Position++; Type[] genericArgs = type.GetGenericArguments(); int length = GetArrayLength(); IList list = value as IList; bool isFirst = true; for (int i = 0; i < length; i++) { if (isFirst) { isFirst = false; } else { // Skip comma m_Position++; } if (list.Count > i && list[i] != null) { ReadInto(list[i]); } else { list.Add(Read(genericArgs[0])); } } // Skip array end m_Position++; } else if (value is ICollection && value is IEnumerable) { // Skip array start m_Position++; IEnumerable e = value as IEnumerable; bool isFirst = true; foreach (object subValue in e) { if (isFirst) { isFirst = false; } else { // Skip comma m_Position++; } ReadInto(subValue); } // Skip array end m_Position++; } else if (SaveGameTypeManager.HasType(type)) { // Skip object start m_Position++; m_IsFirstProperty = true; SaveGameType saveGameType = SaveGameTypeManager.GetType(type); saveGameType.ReadInto(value, this); // Skip object end m_Position++; } else { ReadObject(type, value); } }
/// <summary> /// Reads the data into the value. /// </summary> /// <param name="value">Value.</param> public virtual void ReadInto(object value) { if (value == null || !m_Reader.ReadBoolean()) { return; } Type type = value.GetType(); bool isGeneric = false; #if (UNITY_WSA || UNITY_WINRT) && !UNITY_EDITOR TypeInfo info = type.GetTypeInfo(); isGeneric = info.IsGenericType; #else isGeneric = type.IsGenericType; #endif if (type == typeof(UnityEngine.GameObject)) { // Skip save game type start m_Reader.ReadByte(); m_Reader.ReadInt64(); int layer = ReadProperty <int>(); bool isStatic = ReadProperty <bool>(); string tag = ReadProperty <string>(); string name = ReadProperty <string>(); UnityEngine.HideFlags hideFlags = ReadProperty <UnityEngine.HideFlags>(); // Skip save game type end m_Reader.ReadByte(); UnityEngine.GameObject gameObject = value as UnityEngine.GameObject; gameObject.layer = layer; gameObject.isStatic = isStatic; gameObject.tag = tag; gameObject.name = name; gameObject.hideFlags = hideFlags; m_Reader.ReadString(); int count = m_Reader.ReadInt32(); for (int i = 0; i < count; i++) { string typeName = m_Reader.ReadString(); Type componentType = Type.GetType(typeName); UnityEngine.Component component = gameObject.GetComponent(componentType); if (component == null) { component = gameObject.AddComponent(componentType); } ReadInto(component); } m_Reader.ReadString(); count = m_Reader.ReadInt32(); for (int i = 0; i < count; i++) { if (gameObject.transform.childCount > i) { UnityEngine.Transform childTransform = gameObject.transform.GetChild(i); ReadInto <UnityEngine.GameObject>(childTransform.gameObject); } else { ReadChild(gameObject); } } } else if (type.IsArray) { Type elementType = type.GetElementType(); int rank = m_Reader.ReadInt32(); int[] lengths = new int[rank]; for (int i = 0; i < rank; i++) { lengths[i] = m_Reader.ReadInt32(); } Array array = Array.CreateInstance(elementType, lengths); int[] indices = new int[array.Rank]; for (int i = 0; i < array.Rank; i++) { indices[i] = array.GetLowerBound(i); } indices[array.Rank - 1]--; bool complete = false; while (!complete) { indices[array.Rank - 1]++; for (int i = array.Rank - 1; i >= 0; i--) { if (indices[i] > array.GetUpperBound(i)) { if (i == 0) { complete = true; break; } for (int j = i; j < array.Rank; j++) { indices[j] = array.GetLowerBound(j); } indices[i - 1]++; } } if (!complete) { object arrayValue = array.GetValue(indices); if (arrayValue == null) { array.SetValue(Read(elementType), indices); } else { ReadInto(array.GetValue(indices)); } } } } else if (isGeneric && type.GetGenericTypeDefinition() == typeof(List <>)) { Type[] genericArgs = type.GetGenericArguments(); IList list = value as IList; int length = m_Reader.ReadInt32(); for (int i = 0; i < length; i++) { if (list.Count > i && list[i] != null) { ReadInto(list[i]); } else { list.Add(Read(genericArgs[0])); } } } else if (value is ICollection && value is IEnumerable) { m_Reader.ReadInt32(); IEnumerable e = value as IEnumerable; foreach (object subValue in e) { ReadInto(subValue); } } else if (SaveGameTypeManager.HasType(type)) { // Skip save game type start m_Reader.ReadByte(); m_Reader.ReadInt64(); SaveGameType saveGameType = SaveGameTypeManager.GetType(type); saveGameType.ReadInto(value, this); // Skip save game type End m_Reader.ReadByte(); } else { ReadIntoObject(type, value); } }