当前位置:   article > 正文

unity webGL与js 交互(获取地址栏URL)_unity webgl js 交互

unity webgl js 交互

1.unity传值给js

unity中:

  1. void Awake()
  2. {
  3. Application.ExternalCall("GetCookie");//GetCookie参数是js方法名字
  4. }

js中:

  1. function GetCookie() {
  2. //var result = document.URL;//获取地址
  3. var result = document.location.href;//获取地址
  4. if (result != "") {
  5. var result = decodeURIComponent(result);
  6. }
  7. console.log("发送消息:Cookie,OnCookie_Callback");
  8. unityInstance.SendMessage("Cookie", "OnCookie_Callback", result);
  9. }
  10. function GetCookie() {
  11. var result = document.URL;
  12. console.log("发送消息:Cookie,OnCookie_Callback");
  13. gameInstance.SendMessage("Cookie", "OnCookie_Callback", result);
  14. }

2.js传值给unity

参数一 Cookie:场景中物体的名称 (最上层父物体名字,否则无法成功传值)
参数二 OnCookie_Callback:方法名称
参数三 result:值

unityInstance.SendMessage("Cookie","OnCookie_Callback",result);

unity中:

  1. public void OnCookie_Callback(string cookie)
  2. {
  3. Debug.Log("收到cookie=" + cookie);
  4. }

完整:

unity场景中物体的名称

 untiy代码:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Test : MonoBehaviour
  5. {
  6. private string NextSceneName;
  7. void Awake()
  8. {
  9. Application.ExternalCall("GetCookie");//GetCookie参数是js方法名字
  10. }
  11. public void OnCookie_Callback(string cookie)
  12. {
  13. Debug.Log("收到cookie=" + cookie);
  14. }
  15. if (temps[i].Contains("username="))
  16. {
  17. string ccc = WWW.UnEscapeURL(temps[i].Replace("username=", "").Trim(), System.Text.Encoding.GetEncoding("utf-8"));//url 编码 转中文
  18. // string ccc = WWW.EscapeURL("猴年马月");//中文转url编码
  19. username = ccc;
  20. }
  21. }

js代码:

  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Unity WebGL Player | Test999</title>
  7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
  8. <link rel="stylesheet" href="TemplateData/style.css">
  9. <script src="TemplateData/UnityProgress.js"></script>
  10. <script src="Build/UnityLoader.js"></script>
  11. <script>
  12. var unityInstance = UnityLoader.instantiate("unityContainer", "Build/Test99.json", {onProgress: UnityProgress});
  13. function GetCookie() {
  14. var result = document.URL;//获取地址
  15. // var result = document.location.href;//获取地址
  16. console.log("发送消息:Cookie,OnCookie_Callback");
  17. gameInstance.SendMessage("Cookie", "OnCookie_Callback", result);
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="webgl-content">
  23. <div id="unityContainer" style="width: 960px; height: 600px"></div>
  24. <div class="footer">
  25. <div class="webgl-logo"></div>
  26. <div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div>
  27. <div class="title">Test999</div>
  28. </div>
  29. </div>
  30. </body>
  31. </html>

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

闽ICP备14008679号