我已经从服务器加载了Unity中的Model,它在编辑器上运行得很好,因为一切都运行得很好。但是当我在Andriod上运行它时,它不会从这一行前进
GameObject temp = (GameObject)bundle.LoadAsset(assetName);我尝试了不同的方法,但结果是相同的,因为它在编辑器上运行得很好,但在andriod上运行得不好
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);
}
}发布于 2019-08-04 15:53:45
这很可能是由于尝试将加载的对象从资源束转换为GameObject时发生的异常造成的。
(这主要发生在加载的对象不应该是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
}
}https://stackoverflow.com/questions/57344550
复制相似问题