c# - 将assetBundle转换为游戏对象
问题描述
我已经从服务器加载了 Unity 中的模型,它在编辑器上运行良好,因为一切运行良好。但是当我在 Andriod 上运行它时,它并没有从这条线上继续前进
GameObject temp = (GameObject)bundle.LoadAsset(assetName);
我尝试了不同的方法来做到这一点,但结果是一样的,因为它在编辑器上运行良好,但在安卓上却不行
public IEnumerator DownloadAsset(WWW www, string assetName)
{
yield return www;
AssetBundle bundle = www.assetBundle;
if (www.error == null)
{
GameObject temp = (GameObject)bundle.LoadAsset(assetName);
Instantiate(temp);
}
}
解决方案
这很可能是由于尝试将加载的对象从 Asset Bundle 转换为 GameObject 时出现的异常。
(这主要发生在加载的对象不应该是游戏对象时)
您可以尝试使用as
关键字进行转换。
// Tries to convert to GameObject, returns null if it fails
GameObject temp = _bundle.LoadAsset(_assetName) as GameObject;
if (temp != null){
Instantiate(temp);
} else {
// Failed to convert to gameObject
}
编辑
就像 derHugo 提到的那样,您应该改用UnityWebRequest。
(您所做的是使用WWW,它不会在下载数据时将数据“转换”为 AssetBundle。)
按照在线网站,您应该得到以下结果:
UnityWebRequest www = UnityWebRequest.GetAssetBundle("website Name to get the asset bundle");
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
// Error fetching the asset bundle from the website.
}
else {
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
GameObject temp = bundle.LoadAsset(assetName) as GameObject;
if (temp != null){
Instantiate(temp);
} else {
// Failed to convert to gameObject
}
}
推荐阅读
- wordpress - WooCommerce REST API v3 - 按修改日期过滤产品
- kubernetes - 未成功初始化的 SSH kubernetes pod
- python - 使用迭代在python中找到最小的k
- python - 无法解释正则化标识符:
- android - 如何使用 poll(2) 或 select(2) 服务调用来观察伪文件的 Kotlin 更改
- android - 在 Android 上颤振“始终允许位置”
- angular - 角度应用程序 html *ngfor="ler KitapEvi of kitaplar " 无法访问。你能帮助我吗?
- javascript - 在反应中实现渲染道具的最佳方法
- c# - 无法在 WPF 中使用 Stretch=Fill 获取正确的行位置
- android - 在 Fire TV 中播放 youtube 视频的问题