版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_35080512/article/details/84970825
一、
Unity:创建一个名为Show脚本挂载在Main Camera上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Show : MonoBehaviour {
public Text showText;
// Use this for initialization
void Start () {
//调用外部函数(参数为方法名、参数)
Application.ExternalCall("SayHello", "Hello World!");
}
// Update is called once per frame
void Update () {
}
//获取返回值方法
public void Receive(string s)
{
showText.text = s;
}
}
二、接着发布为WebGL,修改index.html ,如下实例
<!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 | WebGL_Test</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 type="text/javascript" defer="defer" src="https://static.g.mi.com/game/h5sdk/h5-dj-sdk-v.1.0.min.js"> </script>
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webTest.json", {onProgress: UnityProgress});
function SayHello(arge)
{
alert(arge);
var config={"zIndex":9999,"pin":0};
window.hy_dj_sdk.ready(config,function(){
console.log("111111111111111111");
})
var BaseData=hy_dj_sdk.getBaseData();
console.log("11111112223232323"+BaseData);
hy_dj_sdk.reload();
gameInstance.SendMessage("Main Camera", "Receive", "back2");
}
</script>
</head>
<body>
<div class="webgl-content">
<div id="gameContainer" style="width: 960px; height: 600px"></div>
<div class="footer">
<div class="webgl-logo"></div>
<div class="fullscreen" onclick="gameInstance.SetFullscreen(1)"></div>
<div class="title">WebGL_Test</div>
</div>
</div>
</body>
</html>