Log in or register to post comments

Accessing Unity variable from Android Code

June 11, 2013 - 12:25am #1

Hi,

I have extended unity android activity and added custom views in eclipse by following the below link tutorial:

https://developer.vuforia.com/resources/dev-guide/extending-unity-android-activity-and-adding-custom-views-eclipse

Now if I want to access any variable created in c# script (for example a variable inside Eventhandler.cs) from my android activity, how can I do that. Can someone please help me with this?

 

Thank You,

Sushil

Accessing Unity variable from Android Code

June 12, 2013 - 1:53am #8

Cool ! Thanks.

Accessing Unity variable from Android Code

June 12, 2013 - 1:31am #7

Hi AlessandroB,

Directly accessing c# variables from java is not possible. There can be other way to achieve it. Android java webview can access javascripts and javascripts can access C#. So, this way a logic can be written to access C# variables from Android java app code.

In my case, I am accessing my written functions on both sides and inside the function I do my work.

 

From Java code to access C# function, I am using:

QCARUnityPlayer.UnitySendMessage("ImageTargetWood", "ReceiveJavaMessage", "Sushil");

where 1st paramener is the name of the component I want to access, 2nd parameter is name of function I have written in C# script and third parameter is just a message and can be any string.

On C# side, the way to write the function is:

public void ReceiveJavaMessage(string myMessage) {
if(myMessage.Equals("Sushil")) {
/*do something*/
}
}
 
--------------------------------------------------------------------------------------------------------
From C# side, to access a function on java side I write:
 
AndroidJavaClass jc = new AndroidJavaClass("com.example.qcarjava.MainActivity");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("printMessageInJava");
 
On Java side, the function is written in following manner:
 
public  void printMessageInJava () {
 
MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
        Toast myToast = Toast.makeText(MainActivity.this, "Function call from Unity", Toast.LENGTH_LONG);
        myToast.show();
        }
 
      });
 
     }
 
UI thread cannot be accessed directly, so in code running the Toast on UI thread. 

Also this needs to be declared in activity:

public static MainActivity currentActivity;

and in onCreate() {

currentActivity = this;

}

 

This way in both directions, I am able to write and access methods and do my stuff. Hope it helps someone :)

 

Thank You,

Sushil

Accessing Unity variable from Android Code

June 12, 2013 - 1:15am #6

Nice !

so, were you able to access Unity C# variables from the Java code (in Eclipse) in the end ?

or did you find another solution ? would be useful to share it, if you can, as others might be interested too ;-)

 

Accessing Unity variable from Android Code

June 12, 2013 - 12:36am #5

Hi AlessandroB,

Finally I am able to address my requirement by calling functions both ways ie java to c# and vice versa :) . Thank you very much for your help

Accessing Unity variable from Android Code

June 11, 2013 - 6:22pm #4

Hi AlessandroB

Thank you for your reply and I think you are correct that c# variables cannot be accessed from java code. I went through the link you mentioned and even that looks useful.

 

My specific requirement is that I want to make some buttons in Android Ui and on click of those buttons want to show some models or do other task. So, in any case I have to pass event from Android to c# as buttons exist in android Ui and showing model logic is in C# . Is is possible to send events from java code to c# when I press a particular button to achieve this kind of scenario. My Ui contains few other things, so I want to dram UI from android only.

 

Thank You,

Sushil

Accessing Unity variable from Android Code

June 11, 2013 - 6:20pm #3

..

Accessing Unity variable from Android Code

June 11, 2013 - 3:37am #2

Hi, this might be difficult to do, as the C# script variables are not exposed in the Java layer (afaik);

to be sure, you may want to consult the Unity developer guide in the Unity website.

Otherwise, what I would recommend is to follow an alternate approach, where you make your Java (Android) code as a Plugin for Unity, and import this into you Unity project (and then you can work from Unity and you can also call Java methods from a C# script);

we also have a simple tutorial about how to build and use Android plugins that work with a Vuforia-based Unity project:

https://developer.vuforia.com/resources/dev-guide/unity-android-plugins

 

Log in or register to post comments