Having a bit of an oddball issue here. I'm attempting to dump a scene to XML using Reflection and Unity crashes/is forced to close.
I'm fairly certain I've pinpointed the issue to a single line:
PropetyInfo.GetValue()
I get the property (prop) from a list of properties generated from a Component (comp) and then I make this call:
prop.GetValue(comp, (object[])null);
When the code hits that line, Unity freezes up and I get the lovely "UnityEditor has stopped working" popup. Sometimes, however, the code will work to a point (there's a debug for this: At Text) and then stop. If the script is run again, then the crash happens.
EDIT: Thanks to Ricardo Arango for trying it out and showing that it worked with simple objects and scripts. Now the problem is that it doesn't work with some scripts, the one that definitely doesn't work is AnB's UIButton. This also happens to be the home of that Text property that I mentioned above.
Here's the code:
public static void WriteFromProject() { //setup the settings and path XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Indent = true; string path = "xml/test.xml"; //create the writer using(XmlWriter writer = XmlWriter.Create(path, xmlSettings)) { //write the header writer.WriteStartDocument(); //write the header writer.WriteStartElement("ISE"); //grab all the objects UnityEngine.Object[] objs = GameObject.FindObjectsOfType(typeof(GameObject)); foreach(GameObject obj in objs) { //open the tag for a gameobject writer.WriteStartElement("GameObject");
//set the attributes
writer.WriteAttributeString("objType", obj.GetType().ToString());
writer.WriteAttributeString("objName", obj.name);
//for each component, push a tag
Component[] comps = obj.GetComponents(typeof(Component));
//TODO: Do more for attaching scripts/components later
foreach(Component comp in comps)
{
//get the type object
Type compType = comp.GetType();
//open the component tag
writer.WriteStartElement("Component");
//set the name as an attribute
writer.WriteAttributeString("Name", compType.FullName);
Debug.Log("Component Name: " + compType.FullName);
//writer.WriteElementString("Component", compType);
//open fields tag
writer.WriteStartElement("Fields");
//then, dump all the fields with it
//do this for call base classes as well
//TODO: Only grab public variables
for(;compType != null; compType = compType.BaseType)
{
//make a subtree for every class/baseclass
writer.WriteStartElement("Class");
//have the class name for an attribute
writer.WriteAttributeString("Name", compType.FullName);
PropertyInfo[] properties = compType.GetProperties();
Debug.Log("Field Length" + properties.Length + " " + "for " + compType.FullName);
foreach(PropertyInfo prop in properties)
{
//if we're not allowed to read or write this, move on
//reason being: 1) Right now we need to read it. 2) When read from xml, we'll need to write it too
if(!(prop.CanRead || prop.CanWrite))
{
continue;
}
if(prop.Name.CompareTo("Text") == 0)
{
Debug.Log("At Text");
}
prop.GetValue(comp, (object[])null);
}
//close the class subtree
writer.WriteEndElement();
}
//close fields tag
writer.WriteEndElement();
//close component tag
writer.WriteEndElement();
}
//close the gameobject element
writer.WriteEndElement();
}
//write end of doc
writer.WriteEndDocument();
}
}
Any thoughts?
asked
Jun 09 '11 at 03:24 PM
Cloud779
16
●
3
●
3
●
5