从javascript调用Unity Android方法,反之亦然 [英] Call Unity Android Method from javascript and vice-versa
问题描述
我正在使用统一的游戏引擎开发一个 android应用,该应用需要通过JavaScript(一个也在移动浏览器中打开的网站)进行某些功能调用,反之亦然.像JavaScript/html一样会打开我的应用程序.然后,它可以命令我的APK中的对象(播放器或相机)在3D环境中移至特定位置.或者相反,该应用程序可以在网站上调用JavaScript方法,该网站可以在同一设备上的移动浏览器中打开.
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. Like a JavaScript/html will open my app. Then it can order an object in my APK (the player or camera) to go at specific position in a 3D environment. Or conversely the app can call the JavaScript method at the Website which is open in the mobile browser on the same device.
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.
似乎我首先必须统一编写一个函数,然后从Java调用该函数,然后该函数将由JavaScript调用
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
JavaScript代码调用-> java方法-> C#方法
感谢@ Mahdi-Malv的评论,我发现Java到C#和C#到Java调用的工作方式(未经测试,但似乎可以工作)
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)
如果要从Java调用c#,请使用UnitySendMessage()函数. 如果要从c#调用Java,请使用AndroidJavaObject和 AndroidJavaClass
If you want to call a c# from java, use UnitySendMessage() function. If you want to call java from c#, use AndroidJavaObject and AndroidJavaClass
现在,答案仍然是不完整的,我的Andriod应用将如何与网页(javascript)通信.
Now the answer is still incomplete that how do my andriod app will communicate with a web page (javascript).
推荐答案
您可以在下面的行中从Android本机代码调用用javascript编写的方法.
You can call the methods which are written in javascript from Android native code by below line.
webView.loadUrl("javascript:<method_name>(<parameters_if_any>)");
// for e.g. javascript:populateScanResult('" + contents + "');
谢谢!
这篇关于从javascript调用Unity Android方法,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!