要在Unity WebGL项目中播放视频,你需要使用HTML5标签,例如<video>标签。以不同的按钮播放多个视频,每个按钮对应一个视频的代码
时间: 2024-03-06 14:46:41 浏览: 256
可以在Unity中使用WebGL模板,然后在HTML文件中嵌入<video>标签。为了播放多个视频,你可以使用JavaScript代码来控制<video>标签的显示与隐藏。以下是一个示例代码片段,可以为你提供一个基本的思路:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Unity WebGL Video Player</title>
<style>
video {
display: none;
}
</style>
</head>
<body>
<div>
<button onclick="playVideo(1)">Play Video 1</button>
<button onclick="playVideo(2)">Play Video 2</button>
<button onclick="playVideo(3)">Play Video 3</button>
</div>
<video id="video1" width="640" height="360" controls>
<source src="video1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<video id="video2" width="640" height="360" controls>
<source src="video2.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<video id="video3" width="640" height="360" controls>
<source src="video3.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
function playVideo(videoId) {
for (var i = 1; i <= 3; i++) {
if (i == videoId) {
document.getElementById("video" + i).style.display = "block";
document.getElementById("video" + i).play();
} else {
document.getElementById("video" + i).style.display = "none";
document.getElementById("video" + i).pause();
}
}
}
</script>
</body>
</html>
```
在这个示例中,我们定义了三个<video>标签,分别对应三个视频文件。在页面上,我们使用三个按钮来控制视频的播放,每个按钮对应一个视频。当用户点击按钮时,JavaScript代码会根据按钮的ID显示相应的<video>标签,并开始播放视频。在同一时间只有一个<video>标签可见,其他标签会被隐藏。
阅读全文
相关推荐














