UnityWebGL平台打包基础设置,AB包打包读取

WebGL的本地测试华景就不赘述了,找一个教程在本地部署上环境就行了。unity打包webgl 部署到本地Web服务器_unity打包成webgl-CSDN博客

我们主要讲的是在unity中的打包设置,和打AB读取AB的过程和注意事项!

我们就逐步的操作起来吧!

1、下载WebGl平台,调整PC平台基础。(详情Unity 在web上材质显示正常,但是unity端材质显示为紫色_unity模型变成紫红色_一直在努力的平平的博客-CSDN博客转)

 取消勾选Auto Graphics API for Windows

 在Graphics APIs for Windows中添加 OpenGLES3并且置顶

 

 勾选Webgl平台的Decompression Fallback

 好的webGL平台设置就可以了,有其他配置因需求而异。

2、打AB包读取AB包

2.1、打包之前要把自己的shader添加到Always Included Shaders不然出包之后会丢失shader

2.2、编辑快捷打包拓展(这儿只是针对性的对WebGl做得一个简单的快捷打包)

public class CreateAssetBundles : MonoBehaviour
{

    [MenuItem("AssetBundles/Build AssetBundles(WebGL)")] //特性
    static void BuildAssetBundle_WebGL()
    {
        string dir = Application.streamingAssetsPath + "/AssetBundles"; //相对路径
        if (!Directory.Exists(dir))   //判断路径是否存在
        {
            Directory.CreateDirectory(dir);
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.WebGL); //这里是第一点注意事项,BuildTarget类型选择WebGL
    }
}

2.3、WebGL端读取AB包

public class MainManager : MonoBehaviour
{ 

    //提示文字
    [SerializeField] Text text;
    //加载按钮
    [SerializeField] Button loadBtn;

    public void Start()
    {
        loadBtn.onClick.AddListener(() => { Load(); });
    }


    //监听方法
    public void Load()
    {
        IEnumerator ie = load();
        //开启协程
        StartCoroutine(ie);
    }

    GameObject go;

    string url;
    IEnumerator load()
    {

        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            //unity编译器运行时,ab包的加载路径
            text.text = "WindowsEditor";
            url = Application.streamingAssetsPath + "/AssetBundles/cube.u3d";
        }
        else if (Application.platform == RuntimePlatform.WebGLPlayer)
        {
            //网页WebGL运行时,ab包的加载路径
            text.text = "WebGLPlayer";
            url = Application.streamingAssetsPath + "/AssetBundles/cube.u3d";
        }

        //UnityWebRequest获取ab包
        UnityWebRequest Request = UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
        yield return Request.SendWebRequest();

        if (Request.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(Request.error);
        }
        else
        {
            //获取ab包内容
            AssetBundle ab = DownloadHandlerAssetBundle.GetContent(Request);
            //创建指定名称的ab包模型,其中cube代表ab包中模型的名称
            GameObject cube = ab.LoadAsset<GameObject>("dimian");
            go = Instantiate(cube);
        }
    }
}

我这儿是把加载ab的模型写死的,可以自己简单封装一下。

下面就自己去试试吧!!!

猜你喜欢

转载自blog.csdn.net/weixin_52599679/article/details/134379860
今日推荐