当前位置:   article > 正文

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

unity webgl 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做得一个简单的快捷打包)

  1. public class CreateAssetBundles : MonoBehaviour
  2. {
  3. [MenuItem("AssetBundles/Build AssetBundles(WebGL)")] //特性
  4. static void BuildAssetBundle_WebGL()
  5. {
  6. string dir = Application.streamingAssetsPath + "/AssetBundles"; //相对路径
  7. if (!Directory.Exists(dir)) //判断路径是否存在
  8. {
  9. Directory.CreateDirectory(dir);
  10. }
  11. BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.WebGL); //这里是第一点注意事项,BuildTarget类型选择WebGL
  12. }
  13. }

2.3、WebGL端读取AB包

  1. public class MainManager : MonoBehaviour
  2. {
  3. //提示文字
  4. [SerializeField] Text text;
  5. //加载按钮
  6. [SerializeField] Button loadBtn;
  7. public void Start()
  8. {
  9. loadBtn.onClick.AddListener(() => { Load(); });
  10. }
  11. //监听方法
  12. public void Load()
  13. {
  14. IEnumerator ie = load();
  15. //开启协程
  16. StartCoroutine(ie);
  17. }
  18. GameObject go;
  19. string url;
  20. IEnumerator load()
  21. {
  22. if (Application.platform == RuntimePlatform.WindowsEditor)
  23. {
  24. //unity编译器运行时,ab包的加载路径
  25. text.text = "WindowsEditor";
  26. url = Application.streamingAssetsPath + "/AssetBundles/cube.u3d";
  27. }
  28. else if (Application.platform == RuntimePlatform.WebGLPlayer)
  29. {
  30. //网页WebGL运行时,ab包的加载路径
  31. text.text = "WebGLPlayer";
  32. url = Application.streamingAssetsPath + "/AssetBundles/cube.u3d";
  33. }
  34. //UnityWebRequest获取ab包
  35. UnityWebRequest Request = UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
  36. yield return Request.SendWebRequest();
  37. if (Request.result != UnityWebRequest.Result.Success)
  38. {
  39. Debug.Log(Request.error);
  40. }
  41. else
  42. {
  43. //获取ab包内容
  44. AssetBundle ab = DownloadHandlerAssetBundle.GetContent(Request);
  45. //创建指定名称的ab包模型,其中cube代表ab包中模型的名称
  46. GameObject cube = ab.LoadAsset<GameObject>("dimian");
  47. go = Instantiate(cube);
  48. }
  49. }
  50. }

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41261
推荐阅读
相关标签
  

闽ICP备14008679号