我想从我的资产包中加载和实例化资产,但是在统一5.+中,我可以使用如下代码来实现它:注意:我的资产包本身有一个资产,比如:
AssetBundle myLoadedAssetBundle;
public string path;
void Start()
{
LoadAssetBundle(path);
}
void LoadAssetBundle(string bundleUrl)
{
myLoadedAssetBundle = AssetBundle.LoadFromFile(bundleUrl);
Debug.Log(myLoadedAssetBundle == null ? "Faild To Load" : "Succesfully Loaded!");
Debug.Log(myLoadedAssetBundle.mainAsset.name);
Instantiate(myLoadedAssetBundle.mainAsset);
}}
其实连我都用
Debug.Log(myLoadedAssetBundle.mainAsset.name);来记录我的资产包的主资产名称!
但在5+之后,他们一致认为,主流资产已经过时。
我的问题是:
1- 如何加载不知道资产名称的资产包?
2- 如何实例化或分配装载资产包?的sprite
发布于 2019-12-04 22:06:41
根据AssetBundle文档,现在可以调用GetAllAssetNames()作为字符串数组返回包中所有资产名称的列表。您可以通过带有这些资产名称的for循环从包中加载这些资产。
或者,如果您保证每个包只有一个资产,您只需在索引0处获取字符串(不推荐)。
此外,您可以在不知道名称的情况下加载资产,方法是使用LoadAllAssets()返回特定类型的数组(在您的示例中将指定Sprite)。
发布于 2019-12-05 02:02:07
您可以尝试这样的方法:(注意--您需要知道每个资产的目录路径,在这里,我将以一些东西为例)
private List<GameObject> assetBundle = new List<GameObject>();
private GameObject asset1 = Resources.Load("Weapon1") as GameObject;
private GameObject asset2 = Resources.Load("Weapon2") as GameObject;
private GameObject asset3 = Resources.Load("Weapon3") as GameObject;
private void Start()
{
assetBundle.Add(asset1);
assetBundle.Add(asset2);
assetBundle.Add(asset3);
}现在,您可以随时从list by元素访问它们。但是,我不能百分之百地确定Resources.Load()是否能满足您的要求。我大约65% - 70%,尽管如此,我回答。希望能帮上忙!
发布于 2020-06-22 08:19:06
这是我的解决办法
IEnumerator LoadBundleFromDir(string bundleUrl)
{
//which type of asset i want load
GameObject model;
AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromFileAsync(bundleUrl);
yield return bundleRequest;
AssetBundle localeAssetBundle = bundleRequest.assetBundle;
Object[] allBundles = localeAssetBundle.LoadAllAssets();
foreach (Object item in allBundles)
{
if (item.GetType() == typeof(GameObject))
{
AssetBundleRequest assetRequest = localeAssetBundle.LoadAssetAsync<GameObject>(item.name);
yield return assetRequest;
model = assetRequest.asset as GameObject;
localeAssetBundle.Unload(false);
break;
}
}
}https://stackoverflow.com/questions/59185028
复制相似问题