Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Avisingh · May 18, 2016 at 07:29 AM · unity 5asyncthreadsandroidjavaobjectjar

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · May 18, 2016 at 07:50 AM 0
Share

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

avatar image Avisingh meat5000 ♦ · May 18, 2016 at 08:01 AM 0
Share

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.

avatar image Avisingh meat5000 ♦ · May 18, 2016 at 08:13 AM 0
Share

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

  1. AndroidJAVAClass APIClass.........................

  2. APIClass.call(APIFunction).........

  3. 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

avatar image Avisingh meat5000 ♦ · May 18, 2016 at 02:11 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · May 18, 2016 at 09:40 AM 0
Share

I've only had time for basic research, so please let me know if you already know all this.

avatar image Avisingh · May 18, 2016 at 10:19 AM 0
Share

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.......?

avatar image meat5000 ♦ Avisingh · May 18, 2016 at 10:23 AM 0
Share

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?

avatar image Avisingh · May 18, 2016 at 10:57 AM 0
Share

Hi ... pasted code . In unity.png image the two ways i to implement. So please have a look and guide me through my mistakes

androidjar.png (18.8 kB)
unity.png (27.5 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges