当前位置:   article > 正文

Unity3d内嵌浏览器,网页支持流媒体视频播放_unity flv

unity flv

在各类3D大屏的应用中,播放流媒体(例如监控视频)是比较常见的需求,本文会提供以内嵌浏览器的方案来解决该需求。

首先,功能基于插件3D WebView-Windows-macOS-Web Browser v4.2(官方下载地址:3D WebView: the ultimate cross-platform web browser for Unity | Vuplex Store

并感谢Unity3d windows平台基于3D WebView for Windows and macOS (Web Browser)插件打开内嵌网页支持AR/VR功能实现_unity3d 内嵌网页_小张不爱写代码的博客-CSDN博客

文章提供的干货。

代码如下

HTML:

  1. <body>
  2. <div>
  3. <button onclick="sendMessageToCSharp()">网页向Unity发消息</button>
  4. </div>
  5. <div id="mapDiv" style="position: absolute; width: 100vw; height: 100vh"></div>
  6. <script>
  7. //Unity向网页发消息
  8. if (window.vuplex) {
  9. addMessageListener();
  10. } else {
  11. window.addEventListener('vuplexready', addMessageListener);
  12. }
  13. function addMessageListener() {
  14. window.vuplex.addEventListener('message', function (event) {
  15. let json = event.data;
  16. // > JSON received: { "type": "greeting", "message": "Hello from C#!" }
  17. // alert('JSON received: ' + json);
  18. json = JSON.parse(json)
  19. DrawLine(json.data);
  20. });
  21. }
  22. //网页向Unity发消息
  23. if (window.vuplex) {
  24. sendMessageToCSharp();
  25. } else {
  26. window.addEventListener('vuplexready', sendMessageToCSharp);
  27. }
  28. function sendMessageToCSharp() {
  29. console.log("发送消息到Unity");
  30. window.vuplex.postMessage({ type: 'greeting', message: 'Hello from JavaScript!' });
  31. }
  32. </script>
  33. </body>

C#:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Vuplex.WebView;
  5. [HelpURL("https://blog.csdn.net/qq_41973169/article/details/129179855")]
  6. public class Demo : MonoBehaviour
  7. {
  8. public CanvasWebViewPrefab canvasWebView;
  9. // Start is called before the first frame update
  10. async void Start()
  11. {
  12. if (!canvasWebView) canvasWebView = GetComponent<CanvasWebViewPrefab>();
  13. await canvasWebView.WaitUntilInitialized();
  14. canvasWebView.WebView.LoadUrl("http://127.0.0.1:5500/%E8%BD%A6%E8%BE%86%E8%B7%AF%E7%BA%BF.html");
  15. await canvasWebView.WaitUntilInitialized();
  16. await canvasWebView.WebView.WaitForNextPageLoadToFinish();
  17. //var headerText = await canvasWebView.WebView.ExecuteJavaScript("alert('111111')");
  18. Debug.Log("Unity执行页面JS.");
  19. Send a message after the page has loaded.
  20. //await canvasWebView.WebView.WaitForNextPageLoadToFinish();
  21. canvasWebView.WebView.PostMessage("{\"type\": \"greeting\", \"message\": \"http://http://127.0.0.1:5500/rtp/bfe3ecb51e4ff5166554e.live.flv\"}");
  22. canvasWebView.WebView.MessageEmitted += MessageEmittedCallback;
  23. }
  24. private void MessageEmittedCallback(object sender, EventArgs<string> e)
  25. {
  26. Debug.LogWarning("JSON received: " + e.Value);
  27. }
  28. }

最后视频运行效果:

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

闽ICP备14008679号