- Home /
Thread in Jar verify it in Unity
I am writing my first plugin in Unity. As part of the plugin(i.e in Android studio) i am supposed to connect to database using a Async thread class. Exported it as Jar and imported in Unity . back in unity i need to hold my function calls until connected with Database. So how can i perform that.
Code in Android Studio
public class AndroidToDatabase extends AsyncTask{ private String URLresponse; @Override protected String doInBackground(String... uri) { return URLResponse }
I tried using a check with URLresponse at Unity Side
Unity Code :
IEnumerator Initilize() { yield return StartCoroutine(getURLResponse()); }
IEnumerator getURLResponse() { pluginClass.Call ("Init");
//Android Studio Function Which executes my Async thread i.e AndroidToDatabase class
string response= pluginClass.Call[string]("getURLResponse");
// Returns the URL response from Android Studio
}
always returns Null. please help me get through this.
Use the Java Plugin Helpers.
The method for accessing a Java Plugin are pretty much the same despite the case usage, it seems.
http://answers.unity3d.com/questions/1184080/unable-to-retrieve-return-values-from-android-libr.html
yeah. I get the causal calls. But how can i sync the thread from JAR to my UNITY or hold my unity thread until function in the JAR finishes executing (i.e connecting to database) which uses async thread.
ok let me reframe this. Consider i have a function in JAR that access an API and Download/Fetch data from it. And i need that data in Unity3d. So if it make a function call from unity3d like
AndroidJAVAClass APIClass.........................
APIClass.call(APIFunction).........
String Data =APIClass.call(getData);
And data is null since....while the line 3 is executed line 2 is still downloading content And is in JAR. So how to solve this.
Sorry for my Bad English
yeah. I get the causal calls. But how can i sync the thread from JAR to my UNITY or hold my unity thread until function in the JAR finishes executing (i.e connecting to database) which uses async thread.
Answer by meat5000 · May 18, 2016 at 09:38 AM
Ok I see, I think I misunderstood your question.
Its coming back null because its executing too early, you are telling me.
You dont want to halt your Unity thread. That would be terrible.
I believe the answer you are looking for is in this post
http://stackoverflow.com/questions/14827532/waiting-till-the-async-task-finish-its-work
which is simply to track the progress of your ASyncTask and flag in your plugin when that is complete. Only then perform the .Call.
https://developer.android.com/reference/android/os/AsyncTask.html
There is lots of talk of the UI Thread so you may require the means of communicating with it. You may require the current Context
https://developer.android.com/training/multiple-threads/communicate-ui.html
I've only had time for basic research, so please let me know if you already know all this.
Yes, i'm already knew and using onPostExecute method which returns a set of values to a class. But my question is this.
Assume in my Jar - i have a Function1 which starts the Asynctask........ - another Function2 which returns the data retrived from Async task.
i cant call in Unity like this
Plugin.call(Function1());
Plugin.callFunction2();
Coz my Function1 is still downloading but the compiler in Unity has already jumped to Function2() and returns null....
Now , what should be my call structure in unity side to JAR.......?
Its the last bit Im having trouble with here.
Unity has already jumped to Function2()
Can you explain? Show more code perhaps? From what you write I'm thinking "cant you just wrap it in an if statement" but I have a feeling that its a different issue.
Function2 shouldnt be invoked until Unity has confirmed with android that the task has completed. Your Unity to Android chat should run from the Android $$anonymous$$ain thread and so work async from the UI Thread anyway. You can let your plugin do the work to find out when its done, storing that result in a bool which you can read from Unity. When true, call Function2.
Am I missing something here?
Hi ... pasted code . In unity.png image the two ways i to implement. So please have a look and guide me through my mistakes
Your answer
Follow this Question
Related Questions
Jar to Unity project converter 0 Answers
Create jar file from Unity 0 Answers
Help "Too many threads" 0 Answers
Accessing static nested class of AndroidJavaClass 1 Answer
Launch an activity of Android Studio from Unity App 0 Answers