Unity WegGL IIS ab包

1.实现的效果

我想实现一个unity发布成WebGL后加载ab包的效果,将ab包和WebGL部署到IIS上。这些博客都太有用了,谢谢分享的人。webgl iis部署  CORS缺少   这个问题没有遇到 大致如下,按照步骤来应该是可以配置加载成功的,弄了两天就是总结一下,为了以后如果还会遇到问题。IIS的安装在这里。如果哪位大神有更多关于webgl的分享可以加我qq:774603077。谢谢

2.Unity发布WebGL

1.ab包加载代码如下,url路径是一会儿iis上部署的ab包的路径:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Test : MonoBehaviour {

	// Use this for initialization
	void Start () {

        StartCoroutine(LoadABB());
    }


    private IEnumerator LoadABB()
    {
        string url = "http://192.168.0.22:8011/AssetBundle/cube.unity3d";
        //WWW www = new WWW(url);

        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        yield return request.SendWebRequest();

        if (request.isNetworkError)
        {
            Debug.Log("错误:" + request.error);
        }
        else
        {
            AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
            GameObject go = ab.LoadAsset<GameObject>("Cube");
            GameObject.Instantiate(go).transform.position = Vector3.one;
            Debug.Log("加载完成了:" + go.name);
        }

    }
}
2.ab包打包代码如下,注意一点打包的环境就行,打包的文件夹是输入的
 BuildTarget.WebGL

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
//********************************************************************
// 文件名: BuildABEditor
// 描述: 打包生成AB包
// 作者: JMM
// 创建时间: 5/21/2018 9:49:42 AM
//********************************************************************
public class BuildABEditor  {
    /// <summary>
    /// 打包
    /// </summary>
    [MenuItem("工具/生成WebGL ab包")]
    static void Build_AssetBundle_WebGL()
    {
        BuildAssetBundleWindow_WebGL win = new BuildAssetBundleWindow_WebGL();
        win.Show();
    }
}
public class BuildAssetBundleWindow_WebGL : EditorWindow
{
    /// <summary>
    /// 打包到哪个文件夹下
    /// </summary>
    string fileName = "D:/AssetBundle/";

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("打包到的文件夹:");
        fileName = EditorGUILayout.TextField(fileName);

        EditorGUILayout.EndHorizontal();
        if (GUILayout.Button("确定打包"))
        {
            BuildPipeline.BuildAssetBundles(fileName, BuildAssetBundleOptions.None, BuildTarget.WebGL);

            Debug.Log("打包成功");
        }
        if (GUILayout.Button("取消打包"))
        {
            this.Close();
        }
    }

}

3.打包注意事项在官网上有,读懂官网就能全部懂了。我遇到的如下,在Player Setting里面这个选项不要勾选,勾选了的话Unity打包发布的时候就会查找咱们有哪些脚本没有用,如果没有用就不会打包到WebGL上:


3.IIS部署

  1. 简单部署在这里《部署过程》。物理路径里面我放了ab包(在AssetBundle下面)和WebGL的发布包。crossdomain.xml也不知道干什么用的,因为百度的时候有人添加了
  2. 部署后的配置web.config如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="true" />
        <staticContent>
            <mimeMap fileExtension=".unity3d" mimeType="text/plain" />
            <mimeMap fileExtension=".unityweb" mimeType="application/binary" />
        </staticContent>
        <httpProtocol>
            <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>
        </httpProtocol>
    </system.webServer>
</configuration>

3.web.config也可以在iis上手动添加,添加MIME类型:


4.添加HTTP响应标头:


5.因为加载ab包需要访问目录,设置目录浏览权限为启动状态,因为启动,现在显示的是禁止。



猜你喜欢

转载自blog.csdn.net/sinat_25682007/article/details/80407571