I am creating an asset (ScriptableObject) that is accesible to the user at runtime, with AssetDatabase.CreateAsset
. Inside this asset, I am creating a metadata sub-asset with AssetDatabase.AddObjectToAsset
.
I need this object to be editor-only (not included in builds, and unavailable at runtime).
I know that if the user doesn’t reference this asset, it won’t be included in builds.
So I want to hide this subasset from Project view, so that it isn’t displayed to the user in the editor.
Problem #1: Setting hideFlags
via script doesn’t seem to work with AddObjectToAsset
, the sub-asset is created with m_ObjectHideFlags = 0
anyways.
If I somehow manage to set it (by manually opening the .asset file and setting that field to HideFlags.HideInHierarchy
, which is value 1), then the subasset is correctly hidden in Project view. But I need a way to set this flag via script.
Edit: The serializer works, it was my mistake.
Problem #2: When the sub-asset is hidden, using AssetDatabase.LoadAssetAtPath<MySubAssetType>(mainAssetPath)
returns null.
Workaround for #2: While I was typing, I found out that this code works:
AssetDatabase.LoadAllAssetsAtPath(mainAssetPath).OfType<MySubAssetType>().FirstOrDefault()
I’m leaving the solution here in case anyone needs this.