赞
踩
1.unity传值给js
unity中:
- void Awake()
- {
- Application.ExternalCall("GetCookie");//GetCookie参数是js方法名字
- }
js中:
- function GetCookie() {
- //var result = document.URL;//获取地址
- var result = document.location.href;//获取地址
- if (result != "") {
- var result = decodeURIComponent(result);
- }
- console.log("发送消息:Cookie,OnCookie_Callback");
- unityInstance.SendMessage("Cookie", "OnCookie_Callback", result);
- }
-
- function GetCookie() {
- var result = document.URL;
- console.log("发送消息:Cookie,OnCookie_Callback");
- gameInstance.SendMessage("Cookie", "OnCookie_Callback", result);
- }
2.js传值给unity
参数一 Cookie:场景中物体的名称 (最上层父物体名字,否则无法成功传值)
参数二 OnCookie_Callback:方法名称
参数三 result:值
unityInstance.SendMessage("Cookie","OnCookie_Callback",result);
unity中:
- public void OnCookie_Callback(string cookie)
- {
- Debug.Log("收到cookie=" + cookie);
-
- }
完整:
unity场景中物体的名称
untiy代码:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Test : MonoBehaviour
- {
- private string NextSceneName;
-
- void Awake()
- {
- Application.ExternalCall("GetCookie");//GetCookie参数是js方法名字
- }
-
- public void OnCookie_Callback(string cookie)
- {
- Debug.Log("收到cookie=" + cookie);
-
- }
-
- if (temps[i].Contains("username="))
- {
- string ccc = WWW.UnEscapeURL(temps[i].Replace("username=", "").Trim(), System.Text.Encoding.GetEncoding("utf-8"));//url 编码 转中文
- // string ccc = WWW.EscapeURL("猴年马月");//中文转url编码
- username = ccc;
- }
- }
js代码:
- <!DOCTYPE html>
- <html lang="en-us">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Unity WebGL Player | Test999</title>
- <link rel="shortcut icon" href="TemplateData/favicon.ico">
- <link rel="stylesheet" href="TemplateData/style.css">
- <script src="TemplateData/UnityProgress.js"></script>
- <script src="Build/UnityLoader.js"></script>
- <script>
- var unityInstance = UnityLoader.instantiate("unityContainer", "Build/Test99.json", {onProgress: UnityProgress});
-
- function GetCookie() {
- var result = document.URL;//获取地址
- // var result = document.location.href;//获取地址
- console.log("发送消息:Cookie,OnCookie_Callback");
- gameInstance.SendMessage("Cookie", "OnCookie_Callback", result);
- }
- </script>
- </head>
- <body>
- <div class="webgl-content">
- <div id="unityContainer" style="width: 960px; height: 600px"></div>
- <div class="footer">
- <div class="webgl-logo"></div>
- <div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div>
- <div class="title">Test999</div>
- </div>
- </div>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。