Unity-Technologies / UnityDataTools

Experimental tools and libraries for reading and analyzing Unity data files.
Other
575 stars 50 forks source link

Usage from within Unity #3

Open hybridherbst opened 2 years ago

hybridherbst commented 2 years ago

I tried using these tools from inside Unity to analyze import artifacts and for build postprocessors of generated data, which to be best of my understanding fall under SerializedFile.

However, it seems that the app doesn't properly work with domain load and unload, after a first attempt I'm always getting 20220816-192134-APN1-Unity_FBNg

for subsequent tries. Are there plans to get this to work?

I found a relevant comment in the UnityFileSystem constructor that leads me to believe this is a bug / not implemented:

static UnityFileSystem()
{
    // Initialize the native library.
    var r = DllWrapper.Init();
    HandleErrors(r);

    // TODO: should Cleanup be called by the AppDomain.Unload event or something else?
}
hybridherbst commented 2 years ago

This seems to do the trick, instead of the static constructor:

[InitializeOnLoadMethod]
static void Init()
{
    var r = DllWrapper.Init();
    HandleErrors(r);
    AssemblyReloadEvents.beforeAssemblyReload += () => DllWrapper.Cleanup();
}
francis-page commented 2 years ago

Thank you for reporting this. To be honest, I did not design the code to work inside Unity. I will see if I can find a way to fix it so it can work inside or outside Unity without any change.

hybridherbst commented 2 years ago

The above is already enough to get it working in Unity! Could use an ifdef so that the functionality works both in and outside Unity.

Also, it would be interesting to have examples for parsing files with it – I was successfully able to parse many of the asset database artifacts, but I wasn't able to parse scene files, for example (as they seem to live inside memory, not on disk).