Unity3d tutorial-Passing value from Java class to Unity Script

In this article we are going to see how to pass value(parameter) from a java class to unity script.

Step 1:


 Create a method in unity script and assign a needed parameters. You can save the following

parameters in shared-preference(PlayerPrefs).



     



Step 2:


 Click Save and Export project to Android studio.(Refer article "Export project to android studio").

 Open android studio and create a New Activity("MainActivity.class").

 On "Oncreate"  method Start Intent as follows.








Step 3:


On your exported project select "UnityPlayerActivity.class" and Get extras from bundle as follows.




And use "UnitySendMessage" to call method in unity script.

The unity script stores the given string as TokenDetails.


Note:


  Unityplayer.UnitySendMessage("GameobjectName","AttachedScriptName","Parameter")   has

following parameters.



Conclusion:


     On brief we are passing a parameter as a bundle(Extras) from MainActivity(Nativeapp) and

getting those extras in UnityPlayerActivity. Then we sending those parameters to Unity script.


 



Comments

Post a Comment