gree / unity-webview

zlib License
2.31k stars 695 forks source link

Locally send msg from Http -> c# in Unity? #472

Open ksay2k opened 5 years ago

ksay2k commented 5 years ago

Hi, I am trying to build the communication between local Http and c# in Unity. I opened LoadHtml() with mytest.html in Assets/Resources. There is a button in mytest.html and some trigger msg would be sent by that button. Once the msg recieved in c#, I would like to execute something in Unity.

Can I implement this function with this Gree Webview?

KojiNakamaru commented 5 years ago

It should be possible. Please check the sample app in this repository. It extracts and loads local html files, https://github.com/gree/unity-webview/blob/bdbbff36652891a5d1a3d567bae051093ed89885/sample/Assets/Scripts/SampleWebView.cs#L109-L131

defines a JavaScript function Unity.call(), https://github.com/gree/unity-webview/blob/bdbbff36652891a5d1a3d567bae051093ed89885/sample/Assets/Scripts/SampleWebView.cs#L34-L39

which is then utilized to send a message from JavaScript https://github.com/gree/unity-webview/blob/bdbbff36652891a5d1a3d567bae051093ed89885/sample/Assets/StreamingAssets/sample.html#L15

to C# side: https://github.com/gree/unity-webview/blob/bdbbff36652891a5d1a3d567bae051093ed89885/sample/Assets/Scripts/SampleWebView.cs#L34-L39