Unity WebGL发布及Ubuntu Apache服务器部署

Unity 中WebGL的设置

  1. 使用压缩格式:Project Settings->Player->Publishing Settings,将Compression Format设置为Gzip,Decompression Fallback设置为true
  2. 发布:Build Settings->Build,我是发布到新建的html文件夹

Ubuntu Apache web服务器搭建

  1. 安装 apache(Ubuntu20.04)
sudo apt-get install apache2
  1. 设置web资源,将之前发布的html文件夹拷贝到/var/www/html

  2. 添加配置文件,html/Build新建以下.htaccess文件【Unity文档

# This configuration file should be uploaded to the server as "<Application Folder>/Build/.htaccess"
# NOTE: "mod_mime" Apache module must be enabled for this configuration to work.
<IfModule mod_mime.c>

# The following lines are required for builds without decompression fallback, compressed with gzip
RemoveType .gz
AddEncoding gzip .gz
AddType application/octet-stream .data.gz
AddType application/wasm .wasm.gz
AddType application/javascript .js.gz
AddType application/octet-stream .symbols.json.gz

# The following lines are required for builds without decompression fallback, compressed with Brotli
RemoveType .br
RemoveLanguage .br
AddEncoding br .br
AddType application/octet-stream .data.br
AddType application/wasm .wasm.br
AddType application/javascript .js.br
AddType application/octet-stream .symbols.json.br

# The following line improves loading performance for uncompressed builds
AddType application/wasm .wasm

# Uncomment the following line to improve loading performance for gzip-compressed builds with decompression fallback
# AddEncoding gzip .unityweb

# Uncomment the following line to improve loading performance for brotli-compressed builds with decompression fallback
# AddEncoding br .unityweb

</IfModule>
  1. 重启服务器
/etc/init.d/apache2 restart
  1. 查看网页
    输入以下命令查看服务器IP(需要先安装net-tools)
ifconfig

在其他同一局域网的电脑上打开一个支持webGL的浏览器,输入之前查到的IP地址即可查看发布的网页界面

网页修改

打开html/index.html文件进行接下来的修改

  1. 添加按钮(主要是为了调试html与unity交互正常与否)
    在body中加入以下代码
<button style="margin: 20px; padding: 15px" onclick="回调函数名(形参)">按钮显示的文本</button>

下图是我放置的位置
代码插入位置

  1. 声明和赋值实例【十分重要,js调用的关键】
    在script中添加以下代码
var myGameInstance=null;
//其他。。。。。。。。
      script.onload = () => {
        createUnityInstance(canvas, config, (progress) => {
          progressBarFull.style.width = 100 * progress + "%";
        }).then((unityInstance) => {
          myGameInstance=unityInstance;//注意加入这一句
          loadingBar.style.display = "none";
          fullscreenButton.onclick = () => {
            unityInstance.SetFullscreen(1);
          };
        }).catch((message) => {
          alert(message);
        });
      };

  1. js调用Unity函数
    在script中自定义函数
function update(jointIndex,qx,qy,qz,qw)
{
	//最近发现只允许调用至多一个形参的函数
	myGameInstance.SendMessage('GameObject名称','公共函数名',形参)
}

附一:Ubuntu 文件操作常用命令

  1. 复制文件夹
cp A B -r
  1. 删除文件
rm -rf A
  1. 重命名文件
mv A B

附二:Apache 服务器常用操作命令

  1. 查看服务器状态
systemctl status apache2
  1. 开关重启服务器
/etc/init.d apache2 start
/etc/init.d apache2 stop
/etc/init.d apache2 restart
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity WebGL项目中播放视频,你需要使用HTML5标签,例如`<video>`标签。你可以通过以下步骤来实现: 1. 将视频文件添加到Unity项目中,并将其放置在WebGL Build Settings中指定的WebGL输出文件夹中。 2. 在Unity中创建一个UI画布,并添加一个RawImage组件。 3. 在画布中创建一个UI按钮,并将其与一个新的脚本组件相关联。 4. 在脚本中,使用HTML5 `<video>`标签和JavaScript API创建一个视频对象,并将其与RawImage组件关联。 5. 在按钮的OnClick事件中,使用JavaScript API播放视频。 以下是一个示例脚本的基本结构: ```csharp using UnityEngine; using UnityEngine.UI; using System.Collections; public class VideoPlayer : MonoBehaviour { RawImage image; string videoURL = "your_video_url.mp4"; IEnumerator Start() { image = GetComponent<RawImage>(); // Load and wait for the video to be ready UnityWebRequest videoRequest = UnityWebRequest.Get(videoURL); yield return videoRequest.SendWebRequest(); // Create a video object and assign it to the RawImage var videoPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>(); videoPlayer.playOnAwake = false; videoPlayer.source = UnityEngine.Video.VideoSource.Url; videoPlayer.url = videoURL; videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.RenderTexture; videoPlayer.targetTexture = new RenderTexture(1920, 1080, 16, RenderTextureFormat.ARGB32); image.texture = videoPlayer.targetTexture; // Play the video videoPlayer.Prepare(); while (!videoPlayer.isPrepared) { yield return null; } videoPlayer.Play(); } public void PlayVideo() { var videoPlayer = GetComponent<UnityEngine.Video.VideoPlayer>(); videoPlayer.Play(); } } ``` 你可以在按钮的OnClick事件中调用PlayVideo()函数来播放视频。请注意,在这个示例中,视频的URL是硬编码在代码中的,你应该将其替换为你自己的视频URL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值