Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 dr-sonic · Aug 11, 2015 at 12:15 PM · wwwyieldwhile looploadimageintotexture

While loop stops

I use of the shelf code to display camera feed from a server (series of images). But after some time (sometimes few seconds, sometimes few minutes), camera feed freezes, and last two logs are isDone (True) and number i. The rest of the program (kinect tracking and oculus integration) works normally like nothing happened. Feed (server) still works since I can access it in a browser. Any idea why this happens?

 #pragma strict
     // Continuously get the latest webcam shot from the url provided (ffserver on the local network)
     // and DXT compress them at runtime
     var url = "http://192.168.1.50:8090/test.jpg";
     var i : int = 0;
     function Start () {
         Debug.Log(i);
         // Create a texture in DXT1 format
         GetComponent.<Renderer>().material.mainTexture = new Texture2D(4, 4, TextureFormat.RGB24, false);
         while(true) {
             // Start a download of the given URL
             var www = new WWW(url);
 
             // wait until the download is done
             yield www;
             Debug.Log(www.isDone);
             // assign the downloaded image to the main texture of the object
             www.LoadImageIntoTexture(GetComponent.<Renderer>().material.mainTexture);
             Debug.Log(i);
             i++;
         } 
     }    
 
 
Comment
Add comment · Show 13
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 Glurth · Aug 11, 2015 at 01:41 PM 0
Share

Honestly, I'm not quite sure what going on with your code. It LOO$$anonymous$$S like the new WWW(url) command starts the download. If this is the case, I would think you want to call this BEFORE you start looping to wait for the download to finish, rather than inside the loop.

It's also not quite clear to me, when you "break;" or exit the while(true) loop. I'm not even sure why there is a loop at all, nothing except "i" (and your debuglog) seems to change with each iteration. I guess you DO create a new WWW each iteration, but I don't see why.

avatar image dr-sonic · Aug 11, 2015 at 06:28 PM 0
Share

Server in question is ffserver on a machine local network. On it, there is just one file (image test.jpg) which is updated 30 times per second. This code should get the image from server and display it as a texture on a game object, and update it in every iteration of a while loop. So WWW(url) starts downloading, yield waits for download to finish LoadImageIntoTexture displays that image as a texture on an object in game. And it works, I see live camera feed on that object, but for some reason it stops (freezes) after some time.

avatar image Glurth · Aug 11, 2015 at 07:12 PM 0
Share

Ah, I see now. Hmm, is it possible it's never co$$anonymous$$g back from the yield for some reason? One more debug.log between new WWW, and the yield statement, should confirm/eli$$anonymous$$ate that. Currently the Debug.Log(i) is the last item in your log, right?

avatar image Glurth · Aug 13, 2015 at 01:35 PM 1
Share

oh, darn. sorry. Well, if you remove the Thread.Yield line the loop will be infinite, and wont release control back to unity and/or the OS. However, it WILL continue to output debug logs. Perhaps if you just remove the yield line, we will be able to get enough info out of the debug logs to deter$$anonymous$$e the problem.

Possible workaround: not sure if this would work for your situation: What if you put this class inside a $$anonymous$$onobehavior, and only downloaded ONE file each time UPDATE() is called? This way you no longer have any infinite loops you need to worry bout hogging the processor. Checking the time during your loop, and returning when a time limit is reached is also a good idea. $$anonymous$$ight want to take a look at http://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html for that.

The worst thing is that everything worked few days ago and I didn't change anything.

That is the worst! But it's also a clue your code may need to be more robust as far as error handling goes. If something external changes, code that used to work just fine, could be effected.

regarding error handling: I also just noticed this function in WWW (http://docs.unity3d.com/ScriptReference/WWW-responseHeaders.html) Perhaps these response headers will state if the stream is invalid, so you can delay and then try again? Really not sure about that tho. Displaying them all, each download, and seeing if the headers are any different when it fails; this is where I would start.

avatar image dr-sonic · Aug 14, 2015 at 06:59 AM 1
Share

That's exactly what I did at the end. Put the GetUrl function in Update so it's being called every frame again. It works now, doesn't freeze, but in the log I get a lot of "recv failure: connection was reset" error messages, which means that there are some dropped/failed connections to the server. This solution will work for now, and I'll try to figure out that connection along the way. Thanks!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

UnityWebRequest.GetTexture returns incorrect Linear or Gamma or whatever 1 Answer

WebPlayer and WWW, stalls forever on get.... 0 Answers

Updating app cannot load downloaded image 0 Answers

what happens if www cannot connect? 1 Answer

How do you download image to UI.Image? 4 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