public static void LogUnityObject(GameObject obj, UnityObjectLogType logType = UnityObjectLogType.ALL) { #if DEBUG XmlDocument doc = new XmlDocument(); LogUnityObjectProps(obj, doc, doc, logType); doc.Save(@"C:\temp\gameobject.xml"); #endif }
public static void LogComponent <T>(T component, UnityObjectLogType logType = UnityObjectLogType.None) where T : Component { #if DEBUG XmlDocument doc = new XmlDocument(); LogTypedComponent(component, component.GetType(), doc, doc, logType); doc.Save(@"C:\temp\component.xml"); #endif }
private static void LogTypedComponent(Component component, Type type, XmlNode parentNode, XmlDocument doc, UnityObjectLogType logType, bool isFirst = true) { if (IgnoredTypes.Contains(type)) { return; } XmlNode cNode = parentNode.AppendChild(doc.CreateElement(type.Name)); if (isFirst && typeof(Behaviour).IsAssignableFrom(type)) { cNode.Attributes.Append(doc.CreateAttribute("Enabled")).Value = ((Behaviour)component).enabled.ToString(); } LogTypedComponent(component, type.BaseType, cNode, doc, logType, false); BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic; foreach (var member in (type.GetFields(flags).Where(x => !x.IsInitOnly && (x.IsPublic || x.GetCustomAttribute <SerializeField>() != null)) .Select(x => new { x.Name, Type = x.FieldType, Value = (x.IsStatic ? x.GetValue(null) : x.GetValue(component)) })) .Union( type.GetProperties(flags).Where(x => x.SetMethod != null && x.SetMethod.IsPublic && x.GetIndexParameters().Length == 0) .Select(x => new { x.Name, Type = x.PropertyType, Value = (x.GetMethod.IsStatic ? x.GetValue(null) : x.GetValue(component)) })) .OrderBy(x => x.Name)) { XmlNode childNode = cNode.AppendChild(doc.CreateElement(member.Name)); if (member.Value != null && typeof(Component).IsAssignableFrom(member.Type) && ((Component)member.Value).gameObject.transform == null) { childNode.Attributes.Append(doc.CreateAttribute("IsPrefabTemplate")).Value = "True"; LogUnityObjectProps((GameObject)((Component)member.Value).gameObject, childNode, doc, logType); } else if (member.Value != null && (member.Type == typeof(ColorBlock) || member.Type == typeof(SpriteState))) { LogFieldsAndPropertiesForValueType(member.Value, childNode, doc); } else { childNode.InnerText = $"{member.Value}"; } } }
private static void LogUnityObjectProps(GameObject obj, XmlNode parentNode, XmlDocument doc, UnityObjectLogType logType) { XmlNode objNode = parentNode.AppendChild(doc.CreateElement("GameObject")); objNode.Attributes.Append(doc.CreateAttribute("name")).Value = obj.name; objNode.Attributes.Append(doc.CreateAttribute("active")).Value = obj.activeInHierarchy.ToString(); if (logType.HasFlag(UnityObjectLogType.LogComponents)) { XmlNode cNode = objNode.AppendChild(doc.CreateElement("Components")); cNode.Attributes.Append(doc.CreateAttribute("count")).Value = obj.GetComponents <Component>().Length.ToString(); foreach (Component c in obj.GetComponents <Component>()) { LogTypedComponent(c, c.GetType(), cNode, doc, logType); } } if (logType.HasFlag(UnityObjectLogType.LogChildren)) { XmlNode cNode = objNode.AppendChild(doc.CreateElement("Children")); cNode.Attributes.Append(doc.CreateAttribute("count")).Value = obj.transform.childCount.ToString(); for (int i = 0; i < obj.transform.childCount; i++) { GameObject child = obj.transform.GetChild(i).gameObject; LogUnityObjectProps(child, cNode, doc, logType); } } }