Search Unity

Unity3D and C# - Having trouble getting endless data from the cloud

Discussion in 'Scripting' started by EdgarasArt, Apr 22, 2016.

  1. EdgarasArt

    EdgarasArt

    Joined:
    May 21, 2015
    Posts:
    11
    Hello,

    I'm having some issues getting data from particle cloud.To read the data from particle cloud I use WWW class. But there is one problem - the stream of data from particle cloud is infinite, you can see it always updates when the link is opened in the browser and the WWW class returns the data only when it is downloaded. As the stream is endless this data acquisition can't be done. What could be the solution for this?

    For instance, I do not have problems gathering data in this form:
    www.ourtechart.com//wp-content/uploads/2016/04/jsonAllData.txt2

    In this case working code right now looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class particle : MonoBehaviour {
    4.  
    5.     string Url = "www.ourtechart.com//wp-content/uploads/2016/04/jsonAllData.txt";
    6.  
    7. IEnumerator Start()
    8. {
    9.     WWW www = new WWW(Url); // Start a download of the given URL (string defined elsewhere)
    10.     yield return www; // Wait for download to complete
    11.  
    12.     // http://docs.unity3d.com/ScriptReference/WWW-text.html
    13.     if (www.error == null)  {
    14.         Debug.Log("Service data www: " + www);
    15.         Debug.Log("Service data www.isDone: " + www.isDone);
    16.         Debug.Log("Service data www.progress: " + www.progress);
    17.         Debug.Log("Service data www.text: " + www.text);
    18.         Debug.Log("Service data www.bytesDownloaded: " + www.bytesDownloaded );
    19.         Debug.Log("Service data www.size: " + www.size );
    20.  
    21.         if (www.isDone && www.error == null) {
    22.             Debug.Log("WE'VE GOT DATA!");
    23.         }
    24.     }
    25.     else {
    26.         Debug.Log("Error: " + www.error);
    27.     }
    28. }
    29. }

    As you can see with this example I can acquire data, what's left - parse the data, which is not the issue.
    The script should have the name "particle.cs" and attached to an empty gameObject in Unity3D.

    If the String Url will be changed to the particle cloud link with access token we would not get any data as download never ends because of this: "yield return www; // Wait for download to complete"

    Anyone has some ideas how to solve this?
     
  2. Death111

    Death111

    Joined:
    Apr 17, 2016
    Posts:
    38
    Maybe you can use the UnityWebRequest class. There you can specify an own DownloadHandler (where you could handle the download yourself). But the class is experimental in Unity 5.3. In 5.4 no more.
    I did get a unity crash using it in 5.3. But only in the scenario I posted in the thead.

    Maybe this helps.
     
    EdgarasArt likes this.
  3. EdgarasArt

    EdgarasArt

    Joined:
    May 21, 2015
    Posts:
    11
    Thanks! I'll take a look.
     
  4. EdgarasArt

    EdgarasArt

    Joined:
    May 21, 2015
    Posts:
    11

    By researching UnityWebRequest class I founded what I needed here. Thanks man.
     
  5. Death111

    Death111

    Joined:
    Apr 17, 2016
    Posts:
    38
    Nice ;)
    I do just wonder if it's a good idea to use System.Net.HttpWebRequest class. I used the UnityEngine.Networking.(Experimental).UnityWebRequest since I do want it to be supported on all platforms - I wonder if this is the case with the .net class direct also.
     
  6. EdgarasArt

    EdgarasArt

    Joined:
    May 21, 2015
    Posts:
    11
    I have just tested out on Android smartphone - worked great! I Hope iOS devices would handle this without a problem too.
     
  7. Death111

    Death111

    Joined:
    Apr 17, 2016
    Posts:
    38
    If that's also working, I wonder why Unity makes it's own class...