我正在构建一个unity3d-webgl应用程序,该应用程序使用assetbundle加载3d模型。到目前为止,我无法加载任何资产捆绑包,在浏览器的控制台窗口中看到的全部是“无法打开存档文件:http://192.168.1.106/webgl/StreamingAssets/Wheels/tis”
我正在使用Unity 2018.3,并且有一台单独的Linux服务器运行Ubuntu(因此没有localhost),我还测试了firefox,chrome和edge的构建,但在所有浏览器中都遇到相同的错误。我什至已经做了一个开发人员构建,它只给了我同样的错误信息,而没有任何其他解释。
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class AssetLoader : MonoBehaviour
{
public GameResources m_resources;
public WheelPrep m_wheelPrep;
[Space(10)]
public string m_extension = ".asset";
[Space(10)]
public GameObject m_wheelHolder;
[Space(10)]
public LoaderStartingEvent m_LoadStartingEvent;
public LoaderCompleteEvent m_LoadCompleteEvent;
public LoadProgressEvent m_ProgressEvent;
private AssetBundle m_aBundle;
public void LoadAsset(string manufacturer, string model)
{
StartCoroutine(LoadAssets(manufacturer, model));
}
IEnumerator LoadAssets(string manufacturer, string model)
{
foreach (Manufacturer man in m_resources.manufacturers.manufacturers)
{
// find the correct manufacturer
if (man.fileName == manufacturer)
{
// send event that manufacturer asset loading has begun
m_LoadStartingEvent.Invoke();
if (m_aBundle == null || m_aBundle.name != man.fileName)
{
// load manufacturer assets
#if UNITY_WEBGL
m_aBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + '/' + man.filePath + '/' + man.fileName + m_extension);
#else
AssetBundleCreateRequest bundleLoadRequest = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + '/' + man.filePath + '/' + man.fileName + m_extension);
// loading progress for asset data
while (!bundleLoadRequest.isDone)
{
m_ProgressEvent.Invoke(bundleLoadRequest.progress + 0.1f);
yield return null;
}
yield return bundleLoadRequest;
m_aBundle = bundleLoadRequest.assetBundle;
#endif
}
foreach (Wheel w in man.wheels)
{
// find the wheel made by the manufacturer
if (w.fileName == model)
{
if (m_resources.m_currentWheel == w)
break;
m_resources.m_currentWheel = w;
AssetBundleRequest assetLoadRequest = m_aBundle.LoadAssetAsync<GameObject>(w.fileName);
yield return assetLoadRequest;
// remove old wheel from scene
if (m_wheelHolder.transform.childCount > 0)
foreach (Transform t in m_wheelHolder.transform)
GameObject.Destroy(t.gameObject);
// place wheel in scene
GameObject instance = Instantiate((GameObject)assetLoadRequest.asset);
instance.name = instance.name.Replace("(Clone)", string.Empty);
instance.transform.parent = m_wheelHolder.transform;
instance.transform.localPosition = new Vector3(0, 0, 0);
m_wheelPrep.Setup();
}
}
// all assets loaded...
m_LoadCompleteEvent.Invoke();
}
}
}
}
[System.Serializable]
public class LoaderStartingEvent : UnityEvent { }
[System.Serializable]
public class LoaderCompleteEvent : UnityEvent { }
[System.Serializable]
public class LoadProgressEvent : UnityEvent<float> { }
我遵循了针对webgl与Windows独立构建资产的统一指南。当我为Windows独立构建项目并使用独立资产捆绑包时,一切正常。我只在webgl中遇到问题。
注意:我起初虽然该文件需要文件扩展名,所以这就是为什么有一个名为m_extension的变量的原因。即使有代码,添加扩展也无济于事,在Unity编辑器中变量为空。