unity与html通信 自动识别当前web运行平台

unity版本号 2018.4.9 

js脚本后缀名为jslib 这个文件必须放在Unity项目的Plugins文件夹下

mergeInto(LibraryManager.library, {

Hello: function () {
TestSend("Hello, world!");
},
});

c#代码

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class ShowManager : MonoBehaviour
{
float time = 0;
public Text sa;
[DllImport("__Internal")]
private static extern void Hello();
void Start()
{
Hello();
}
public void TestMethod(string s)//打包后的Html中的SendMessage()里面的第二个参数
{
if (s == "true")
{

//pc端触控
View b = GetComponent<View>();
b.enabled = true;
}
else
{

//移动端触控
CameraController a = GetComponent<CameraController>();
a.enabled = true;
}
sa.text = s;
}

发布webgl后 在 html中添加

//判断当前设备

function TestSend(s) {
var w = document.documentElement.clientWidth;
var h = document.documentElement.clientHeight;
console.log(w);
console.log(h);
if (w > h) {
s = "true"
} else {
s = "false"
}
gameInstance.SendMessage("Camera", "TestMethod", s);
}

posted @ 2019-11-21 14:50  zpz904  阅读(686)  评论(0编辑  收藏  举报