7
\$\begingroup\$

I have made my own C# classes in Unity. How can I access them from the Android Activity that starts the UnityPlayer?

Example: I have a C# class called testClass in Unity:

class testClass {
    public static string myString = "test string";
}

From the Android activity in Java, I want to access a member of that class:

string str = testClass.myString;

Is this possible? If so, how? Is there another way to do this? In the end, I basically want to communicate between my Android activity and the UnityPlayer object.


Edit: I found a solution.

I looked at building Android plugins for Unity but this wasn't satisfactory to me.

I ended up building a socket client-server interface in Unity with C# and another one in Java for the Android app: Unity listens on port X and broadcasts on port Y. The Android activity listens on port Y and broadcasts on port X. This is necessary as both interfaces are running on the same host.

So that's how I solved my problem, but I'm open for any suggestions if anyone knows a better way of communicating between the Unityplayer and your app.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You should post that last bit with the solution you found as an answer. (It's totally fine—even encouraged—to answer your own questions.) \$\endgroup\$
    – Anko
    Commented Oct 11, 2014 at 19:44

3 Answers 3

1
\$\begingroup\$

The degree of access you are expecting may or may not be doable. However there is a simple callback function designed for sending messages from native plugins back to Unity: UnitySendMessage(). That will call the named method on the named object; in other words, you need to have an object with the desired method in the scene and then your plugin can call that. Typically most plugins create an empty object with DontDestroyOnLoad and a series of callbacks.

For example, look for that function explained on this page http://docs.unity3d.com/Manual/PluginsForIOS.html

Note that page is about iOS and not Android. However it works the same way in Android; in order to call UnitySendMessage() from Java code, you must include the Unity jar. That import is explained on this page; scroll all the way down to "Extending the UnityPlayerActivity" http://docs.unity3d.com/Manual/PluginsForAndroid.html

and another page explaining that command http://forum.unity3d.com/threads/android-plugin-call-c-from-java.78598/

\$\endgroup\$
0
\$\begingroup\$

If you just want to access the value of a member variable of any unity class or want to call a method of Unity class from Android activity, you can always use JNI for this. You'll need to create a C++ interface between them as well(Dunno if it can be done without C++ interface, but I believe that's required).

Check this link for a quick solution.

\$\endgroup\$
2
  • \$\begingroup\$ I actually played with ikvm a bit to see what I could achieve there, it's pretty much the same concept just in reverse, it makes a dll from a jar and can make jars from .net assemblies as well. Great tool and it works 100% in a normal .net app, but when used in Unity it throws some java exceptions that prevents you from exporting your project even though the exceptions doesn't stop it from working. If I could get around it it'd be perfect. I can't handle the exceptions globally either due to the fact that they are occuring within the wrapped jar/dll \$\endgroup\$ Commented May 13, 2014 at 15:37
  • \$\begingroup\$ Unity never stops when there're any exception in any of the script. It'll just skip rest of the code in the same frame and will move on. I suppose there might be some unsupported API calls in the tool. However a raw JNI call is guaranteed to work since it's been used in many Android plugin. Try a simple method to call first, if that suits your need or not. \$\endgroup\$
    – noob
    Commented May 13, 2014 at 17:08
0
\$\begingroup\$

As far as I know, the C# classes inside Unity won't be directly accessible from outside (i.e. in your Activity). You can use the function UnitySendMessage, but it is a relatively simple function with some strict limitations on it (such as you can only send 1 parameter, and it will be a string). If you need to do something more complex, then your method of creating a localsocket connection is certainly not a bad one.

The other possibility is to send a message to the Unity app that data is available, and then have the C# code query into the Android code to get the values. For example, if you receive an in-app purchase, you can have the Android Activity signal to the C# app that there a new in-app purchase event has occurred, and then the C# code can query into the Android activity to get the results of the purchase. Generally speaking, this is how I've set up my code.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .