[英]Call Unity Android Method from javascript and vice-versa
I am developing an android app using unity game-engine that require some function calling from JavaScript(A website which also open in a mobile browser) and vice-versa. 我正在使用统一的游戏引擎开发一个Android应用程序,该应用程序需要从JavaScript(一个也在移动浏览器中打开的网站)进行某些功能调用,反之亦然。 Like a JavaScript/html will open my app.
像JavaScript / html一样会打开我的应用。 Then it can order an object in my APK (the player or camera) to go at specific position in a 3D environment.
然后,它可以命令我的APK中的对象(播放器或相机)在3D环境中移至特定位置。 Or conversely the app can call the JavaScript method at the Website which is open in the mobile browser on the same device.
或者相反,应用程序可以在网站上调用JavaScript方法,该网站在同一设备上的移动浏览器中打开。
I am able to successfully open my unity based APK from a web-page using this answer at Stackoverflow (Thanks to @romaroma) but I don't have any idea that how to do other stuff. 我可以在Stackoverflow上使用此答案从网页成功打开基于统一的APK(感谢@romaroma),但我不知道如何做其他事情。
It seems that first I have to write a function in unity, then call that function from java and then that function will be called by the JavaScript 看来,首先我必须统一编写一个函数,然后从java调用该函数,然后该函数将由JavaScript调用
JavaScript code call->java method->C# method JavaScript代码调用-> java方法-> C#方法
Edit: Thanks to @Mahdi-Malv comment, i found that how Java to C# and C# to Java Calling can work (not tested but seems it will work) 编辑:感谢@ Mahdi-Malv的评论,我发现Java到C#和C#到Java调用可以如何工作(未经测试,但似乎可以工作)
If you want to call ac# from java, use UnitySendMessage() function.
如果要从Java调用ac#,请使用UnitySendMessage()函数。 If you want to call java from c#, use AndroidJavaObject and AndroidJavaClass
如果要从C#调用Java,请使用AndroidJavaObject和AndroidJavaClass
Now the answer is still incomplete that how do my andriod app will communicate with a web page (javascript). 现在,答案仍然是不完整的,我的Andriod应用程序将如何与网页(javascript)通信。
You can call the methods which are written in javascript from Android native code by below line. 您可以在下面的行中调用从Android本机代码中用javascript编写的方法。
webView.loadUrl("javascript:<method_name>(<parameters_if_any>)");
// for e.g. javascript:populateScanResult('" + contents + "');
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.