Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DaveA · Feb 13, 2011 at 01:13 AM · coroutinewwwbestpractices

Coroutine needed here?

I'm a little confused. I have a routine that loads an object from a file (using WWW and yield), and after it's loaded, I want to modify the game object that it loads into.

What's the best way to do this?

What I have now looks something like this:

void MakeObjectFromFile (url) { GameObject newObj = GetObjectViaWWW (url); // returns a gameobject with the thing in it newObject.GetComponent(Collider).isTrigger = true; }

GameObject GetObjectViaWWW (url) { make a game object, various setup StartCoroutine(WaitForResponse(www)); }

public IEnumerator WaitForResponse(WWW www) { yield return www; // check for errors if (www.error == null) etc. }

If I understand correctly, GetObjectViaWWW will return immediately, so GetComonent(Collider) will probably fail because the coroutine is still just getting started.

I don't really want to do the post-processing in the WaitForResponse routine. Mainly, I haven't found a coding pattern I like for that, if one exists.

So is there one? Is there a preferred clean way of 'waiting for the operation to complete' when it's called in a subroutine like this? Should I set a semaphore and check it in an Update function or something?

It feels like I want to use a callback function. Which is sorta what a coroutine is, right? Can I pass a function into this somehow, essentially a callback from a callback?

Comment
Add comment · Show 1
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 Peter G · Feb 13, 2011 at 01:55 AM 0
Share

You can wait for the completion of a coroutine use "yield return StartCoroutine();" But then you would need to change how you assign a reference to newObj, so it probably won't work as you need it to.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mike 3 · Feb 13, 2011 at 11:47 AM

You can do something like this:

void MakeObjectFromFile (url) { //lambda version to do it inline GetObjectViaWWW (url, go=>go.GetComponent<Collider>.isTrigger = true); //or with a callback function GetObjectViaWWW (url, ManipulateGameObject); }

//callback function void ManipulateGameObject(GameObject go) { go.GetComponent<Collider>.isTrigger = true; }

//wrapper for the coroutine, stops you needing to add on StartCoroutine void GetObjectViaWWW(string url, Action<GameObject> callback) { StartCoroutine(GetObjectViaWWWCoroutine(url, callback)); }

//Action<T> is a delegate which takes one parameter, void return type IEnumerator GetObjectViaWWWCoroutine (string url, Action<GameObject> callback) { //make a game object, various setup yield return StartCoroutine(WaitForResponse(www)); //do stuff with gameobject callback(createdGameobject); }

public IEnumerator WaitForResponse(WWW www) { yield return www; // check for errors if (www.error == null) etc. }

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Nested Coroutines Waiting for Each Other 1 Answer

How to save to a database on application close 2 Answers

Ios problem with www class 2 Answers

Loading files via WWW class and Unity hangs for a few seconds if I use coroutines. If I don't, it works perfectly. What is going on? 1 Answer

How to yiled a try/catch block? 2 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