unity-web端h5记录

news/2025/3/15 0:43:59/

title: unity-web端h5记录
categories: Unity3d
tags: [unity, web, h5]
date: 2023-02-23 17:00:53
comments: false
mathjax: true
toc: true

unity-web端h5记录


前篇

  • 5款常用的html5游戏引擎以及优缺点分析 - https://imgtec.eetrend.com/blog/2022/100557792.html
  • Unity WebGL 开发指北(完全篇) - https://zhuanlan.zhihu.com/p/475307249

平台适配

文件 io 适配

  • web 请求资源, 全都需要异步加载

构建和运行 WebGL 项目

  • 构建和运行 WebGL 项目 - https://docs.unity3d.com/cn/2021.2/Manual/webgl-building.html

使用 nginx 作为文件服务器

  • 下载地址: http://nginx.org/en/download.html

使用

  • 启动

    C:\server\nginx-1.0.2>start nginx
    or
    C:\server\nginx-1.0.2>nginx.exe
    
  • 停止

    C:\server\nginx-1.0.2>nginx.exe -s stop
    或
    C:\server\nginx-1.0.2>nginx.exe -s quit
    
  • 重新载入配置

    C:\server\nginx-1.0.2>nginx.exe -s reload
    
  • 重新打开日志文件

    C:\server\nginx-1.0.2>nginx.exe -s reopen
    
  • 查看版本

    C:\server\nginx-1.0.2>nginx -v

平台判断

  • 编译宏 https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    #if UNITY_WEBGLLogUtil.D("--- UNITY_WEBGL ");
    #endif
    
  • 运行时

    Application.platform == RuntimePlatform.WebGLPlayer
    

http

使用 best http, 版本最新即可, 如: 2.8.2


websocket

使用 best http, 版本最新即可, 如: 2.8.2

  • Unity下使用BestHTTP插件进行Http和WebSocket通信 - https://blog.csdn.net/foupwang/article/details/104725423

踩坑

未开启 gz

  • 报错

    Unable to parse Build/aaa.framework.js.gz! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: gzip" present. Check browser Console and Devtools Network tab to debug.

官方推荐的是 使用 nginx, 参考: https://docs.unity3d.com/cn/2021.2/Manual/webgl-server-configuration-code-samples.html

  • http {# 增加一个 server 配置server {listen       8811; server_name  localhost;root C:/Users/wilker/Desktop/aaa; # unity 打出的 h5 包index  index.html# location / {#     root   html;#     index  index.html index.htm;# }error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}######################################### Add the following config within http server configuration# ...# On-disk Brotli-precompressed data files should be served with compression enabled:location ~ .+\.(data|symbols\.json)\.br$ {# Because this file is already pre-compressed on disk, disable the on-demand compression on it.# Otherwise nginx would attempt double compression.gzip off;add_header Content-Encoding br;default_type application/octet-stream;}# On-disk Brotli-precompressed JavaScript code files:location ~ .+\.js\.br$ {gzip off; # Do not attempt dynamic gzip compression on an already compressed fileadd_header Content-Encoding br;default_type application/javascript;}# On-disk Brotli-precompressed WebAssembly files:location ~ .+\.wasm\.br$ {gzip off; # Do not attempt dynamic gzip compression on an already compressed fileadd_header Content-Encoding br;# Enable streaming WebAssembly compilation by specifying the correct MIME type for# Wasm files.default_type application/wasm;}# On-disk gzip-precompressed data files should be served with compression enabled:location ~ .+\.(data|symbols\.json)\.gz$ {gzip off; # Do not attempt dynamic gzip compression on an already compressed fileadd_header Content-Encoding gzip;default_type application/octet-stream;}# On-disk gzip-precompressed JavaScript code files:location ~ .+\.js\.gz$ {gzip off; # Do not attempt dynamic gzip compression on an already compressed fileadd_header Content-Encoding gzip;default_type application/javascript;}# On-disk gzip-precompressed WebAssembly files:location ~ .+\.wasm\.gz$ {gzip off; # Do not attempt dynamic gzip compression on an already compressed fileadd_header Content-Encoding gzip;# Enable streaming WebAssembly compilation by specifying the correct MIME type for# Wasm files.default_type application/wasm;}}
    }

移动设备不支持

  • 报错

    WebGL builds are notsupported on mobile devices.

据说不用管这个


中文显示

  • a

http 请求

原生端旧的 best http 插件报错
Invoking error handler due to
ReferenceError: Runtime is not defined
at XMLHttpRequest.http_onload (http://localhost:8811/Build/aaa.framework.js.gz:3:85864)
  • 解决办法: 升级 best http 插件到 2.8.2+ 即可.
UnityWebRequest 请求报错
exception thrown: TypeError: Cannot set properties of undefined (setting '1'),TypeError: Cannot set properties of undefined (setting '1')at _JS_WebRequest_Create (http://localhost:8811/Build/slotsweb.framework.js.gz:3:73755)at http://localhost:8811/Build/slotsweb.wasm.gz:wasm-function[47545]:0x10870bf
  • 参考: https://forum.unity.com/threads/cannot-set-properties-of-undefined-setting-1-when-running-a-unitywebrequest.1248445/
  • 暂时不去解决, 采用升级 best http 插件的办法


http://www.ppmy.cn/news/29083.html

相关文章

项目经理处理团队冲突 5大注意事项

1、在时间、场景、体验矩阵中的5种处理方式 第一种方式:强迫命令,即职位高的一方在不考虑对方感受的情况下,强迫职位低的一方接受自己的意见。这种处理方式的适用场景为重要且紧急,这种方式团队成员的体验感低。 第二种方式&#…

在C#中使用互斥量解决多线程访问共享资源的冲突问题

在阿里云上对互斥量的概述:互斥量的获取是完全互斥的,即同一时刻,互斥量只能被一个任务获取。而信号量按照起始的计数值的配置,可以存在多个任务获取同一信号量的情况,直到计数值减为0,则后续任务无法再获取…

Android - API对应版本名称

APIAndroid版本名称3414.0UpsideDownCake3313.0Tiramisu3212.0Sv23112.0S3011.0R2910.0Q289.0Pie278.1Oreo268.0Oreo257.1Nougat247.0Nougat236.0Marshmallow225.1LOLLIPOP_MR1215.0LOLLIPOP194.4KITKAT194.4KITKAT184.3JELLY_BEAN_MR2174.2/4.2.2JELLY_BEAN_MR1164.1/4.1.1JEL…

Mask R-CNN 算法学习总结

Mask R-CNN 相关知识点整体框架1.Resnet 深度残差学习1.1 目的1.2 深度学习深度增加带来的问题1.3 Resnet实现思想【添加恒等映射】2.线性插值2.1 目的2.2 线性插值原理2.3 为什么使用线性插值?3.FPN 特征金字塔3.1 FPN介绍3.2 为什么使用FPN?3.3 自下而上层【提取特征】3.4 …

【调试方法】基于vs环境下的实用调试技巧

前言: 对万千程序猿来说,在这个世界上如果有比写程序更痛苦的事情,那一定是亲手找出自己编写的程序中的bug(漏洞)。作为新手在我们日常写代码中,经常会出现报错的情况(好的程序员只是比我们见过…

Netty学习(一):Netty概述

一、原生NIO存在的问题 NIO 的类库和API繁杂,使用麻烦:需要熟练掌握Selector、ServerSocketChannel、SocketChannel、ByteBuffer等。需要具备其他的额外技能:要熟悉Java 多线程编程,因为NIO编程涉及到Reactor 模式,你必须对多线程和网络编程…

Ubuntu学习篇

前言 环境:Ubuntu 20.4lts Ubuntu系统跟centos还是有很多区别的,笔者之前一直使用的是centos7.x版本。 镜像下载地址:https://ubuntu.com/download/server#downloads 其他版本下载地址:https://launchpad.net/ubuntu/cdmirrors&a…

Vue3的新特性变化,上手指南!

文章目录一、Vue3相比Vue2,更新了什么变化?二、Proxy 代理响应式原理三、组合式 API (Composition API)setup()函数:ref()函数reactive()函数组合式 setup 中使用 Props 父向子传递参数计算属性watch(数据监视)watchEffect&#x…