/// <summary> /// Write the values into the stream writer. /// </summary> static void Write(StreamWriter writer, string name, object value, int tab = 0) { bool prefix = false; if (!string.IsNullOrEmpty(name)) { prefix = true; writer.WriteTabs(tab); writer.Write(name); } else if (value != null) { writer.WriteTabs(tab); } if (value != null && !writer.WriteObject(value, prefix)) { Type type = value.GetType(); #if !STANDALONE if (value is AnimationCurve) { AnimationCurve ac = value as AnimationCurve; Keyframe[] kfs = ac.keys; type = typeof(Vector4[]); Vector4[] vs = new Vector4[kfs.Length]; for (int i = 0, imax = kfs.Length; i < imax; ++i) { Keyframe kf = kfs[i]; vs[i] = new Vector4(kf.time, kf.value, kf.inTangent, kf.outTangent); } value = vs; } #endif if (value is TList) { TList list = value as TList; if (prefix) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); if (list.Count > 0) { for (int i = 0, imax = list.Count; i < imax; ++i) { writer.Write('\n'); Write(writer, null, list.Get(i), tab + 1); } } return; } if (value is System.Collections.IList) { System.Collections.IList list = value as System.Collections.IList; if (prefix) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); if (list.Count > 0) { for (int i = 0, imax = list.Count; i < imax; ++i) { writer.Write('\n'); Write(writer, null, list[i], tab + 1); } } return; } if (value is IDataNodeSerializable) { IDataNodeSerializable ser = value as IDataNodeSerializable; DataNode node = new DataNode(); ser.Serialize(node); if (prefix) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); for (int i = 0; i < node.children.size; ++i) { DataNode child = node.children[i]; writer.Write('\n'); child.Write(writer, tab + 1); } return; } #if REFLECTION_SUPPORT if (prefix) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); List <FieldInfo> fields = type.GetSerializableFields(); if (fields.size > 0) { for (int i = 0; i < fields.size; ++i) { FieldInfo field = fields[i]; object val = field.GetValue(value); if (val != null) { writer.Write('\n'); Write(writer, field.Name, val, tab + 1); } } } #endif } }
/// <summary> /// Write the values into the stream writer. /// </summary> static void Write(StreamWriter writer, int tab, string name, object value, bool writeType) { if (string.IsNullOrEmpty(name) && value == null) { return; } WriteTabs(writer, tab); if (name != null) { writer.Write(Escape(name)); if (value == null) { writer.Write('\n'); return; } } Type type = value.GetType(); if (type == typeof(string)) { if (name != null) { writer.Write(" = \""); } writer.Write((string)value); if (name != null) { writer.Write('"'); } writer.Write('\n'); } else if (type == typeof(bool)) { if (name != null) { writer.Write(" = "); } writer.Write((bool)value ? "true" : "false"); writer.Write('\n'); } else if (type == typeof(Int32) || type == typeof(float) || type == typeof(UInt32) || type == typeof(byte) || type == typeof(short) || type == typeof(ushort)) { if (name != null) { writer.Write(" = "); } writer.Write(value.ToString()); writer.Write('\n'); } else if (type == typeof(Vector2)) { Vector2 v = (Vector2)value; writer.Write(name != null ? " = (" : "("); writer.Write(v.x.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.y.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (type == typeof(Vector3)) { Vector3 v = (Vector3)value; writer.Write(name != null ? " = (" : "("); writer.Write(v.x.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.y.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.z.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (type == typeof(Color)) { Color c = (Color)value; writer.Write(name != null ? " = (" : "("); writer.Write(c.r.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(c.g.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(c.b.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(c.a.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (type == typeof(Color32)) { Color32 c = (Color32)value; writer.Write(name != null ? " = 0x" : "0x"); if (c.a == 255) { int i = (c.r << 16) | (c.g << 8) | c.b; writer.Write(i.ToString("X6")); } else { int i = (c.r << 24) | (c.g << 16) | (c.b << 8) | c.a; writer.Write(i.ToString("X8")); } writer.Write('\n'); } else { if (type == typeof(Vector4)) { Vector4 v = (Vector4)value; if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('('); writer.Write(v.x.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.y.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.z.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.w.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (type == typeof(Quaternion)) { Quaternion q = (Quaternion)value; Vector3 v = q.eulerAngles; if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('('); writer.Write(v.x.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.y.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(v.z.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (type == typeof(Rect)) { Rect r = (Rect)value; if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('('); writer.Write(r.x.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(r.y.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(r.width.ToString(CultureInfo.InvariantCulture)); writer.Write(", "); writer.Write(r.height.ToString(CultureInfo.InvariantCulture)); writer.Write(")\n"); } else if (value is TList) { TList list = value as TList; if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('\n'); if (list.Count > 0) { for (int i = 0, imax = list.Count; i < imax; ++i) { Write(writer, tab + 1, null, list.Get(i), false); } } } else if (value is System.Collections.IList) { System.Collections.IList list = value as System.Collections.IList; if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('\n'); if (list.Count > 0) { for (int i = 0, imax = list.Count; i < imax; ++i) { Write(writer, tab + 1, null, list[i], false); } } } else if (value is IDataNodeSerializable) { IDataNodeSerializable ser = value as IDataNodeSerializable; DataNode node = new DataNode(); ser.Serialize(node); if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); writer.Write('\n'); for (int i = 0; i < node.children.size; ++i) { DataNode child = node.children[i]; child.Write(writer, tab + 1); } } else if (value is GameObject) { Debug.LogError("It's not possible to save game objects."); writer.Write('\n'); } else if ((value as Component) != null) { Debug.LogError("It's not possible to save components."); writer.Write('\n'); } else { if (writeType) { if (name != null) { writer.Write(" = "); } writer.Write(Serialization.TypeToName(type)); } writer.Write('\n'); #if REFLECTION_SUPPORT List <FieldInfo> fields = type.GetSerializableFields(); if (fields.size > 0) { for (int i = 0; i < fields.size; ++i) { FieldInfo field = fields[i]; object val = field.GetValue(value); if (val != null) { Write(writer, tab + 1, field.Name, val, true); } } } #endif } } }