Unity和js通讯(获取当前网页地址)
1、新建__Internal.jslib文件(文件名随便,后缀是.jslib),放在Plugins文件夹下
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7faebbd198264f68b6271fc04490f72a.png)
2、__Internal.jslib文件内容
mergeInto(LibraryManager.library, {
GetIp: function(){
var returnStr = ""+ window.location.href;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
});
3、新建一个脚本,挂载到场景中,然后给文本框赋值
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
[DllImport("__Internal")]
private static extern string GetIp();
public string m_IpStr;
public Text te;
void Start()
{
te.text = GetIp();
m_IpStr = GetIp();
Debug.Log("m_IpStr::::::::::::" + m_IpStr);
}
}
4、发布测试
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/feb66c3f379b4aeea93a093f034c64f1.png)