-
Notifications
You must be signed in to change notification settings - Fork 2.6k
MonoBehaviour export as Json #477
Comments
I did some work on adding JSON support to AssetStudio for TextAsset. Still isn't finished and requires some more testing. I created basic parent/child-based logic for creating hierarchies, but it's still in an early stage, creates very verbose output and may not even be the best way to do it. The structure public class ScriptNode
{
public string name;
public string type;
public object value;
public List<ScriptNode> children;
public ScriptNode this[string childName] => children?.Single((ScriptNode c) => c.name == childName);
public ScriptNode this[int childIndex] => children?.ElementAt(childIndex);
public ScriptNode()
public ScriptNode(string type, string name, object value = null)
public ScriptNode(object typeInstance, string name, bool useInstanceValue = true, object value = null)
public ScriptNode AddChildNode(object typeInstance, string name, bool useInstanceValue = true, object value = null)
public ScriptNode AddChildNode(string type, string name, object value = null)
public ScriptNode AddChildNode(ScriptNode node)
public string ToJson(Formatting formatting = Formatting.Indented, JsonSerializerSettings settings = null)
public override string ToString()
} Example MonoBehaviour public class DisplayObject : MonoBehaviour
{
public GameObject gameObj;
public int childIndex;
public bool hasMasks;
} MonoBehaviour asset JSON output {
"name": "m_MonoBehaviour",
"type": "AssetStudio.MonoBehaviour",
"children": [
{
"name": "m_GameObject",
"type": "AssetStudio.PPtr`1[AssetStudio.GameObject]",
"children": [
{
"name": "m_FileID",
"type": "System.Int32",
"value": 0
},
{
"name": "m_PathID",
"type": "System.Int64",
"value": 4641
},
{
"name": "m_Enabled",
"type": "System.Byte",
"value": 1
}
]
},
{
"name": "m_Script",
"type": "AssetStudio.PPtr`1[AssetStudio.MonoScript]",
"children": [
{
"name": "m_FileID",
"type": "System.Int32",
"value": 0
},
{
"name": "m_PathID",
"type": "System.Int64",
"value": 3311
},
{
"name": "m_Name",
"type": "System.String",
"value": ""
}
]
},
{
"name": "gameObj",
"type": "AssetStudio.PPtr`1[UnityEngine.GameObject]",
"children": [
{
"name": "m_FileID",
"type": "System.Int32",
"value": 0
},
{
"name": "m_PathID",
"type": "System.Int64",
"value": 4641
}
]
},
{
"name": "childIndex",
"type": "System.Int32",
"value": 0
},
{
"name": "hasMasks",
"type": "System.Boolean",
"value": false
}
]
} Examples of how the hierarchy is built if (typeSig.FullName == "System.String")
{
parentNode.AddChildNode(typeDef.FullName, name, reader.ReadAlignedString());
return;
}
...
if (depth != -1 && IsAssignFromUnityObject(typeDef))
{
var pptr = new PPtr<Object>(reader);
var node = parentNode.AddChildNode($"AssetStudio.PPtr`1[{typeDef.FullName}]", name);
node.AddChildNode(pptr.m_FileID, nameof(pptr.m_FileID));
node.AddChildNode(pptr.m_PathID, nameof(pptr.m_PathID));
return;
} It's still using types that AssetStudio is seeing at runtime, instead of Unity runtime types it probably should be showing. I'm not sure if Using the JSON text would be as simple as deserializing it/casting it into the using (StreamReader sr = new StreamReader("MonoBehaviour.json"))
using (JsonReader reader = new JsonTextReader(sr))
{
ScriptNode root = new JsonSerializer().Deserialize<ScriptNode>(reader);
Console.WriteLine(root["hasMasks"].value);
ScriptNode pathId = root["gameObj"]["m_PathID"];
Console.WriteLine($"{pathId.type} {pathId.name}: {pathId.value}");
}
Comments, suggestions? 😄 |
@MichaelPriebe @TheLounger I refactored the parsing part of MonoBehaviour, I think it is now almost perfect and can output json. |
Former-commit-id: 835b6ccaffe70f47f4816c4b1dca8ec88f35e6d1
It would be so cool to be able to export MonoBehaviours as Json, making it easier to sort through data.
The text was updated successfully, but these errors were encountered: