Unity Message To Web
1. Create your Unity Script
public class TestScript : MonoBehaviour
{
public class TestDataModel
{
public string str_value;
public int int_value;
public double float_value;
}
public EdgeCanvas m_edgeCanvas;
private void OnGUI()
{
if (GUILayout.Button("SendMessage"))
{
var value = new TestDataModel()
{
str_value = "Hello UWebView2",
int_value = 100,
float_value= 1.234d
};
m_edgeCanvas.PostJsonMessage(JsonUtility.ToJson(value));
}
}
}
2. Create your HTML Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.chrome.webview.addEventListener('message', arg => {
console.log(arg);
});
</script>
</head>
<body>
<h1 style="color:red">Test JavsScirpt</h1>
</body>
</html>
3. Play
Set the address of this HTML in the Source property of EdgeCanvas. Clicking the button will result in the following message.