I want to call a method in C# from Java via JNI. Did someone try this before with Unity or is it even possible?
aww, I was looking for this for hours but finally I found a solution. Code (csharp): UnityPlayer.UnitySendMessage("GameObjectName1", "MethodName1", "Message to send"); Same as in iOS but it's not documented for android or am I missing something?
I'm trying to do this as well, but I can't seem to get it to work. The problem is probably that my java doesn't know the UnityPlayer class or the UnitySendMessage() function. Do I need to add a reference to the UnityEngine or anything in my java library (I'm using Eclipse to generate the .jar file)?
yes you need to import classes.jar files.Also i like to suggest you use AndroidJavaObject for that.It would be easy.We neednt require jni part.
That part I have. Calling Java from C# with AndroidJavaObject works. But I wanna call C# code from within my java code (as I need to wait for a callback in java before I can return the return value to my C# code). And that should be done (as I understand it) with the UnitySendMessage() function from within java. But I can't get that to work.
Hi, I want to call C# function from java. I made a call from java as below: UnityPlayer.UnitySendMessage("GameObjectName1", "MethodName1", "Message to send"); and in the unity script i have created the function GameObjectName1. but, i don't know how to specify a receiver for the function. When i run the code, i get the below error: object GameObjectName1 doesn't have receiver for function MethodName1. I tried to define the GameObjectName1 as static but the same error still appears. Please, help i am stuck with this problem.
GameObjectName1 is the name of the GameObject, not the function. I.e. "Main Camera" or something like that. Your function name should match MethodName1 instead.
To try and resurrect this thread can anyone tell me what package I need to import in Java in order to be able to use UnityPlayer?
For those interested see my post on this thread for my solution: http://forum.unity3d.com/threads/77546-OpenFeint-plugin-that-works-in-Unity-3.2/page6
Hello, the second param in the fuction: UnityPlayer.UnitySendMessage("GameObjectName1", "MethodName1", "Message to send") is the method in javascript code in Unity? I know the part of Android and I know JavaScript programming but I'm very noob with Unity :S for example If in unity i have: PHP: function readMessage(message){ guiText.text = message; } Should I call sendMessage in this way? PHP: UnityPlayer.UnitySendMessage("guiText", "readMessage", "Hello World!") Could u put me some example please? Thanks for All!
Hey, could anyone tell me how to properly use UnitySendMessage? I am trying to get a string of what callback function the android phone called so I can do certain actions in each one. Here are some codes that I have: AndroidActivity.java: Code (csharp): package com.companytest.companygame; import com.unity3d.player.UnityPlayerActivity; import com.unity3d.player.UnityPlayer; import android.content.res.Configuration; import android.os.Bundle; import android.view.KeyEvent; public class AndroidActivity extends UnityPlayerActivity { private UnityPlayer mUnityPlayer; // UnityPlayer.init() should be called before attaching the view to a layout. // UnityPlayer.quit() should be the last thing called; it will terminate the process and not return. @Override protected void onCreate (Bundle savedInstanceState) { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onCreate"); super.onCreate(savedInstanceState); } @Override protected void onDestroy () { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onDestroy"); super.onDestroy(); mUnityPlayer.quit(); } // onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume. @Override protected void onPause() { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onPause"); super.onPause(); mUnityPlayer.pause(); } @Override protected void onResume() { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onResume"); super.onResume(); mUnityPlayer.resume(); } @Override public void onConfigurationChanged(Configuration newConfig) { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onConfigChanged"); super.onConfigurationChanged(newConfig); mUnityPlayer.configurationChanged(newConfig); } @Override public void onWindowFocusChanged(boolean hasFocus) { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onFocusChanged"); super.onWindowFocusChanged(hasFocus); mUnityPlayer.windowFocusChanged(hasFocus); } // Pass any keys not handled by (unfocused) views straight to UnityPlayer @Override public boolean onKeyDown(int keyCode, KeyEvent event) { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onKeyDown"); return mUnityPlayer.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { mUnityPlayer.UnitySendMessage("TestObject", "sendEventStatus", "onKeyUp"); return mUnityPlayer.onKeyUp(keyCode, event); } } AndroidEventHandler.cs: Code (csharp): void sendEventStatus(string eventStatus) { switch(eventStatus) { case "onResume": { Debug.Log("onResume Received"); }break; default:break; } } What I'm trying to do on my code is to try to see if waking the phone up from standby mode would send an onResume message. I have tried but it didn't work so either I am getting the wrong message or I need to change my sendEventStatus function.
Calling UnityPlayer.UnitySendMessage() in onCreate will have no affect since the Unity runtime isn't initialized nor running at that point. In general you need to make sure Unity is running when calling UnitySendMessage(), since if it isn't those messages will just be dropped. Moreover, to get callbacks for onResume/onPause please use OnApplicationPause.
Thank you for the info, and I have tried OnApplicationPause for both true and false but they haven't worked for me. The steps that I am trying to test this on is: 1. Turn on App 2. Click on input to bring up touch screen keyboard 3. Press Power button to go into standby mode. 4. Press Power button again to turn back on and unlock phone. I just want to know if any messages are being called when the phone enters or exits standby mode.
You are saying you don't get any calls to OnApplicationPause (or OnApplicationFocus)? If so, then that sounds like a bug. If you can report the bug and attach your project as repro, then that would be great. Please state information such as what device you are using, it's Android OS version etc. Thanks.
Yeah I haven't been getting any calls on any of those two functions with any of our android devices. I will go report the bug then, thank you.
Our soution does just this without any additional effort, you don't need to bother about anything, you just copy a file and do your work. Try it for free, check us out at our website!
Code (csharp): private AndroidJavaClass cls_Fb; public void Share() { cls_Fb = new AndroidJavaClass("com.example.sample.MainActivity") ; using(AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using(AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) { cls_Fb.CallStatic("loadLeaderboardFromJni","hi"); } } actually I want to initialize the instance to this in onCreate (). But its not getting initialized.
Hi people. I create a plugin that helps with interaction between Java and C#. You can listen for system messages with broadcast receivers, and pick images from gallery using StartActivityForResult/OnActivityResult methods. This is the link for Asset Store: Android System for Unity. Let me know if it attend your needs. Thanks.
I really hate all these opportunists trying to sell their product instead of post a solution to this problem, now I know what plugins I don't have to buy. This is not for sell your product, and we all know there is an asset store with plugins to buy.
Well, instead of you research days and days, and taking more time to launch your game, why not cut time and use something another guy research and made available for you?
Man, don't be a jerk. Do you feel fair If I'm going to charge $20 to solve the problem of this guy, or any post on this forum? Again, this is not the right place to sell your product. Go to the correct forum. This is for FREE help, nobody is posting their problems here expecting to pay for a solution, there are other forums for this.
This is the first result google shows, so here's a link to an example for a callback from unity docs: http://docs.unity3d.com/ScriptReference/AndroidJavaProxy.html
In order to use UnityPlayer in your java plugin (assuming you don't want to export unity project and don't want to extend UnityPlayerActivity), you should: 1) Copy the following file into 'libs' folder of your android plugin's project: C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes \classes.jar Note: I had to rename it to something else (unity_classes.jar) to prevent name conflict between this and the classes.jar file that Unity makes out of the plugin. 2) In 'build.gradle' of your module, add the following in the 'dependencies ' section: provided files('libs/jars/unity_classes.jar') Note: make sure it's 'provided' and not 'compile' because Unity adds this library to your final build again and the project ends up having two copies of that UnityPlayer jar file and complains by saying ''Unable to convert classes into dex format." (you can see my struggle here). 3) Add the following import to your java code: import com.unity3d.player.UnityPlayer; 4) Add your method call: UnityPlayer.UnitySendMessage("Manager", // gameObject name "PluginCallback", // this is a callback in C# "Hello from android plugin"); // msg 5) Build your module and then copy the generated .aar file into Assets\Plugins\Android folder of my Unity project. I assume you have a gameObject (not your script/class name!) named "Manager" in your scene (obviously this is an arbitrary name). Your C# callback should be written in a script that is attached to "Manager" gameObject and have the following signature: Code (CSharp): public void PluginCallback(string msg) { // Do something like: GUI.Label(new Rect(100, 400, 100, 100), msg); }
This last message needs to be in Unity documentation. Seriously, I've spent DAYS trying to find THIS !
A general and flexible way to call your Unity C# code from your android java plugin is using AndroidJavaProxy. There are a good examples here and here.
It significantly saved me time, even though I couldn't utilize UnitySendMessage. However, AndroidJavaProxy proved to be an excellent solution for facilitating communication between the Unity 3D App and the Android App.