Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raycasting on Unity Button through Webview [Android] #1116

Open
SadevK95 opened this issue Nov 18, 2024 · 6 comments
Open

Raycasting on Unity Button through Webview [Android] #1116

SadevK95 opened this issue Nov 18, 2024 · 6 comments

Comments

@SadevK95
Copy link

Hi. I'm using this webview for load Web based Video Call application. I just want to navigate different Unity scene when the user click on hang-up button on webpage.

  1. My workaround on achieving this is place a unity UI button under the the web hangup button and click it. Can you kindly assist me to achieve this.

  2. Or is there any way to communicate webpage to Unity like trigger an Unity event by listing on the webpage clicks.

@SadevK95 SadevK95 changed the title Raycasting on Unity Button through Webview Raycasting on Unity Button through Webview [Android] Nov 18, 2024
@Volko76
Copy link

Volko76 commented Nov 18, 2024

Hi @SadevK95 ,
Both are possibe but method 2 is recommended as method 1 is experimental (and I have personnaly issues with him for 2021+ versions).

So for method 2, you just need in your webpge to create a html button that calls the js function Unity.call(something) where "something" is a string that will be passed to Unity in the SampleWebView.cs script under webViewObject.Init(cb: (msg)).
msg will be your "something".

So here is an idea of the code :

In you webpage :

In you SampleWebView.cs in unity :
IEnumerator Start()
{
webViewObject = (new GameObject("WebViewObject")).AddComponent();
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
webViewObject.canvas = GameObject.Find("Canvas");
#endif
webViewObject.Init(
cb: (msg) =>
{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.GetComponent().Play();
if(msg = "loadScene1"){
SceneManager.LoadScene("Scene1ForExample");
}
},

@SadevK95
Copy link
Author

Hi @Volko76, Thanks a lot for prompt response. Can you bit elaborate on how to webpage html button code must be construct. Thanks a lot.

@KojiNakamaru
Copy link
Member

You can find a concrete example in the sample app. The sample app invokes Unity.call() as below:

<input type="submit" value="Send Message!" onclick="Unity.call('form?msg=' + document.getElementById('input1').value); return false;"/><br/>

which in turn invokes the cb: callback:

webViewObject.Init(
cb: (msg) =>
{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.GetComponent<Animation>().Play();
},

@Volko76
Copy link

Volko76 commented Nov 19, 2024

@SadevK95

Hi @Volko76, Thanks a lot for prompt response. Can you bit elaborate on how to webpage html button code must be construct. Thanks a lot.

Oh i provided the button example but it seems that Github has deleted it from my comment due to cybersecurity reasons.

I meant :
<button onclick="Unity.call('loadScene1')">Load Scene 1<button>

But @KojiNakamaru provided also a sample project, you can take a look at it.

@SadevK95
Copy link
Author

@Volko76 Thanks a lot. It works now. :)

@Volko76
Copy link

Volko76 commented Nov 20, 2024

@SadevK95 You're welcome ^^, I'm glad you managed to do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants