public static object DrawFields(Task task, object obj, GUIContent guiContent) { if (obj == null) { return(null); } List <Type> baseClasses = FieldInspector.GetBaseClasses(obj.GetType()); BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; for (int i = baseClasses.Count - 1; i > -1; i--) { FieldInfo[] fields = baseClasses[i].GetFields(bindingAttr); for (int j = 0; j < fields.Length; j++) { if (!BehaviorDesignerUtility.HasAttribute(fields[j], typeof(NonSerializedAttribute)) && !BehaviorDesignerUtility.HasAttribute(fields[j], typeof(HideInInspector)) && ((!fields[j].IsPrivate && !fields[j].IsFamily) || BehaviorDesignerUtility.HasAttribute(fields[j], typeof(SerializeField))) && (!(obj is ParentTask) || !fields[j].Name.Equals("children"))) { if (guiContent == null) { string name = fields[j].Name; BehaviorDesigner.Runtime.Tasks.TooltipAttribute[] array; if ((array = (fields[j].GetCustomAttributes(typeof(BehaviorDesigner.Runtime.Tasks.TooltipAttribute), false) as BehaviorDesigner.Runtime.Tasks.TooltipAttribute[])).Length > 0) { guiContent = new GUIContent(BehaviorDesignerUtility.SplitCamelCase(name), array[0].Tooltip); } else { guiContent = new GUIContent(BehaviorDesignerUtility.SplitCamelCase(name)); } } EditorGUI.BeginChangeCheck(); object value = FieldInspector.DrawField(task, guiContent, fields[j], fields[j].GetValue(obj)); if (EditorGUI.EndChangeCheck()) { fields[j].SetValue(obj, value); GUI.changed = (true); } guiContent = null; } } } return(obj); }
private static void SaveFields(object obj, string namePrefix) { FieldInfo[] allFields = TaskUtility.GetAllFields(obj.GetType()); for (int i = 0; i < allFields.Length; i++) { if (!BehaviorDesignerUtility.HasAttribute(allFields[i], typeof(NonSerializedAttribute)) && ((!allFields[i].IsPrivate && !allFields[i].IsFamily) || BehaviorDesignerUtility.HasAttribute(allFields[i], typeof(SerializeField))) && (!(obj is ParentTask) || !allFields[i].Name.Equals("children"))) { object value = allFields[i].GetValue(obj); if (!object.ReferenceEquals(value, null)) { BinarySerialization.SaveField(allFields[i].FieldType, namePrefix + allFields[i].Name, value, allFields[i]); } } } }
private static void SaveField(Type fieldType, string fieldName, object value, FieldInfo fieldInfo = null) { string text = fieldType.Name + fieldName; BinarySerialization.fieldSerializationData.typeName.Add(text); BinarySerialization.fieldSerializationData.startIndex.Add(BinarySerialization.fieldIndex); if (typeof(IList).IsAssignableFrom(fieldType)) { Type fieldType2; if (fieldType.IsArray) { fieldType2 = fieldType.GetElementType(); } else { Type type = fieldType; while (!type.IsGenericType) { type = type.BaseType; } fieldType2 = type.GetGenericArguments()[0]; } IList list = value as IList; if (list == null) { BinarySerialization.AddByteData(typeof(int), BinarySerialization.IntToBytes(0)); } else { BinarySerialization.AddByteData(typeof(int), BinarySerialization.IntToBytes(list.Count)); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { if (object.ReferenceEquals(list[i], null)) { BinarySerialization.AddByteData(fieldType2, BinarySerialization.IntToBytes(-1)); } else { BinarySerialization.SaveField(fieldType2, text + i, list[i], fieldInfo); } } } } } else if (typeof(Task).IsAssignableFrom(fieldType)) { if (fieldInfo != null && BehaviorDesignerUtility.HasAttribute(fieldInfo, typeof(InspectTaskAttribute))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.StringToBytes(value.GetType().ToString())); BinarySerialization.SaveFields(value, text); } else { BinarySerialization.AddByteData(fieldType, BinarySerialization.IntToBytes((value as Task).ID)); } } else if (typeof(SharedVariable).IsAssignableFrom(fieldType)) { BinarySerialization.SaveSharedVariable(value as SharedVariable, text); } else if (typeof(UnityEngine.Object).IsAssignableFrom(fieldType)) { BinarySerialization.AddByteData(fieldType, BinarySerialization.IntToBytes(BinarySerialization.fieldSerializationData.unityObjects.Count)); BinarySerialization.fieldSerializationData.unityObjects.Add(value as UnityEngine.Object); } else if (fieldType.Equals(typeof(int)) || fieldType.IsEnum) { BinarySerialization.AddByteData(fieldType, BinarySerialization.IntToBytes((int)value)); } else if (fieldType.Equals(typeof(short))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.Int16ToBytes((short)value)); } else if (fieldType.Equals(typeof(uint))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.UIntToBytes((uint)value)); } else if (fieldType.Equals(typeof(float))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.FloatToBytes((float)value)); } else if (fieldType.Equals(typeof(double))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.DoubleToBytes((double)value)); } else if (fieldType.Equals(typeof(long))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.LongToBytes((long)value)); } else if (fieldType.Equals(typeof(bool))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.BoolToBytes((bool)value)); } else if (fieldType.Equals(typeof(string))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.StringToBytes((string)value)); } else if (fieldType.Equals(typeof(byte))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.ByteToBytes((byte)value)); } else if (fieldType.Equals(typeof(Vector2))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.Vector2ToBytes((Vector2)value)); } else if (fieldType.Equals(typeof(Vector3))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.Vector3ToBytes((Vector3)value)); } else if (fieldType.Equals(typeof(Vector4))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.Vector4ToBytes((Vector4)value)); } else if (fieldType.Equals(typeof(Quaternion))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.QuaternionToBytes((Quaternion)value)); } else if (fieldType.Equals(typeof(Color))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.ColorToBytes((Color)value)); } else if (fieldType.Equals(typeof(Rect))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.RectToBytes((Rect)value)); } else if (fieldType.Equals(typeof(Matrix4x4))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.Matrix4x4ToBytes((Matrix4x4)value)); } else if (fieldType.Equals(typeof(LayerMask))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.IntToBytes(((LayerMask)value).value)); } else if (fieldType.Equals(typeof(AnimationCurve))) { BinarySerialization.AddByteData(fieldType, BinarySerialization.AnimationCurveToBytes((AnimationCurve)value)); } else if (fieldType.IsClass || (fieldType.IsValueType && !fieldType.IsPrimitive)) { if (object.ReferenceEquals(value, null)) { value = Activator.CreateInstance(fieldType, true); } BinarySerialization.SaveFields(value, text); } else { Debug.LogError("Missing Serialization for " + fieldType); } }
private static void SerializeFields(object obj, ref Dictionary <string, object> dict, ref List <UnityEngine.Object> unityObjects) { FieldInfo[] allFields = TaskUtility.GetAllFields(obj.GetType()); for (int i = 0; i < allFields.Length; i++) { if (!BehaviorDesignerUtility.HasAttribute(allFields[i], typeof(NonSerializedAttribute)) && ((!allFields[i].IsPrivate && !allFields[i].IsFamily) || BehaviorDesignerUtility.HasAttribute(allFields[i], typeof(SerializeField))) && (!(obj is ParentTask) || !allFields[i].Name.Equals("children"))) { if (allFields[i].GetValue(obj) != null) { if (typeof(IList).IsAssignableFrom(allFields[i].FieldType)) { IList list = allFields[i].GetValue(obj) as IList; if (list != null) { List <object> list2 = new List <object>(); for (int j = 0; j < list.Count; j++) { if (list[j] == null || object.ReferenceEquals(list[j], null) || list[j].Equals(null)) { list2.Add(-1); } else { Type type = list[j].GetType(); if (list[j] is Task) { Task task = list[j] as Task; list2.Add(task.ID); } else if (list[j] is SharedVariable) { list2.Add(SerializeJSON.SerializeVariable(list[j] as SharedVariable, ref unityObjects)); } else if (list[j] is UnityEngine.Object) { UnityEngine.Object @object = list[j] as UnityEngine.Object; if (!object.ReferenceEquals(@object, null) && @object != null) { list2.Add(unityObjects.Count); unityObjects.Add(@object); } } else if (type.Equals(typeof(LayerMask))) { list2.Add(((LayerMask)list[j]).value); } else if (type.IsPrimitive || type.IsEnum || type.Equals(typeof(string)) || type.Equals(typeof(Vector2)) || type.Equals(typeof(Vector3)) || type.Equals(typeof(Vector4)) || type.Equals(typeof(Quaternion)) || type.Equals(typeof(Matrix4x4)) || type.Equals(typeof(Color)) || type.Equals(typeof(Rect))) { list2.Add(list[j]); } else { Dictionary <string, object> item = new Dictionary <string, object>(); SerializeJSON.SerializeFields(list[j], ref item, ref unityObjects); list2.Add(item); } } } if (list2 != null) { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, list2); } } } else if (allFields[i].FieldType.Equals(typeof(Task)) || allFields[i].FieldType.IsSubclassOf(typeof(Task))) { Task task2 = allFields[i].GetValue(obj) as Task; if (task2 != null) { if (BehaviorDesignerUtility.HasAttribute(allFields[i], typeof(InspectTaskAttribute))) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); dictionary.Add("ObjectType", task2.GetType()); SerializeJSON.SerializeFields(task2, ref dictionary, ref unityObjects); dict.Add(allFields[i].Name, dictionary); } else { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, task2.ID); } } } else if (allFields[i].FieldType.Equals(typeof(SharedVariable)) || allFields[i].FieldType.IsSubclassOf(typeof(SharedVariable))) { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, SerializeJSON.SerializeVariable(allFields[i].GetValue(obj) as SharedVariable, ref unityObjects)); } else if (allFields[i].FieldType.Equals(typeof(UnityEngine.Object)) || allFields[i].FieldType.IsSubclassOf(typeof(UnityEngine.Object))) { UnityEngine.Object object2 = allFields[i].GetValue(obj) as UnityEngine.Object; if (!object.ReferenceEquals(object2, null) && object2 != null) { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, unityObjects.Count); unityObjects.Add(object2); } } else if (allFields[i].FieldType.Equals(typeof(LayerMask))) { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, ((LayerMask)allFields[i].GetValue(obj)).value); } else if (allFields[i].FieldType.IsPrimitive || allFields[i].FieldType.IsEnum || allFields[i].FieldType.Equals(typeof(string)) || allFields[i].FieldType.Equals(typeof(Vector2)) || allFields[i].FieldType.Equals(typeof(Vector3)) || allFields[i].FieldType.Equals(typeof(Vector4)) || allFields[i].FieldType.Equals(typeof(Quaternion)) || allFields[i].FieldType.Equals(typeof(Matrix4x4)) || allFields[i].FieldType.Equals(typeof(Color)) || allFields[i].FieldType.Equals(typeof(Rect))) { dict.Add(allFields[i].FieldType + "," + allFields[i].Name, allFields[i].GetValue(obj)); } else if (allFields[i].FieldType.Equals(typeof(AnimationCurve))) { AnimationCurve animationCurve = allFields[i].GetValue(obj) as AnimationCurve; Dictionary <string, object> dictionary2 = new Dictionary <string, object>(); if (animationCurve.keys != null) { Keyframe[] keys = animationCurve.keys; List <List <object> > list3 = new List <List <object> >(); for (int k = 0; k < keys.Length; k++) { list3.Add(new List <object> { keys[k].time, keys[k].value, keys[k].inTangent, keys[k].outTangent, AnimationUtility.GetKeyLeftTangentMode(animationCurve, k) }); } dictionary2.Add("Keys", list3); } dictionary2.Add("PreWrapMode", animationCurve.preWrapMode); dictionary2.Add("PostWrapMode", animationCurve.postWrapMode); dict.Add(allFields[i].FieldType + "," + allFields[i].Name, dictionary2); } else { Dictionary <string, object> value = new Dictionary <string, object>(); SerializeFields(allFields[i].GetValue(obj), ref value, ref unityObjects); dict.Add(allFields[i].FieldType + "," + allFields[i].Name, value); } } } } }