具体就是说存在两个相同的文件,但是我代码是没有只加载一次的。所以我就很疑问,一直查找原因,发现是因为我在打包的场景中已经有一个模型了,导致我打包出去的包已经有ab 包存在了。我代码是将模型放在服务器然后加载。冲突了,把打包出去的场景的模型删除就可以了!
await ServerManager.Instance.GetModel().ContinueWith(res =>
{
if (res == null)
{
Debug.LogError("获取模型失败");
return;
}
AssetBundle bundle = AssetBundle.LoadFromMemory(res);
if (bundle == null)
{
Debug.LogError("LoadFromMemory失败");
return;
}
string[] assetNames = bundle.GetAllAssetNames();
// 找到以 .prefab 结尾的资源并加载
string prefabPath = assetNames.FirstOrDefault(name => name.EndsWith(".prefab"));
if (prefabPath != null)
{
GameObject loadedObject = bundle.LoadAsset<GameObject>(prefabPath);
if (loadedObject != null)
{
Instantiate(loadedObject, modelParent.transform).GetOrAddComponent<ColliderTest>();
}
else
{
Debug.LogError(" 从 Asset Bundle加载失败");
}
}
else
{
Debug.LogError("Asset Bundle没有prefab");
}
bundle.Unload(false); // 卸载 Asset Bundle
});