1、先来Unity3d官方链接
2、调用html中的方法(老版本Application.ExternalCall();已经弃用)
首先在Unity中创建文件夹Plugins,在文件夹中创建文本改为后缀名为( .jslib)的文件,在里面写入我们Unity中需要调用的方法eg: 代码
mergeInto(LibraryManager.library, {
HelloString: function () {
window.alert("hell world");
},
}):
然后我们在Unity中调用代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
public class LoadHtml : MonoBehaviour {
private Button m_button;
[DllImport("__Internal")]
private static extern void HelloString();
void Start () {
m_button = GetComponent<Button>();
m_button.onClick.AddListener(() => { HelloString(); });
HelloString();
}
在unity里面好像不能直接调用,需要打包出来WebGL才能查看
3、接收html中发送的消息(也就是html往Unity里面发送消息)
unityInstance.SendMessage (obiectName, methodName, value):
objectName是场景中一个对象的名称;methodName是脚本中当前附加到该对象的方法的名称;值可以是字符串、数字或空值。例如:
unityInstance.SendMessage('MyGameObject', 'MyFunction');
unityInstance.SendMessage('MyGameObject', 'MyFunction', 5);
unityInstance.SendMessage('MyGameObject', 'MyFunction', 'MyString');