参考博客
1. 检查模型所用到的Shader是否添加到 Always Included Shaders,如下图
2. 打包的时候可以不添加后缀(添加MIME类型如图2)或者给添加的后缀配置MIME类型(如图3)
application/octet-stream:代表任意的二进制数据传输
3.设置目录浏览权限,默认是禁用的,因为加载ab包需要访问目录,如下图
4.添加HTTP响应标头:
<customHeaders>
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="X-Requested-With,origin,content-type,accept" />
<add name="Access-Control-Allow-Methods" value="GET, PUT,POST,DELETE,HEAD OPTIONS" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
结果如下图
5.在Player Setting中不要勾选Strip Engine Code,如下图
打包AB包的方式,网上很多,我就不记录了,以下是我从服务器加载的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LoadAB : MonoBehaviour
{
void Start()
{
StartCoroutine(InstantiateObject());
}
IEnumerator InstantiateObject()
{
string uri = @"http://localhost:8081/AssetBundles/ga";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log("错误:" + request.error);
}
else
{
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
var game = ab.LoadAsset<GameObject>("Cube");
Instantiate(game);
Debug.Log("加载完成了");
}
}
}