What determines the order of sub-assets in ScriptedImporter?

my expectation was that subasset order would be sorted by either:

  1. the order in which you call AddObjectToAsset(...)
  2. identifier supplied in AddObjectToAsset( identifier, obj );
  3. object name (although objects of the same type sort by name within that same type)

but, it doesn’t seem to be any of them? I’m kinda losing my mind here lol, Is there a way to control this order somehow, ideally from within the ScriptedImporter?

1 Like

I‘m not aware of any way of sorting subassets for serialization.

Just a thought. I would open the serialized asset in a text editor and move the yaml block of one subasset up or down to see if that affects the order. You may have to LoadAsset the asset explicitly to reload the serialized version, If it doesn‘t it‘s likely internally using an unsorted collection.

In that case you can only load all of the subassets and provide a sorted list in your api. You could also create another subasset (SO) which merely contains that sorted list of subassets (update every time you add/remove a subasset). Might make loading faster.

In my case it’s for a ScriptedImporter, so the serialized data lives in /Library in binary, so I can’t really manually reorder them in there as far as I can tell

Which ordering are you thinking about here? The visual order in the project folder? The order in the yaml file? The order in which things are returned by AssetDatabase.LoadAllAssetsAtPath?

the visual order in the project view!

ok I think I’ve got it

  1. Subassets are first sorted by unity’s hardcoded ClassID, then,
  2. they are sorted by object name, within each ClassID

This means:

  1. The most common unity types will go before your custom types
  2. All custom types are sorted by object name amongst each other, because all custom types use the same ClassID (114)
  3. The less common unity types will go after your custom types

Here’s an example:

  • :ice_cube: GameObject (id 1) has to go first as the main asset, if present
    • :soccer: Material (id 21)
    • :globe_with_meridians: Mesh (id 43)
    • :running_woman: Animation (id 111)
    • :point_left: All your custom types go here, sorted by object name, not grouped by type (id 114)
      • :elephant: aaron the elephant
      • :cat2: freya the cat
      • :hippopotamus: richard the hippo
      • :elephant: zachary the second elephant demonstrating he is sorted by name, not type
    • :bread: Texture3D (id 117)
    • :mountain: Terrain Data (id 156)

Extra thanks to @RichardFine for helping me figure this out!

4 Likes

I’m looking at an .fbx file here with a bunch of meshes and animations, and here’s the sub-assets:

image

So it’s animation, prefab, mesh, animation. All of them seems to be completely alphabetic. It might be that the ModelImporter does different things than the ScriptedImporter, though?

oh yeah, huh! I wonder if there’s a special case there.

Is it possible the animations at top are of different ClassID from the ones underneath with the same icons?

image