I have a situation where I want to load several asset bundles representing the same time of information. This information is structured on the file system (at build time) and converted to an asset bundle by a script. I will load multiples of these asset bundles at a time and I don't want different aspects of the code to worry about which bundle the information is coming from. Instead I will create a class that handles loading the asset from the asset bundles.
So for example each of my asset bundles will have a Config.xml file. If each asset bundle represents a project, I want to fetch the Config.xml for a specific project using a name like "/Project1/Config.xml" to distinguish it from "/Project2/Config.xml".
Is there a way to get a GUID or other long-lived identifier for an asset so that in my build script I can create a mapping from my naming structure for assets to the actual name of the asset accepted by AssetBundle.Load(...)?
Unity provides the following methods that allow you to uniquely identify asset files:
-
AssetDatabase.GetAssetPath
-
AssetDatabase.AssetPathToGUID
-
AssetDatabase.GUIDToAssetPath (not required by your problem)
If you use an external version control these guids are stored inside the .meta files.
A very nice thing is that when you move an asset file inside the editor the guid stays the same, so the artist may move or rename files without breaking your code or database.
Also you may want to use:
-
BuildPipeline.BuildAssetBundleExplicitAssetNames: allows you to circunvent the problem of files with the same name, may also be used to change the asset file name to include the guid, with makes the whole process name safe.
-
BuildPipeline.PushAssetDependencies: allows you to build bundles that share dependencies, very useful to save memory and disk space when working with bundles.
I've done almost the same thing that you described.
I my case I have several bundles and generate a single xml file that describes wich bundle stores wich resource. I also manage bundle and asset dependencies so several bundles may share data. The user loads the assets by Guid.
There is no long-term UID for objects in Unity. The only thing I can think of that might work is creating some kind of hash from the type and modification time and adding it to the name of the object.