学习AssetBundle时,看到网上从AssetBundle加载资源的的方法一般如下:
1 AssetBundle ab=AssetBundle.LoadFromFile("AssetBundles/scene/model.ab"); 2 GameObject go = ab.LoadAsset<GameObject>("Cube");
我自己动手尝试发现LoadAsset一直返回null,unity版本2018.4,我的代码如下:
AssetBundle ab = AssetBundle.LoadFromFile("assets/res/ui/com/prefab1.unity3d"); Object obj = ab.LoadAsset("Assets/Res/UI/Com/prefab1/SKillIcon.prefab");
prefab1.unity3d.manifest如下:
ManifestFileVersion: 0 CRC: 3804775825 Hashes: AssetFileHash: serializedVersion: 2 Hash: a0170fda41556bdab22578bee2082945 TypeTreeHash: serializedVersion: 2 Hash: 75a1acdd0f98b3c749495ab12239c32d HashAppended: 0 ClassTypes: - Class: 1 Script: {instanceID: 0} - Class: 28 Script: {instanceID: 0} - Class: 114 Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3} - Class: 114 Script: {fileID: 1679637790, guid: f70555f144d8491a825f0804e09c671c, type: 3} - Class: 115 Script: {instanceID: 0} - Class: 213 Script: {instanceID: 0} - Class: 222 Script: {instanceID: 0} - Class: 224 Script: {instanceID: 0} - Class: 687078895 Script: {instanceID: 0} Assets: - Assets/Res/UI/Com/prefab1/UiIcon.prefab - Assets/Res/UI/Com/prefab1/SKillIcon.prefab Dependencies:
官方文档如下:
AssetBundle.LoadAsset
public Object LoadAsset (string name);描述
从捆绑包中加载名为 name
的资源。
您应该准确指定 AssetBundleBuild 对象中的名称,即对象的相对路径加文件扩展名。
对照官方文档,name就应该是 Assets/Res/UI/Com/prefab1/SKillIcon.prefab 才对,百思不得其解,最后我试着用AssetBundle的 GetAllAssetNames接口把所有name都打印出来,发现打印的是 AssetBundleBuild.addressableNames里的东西,把name换成addressableNames对应的值后,LoadAsset返回正常了。
也就是说name 代表addressableNames中对应的值。
标签:LoadAsset,Script,instanceID,AssetBundle,Class,name From: https://www.cnblogs.com/gamesky/p/17050393.html