#AssetImporter.userData improvements

1 messages Β· Page 1 of 1 (latest)

pseudo pulsar
#

It can be desirable for different plugins to be using userData, and I have wondered why there isn't a wrapper API that handles this elegantly.
There is no standard for what is put in this field, so if you're a plugin developer that wants to use it you just have to hope nobody does, or implement some mechanism to append your own data that will probably just end up breaking their implementation anyway.

A simple example would be adding notes to assets, userData seems like a simple no fuss way to do this, until you realise another plugin can conflict with your work.
In the past when I was developing a virtual production pipeline we were using it for: capture notes/priority colors, amending timecode data to timelines (saves having to find tracks), storing loading priorty for scenes, storing fallback binding data for timelines that allowed them to be rebound if they were duplicated without being referenced by a PlayableDirector.

In practice is this API considered off limits for plugin developers, and only intended for in-house asset pipelines. Are there any considerations to improve upon it?

#

I was picturing something like PlayerPrefs/SessionState, where you can key data, and Get with defaults, and then the API wraps that up in some JSON or whatever's suitable.
I also wonder whether such a thing could just add arbitrary key/string pairs into the YAML, or whether that would just cause the YAML parser to slow, as I know the asset pipeline is heavily optimised.

pine merlin
#

I've ran into this as well and have created my own solution in the past to serialize to json and expose an api to safely add/get keys

pseudo pulsar
#

Me too (also JSON), but who knows if ours are compatible πŸ˜›

ripe latch
#

Howdy! So, the userData issue comes up often enough that I went ahead and made a little script to try and help out. The forums also had this question, so I put that information there: https://forum.unity.com/threads/can-we-get-individualized-userdata-please.1445956/

What do you all think?

pine merlin
#

That approach seems to work fine for references to Unity Objects however it seems to lack support for POCOs or pure data

ripe latch
#

I do agree that POCOs wouldn't work, but the pure data part of it could be wrapped in a ScriptableObject no?
So, you have your ScriptableObject and it has a public byte[], where you place your pure data. If you then register that as an external reference, I think this should work πŸ™‚

pseudo pulsar
#

If so, that does seem alright. You could create a dictionary and shove whatever you want there

pine merlin
ripe latch
ripe latch
# pseudo pulsar The importer API is a little confusing to glance at as it can be a bit fiddly wi...

I just gave this a try, and it seems to work. The FBX now has an extra reference to the EditorSettings.asset file:

externalObjects:
  - first:
      type: UnityEditor:EditorSettings
      assembly: UnityEditor.CoreModule
      name: Editor Settings
    second: {fileID: 1, guid: 0000000000000000c000000000000000, type: 0}

The code looks like this:

    [MenuItem("AssetDatabase/AddReferenceToManager")]
    public static void AddReferenceToManager()
    {
        var managerPath = "ProjectSettings/EditorSettings.asset";
        var fullPath = Path.GetFullPath(managerPath);
        var manager = AssetDatabase.LoadAssetAtPath<Object>(managerPath);

        var fbxPath = "Assets/coloredCube.fbx";

        var fbxImporter = (ModelImporter)AssetImporter.GetAtPath(fbxPath);

        fbxImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(manager), manager);
        fbxImporter.SaveAndReimport();

        var externalReferences = fbxImporter.GetExternalObjectMap();
        foreach (var curRef in externalReferences)
        {
            Debug.Log($"Found reference to {curRef.Value.name}");
        }
    }