WWW text feed?

Uhm… How can i show a WWW .txt file as a String? like a news feed or a wellcome window.

did you ever get an answer to this one?

using UnityEngine;
using System.Collections;

void Start()
{
    StartCoroutine(getFeed("http://your_url/textfile.txt"));
}
public IEnumerator getFeed(string URL)
{
    WWW stringWWW = new WWW(URL);
        
    yield return stringWWW;
    if(stringWWW.error == null)
    {
        returnData = stringWWW.data;
        Debug.Log(stringWWW.data);
    }
    else
    {
        returnData = stringWWW.error;
    }
    stringWWW.Dispose();
    return returnData;
}

I think that should work.

!! Thank you CorruptedHeart :slight_smile:

I have actually just noticed a few bugs in my code, they simple bugs but bugs none the less.
This code will work and show the results in the console for now, save it as Feed.cs

using UnityEngine;
using System.Collections;

public class Feed : MonoBehaviour
{
    private string feedText = "";
    void Start()
    {
        StartCoroutine(getFeed("http://your_url/textfile.txt"));
    }
    
    public IEnumerator getFeed(string URL)
    {
        WWW stringWWW = new WWW(URL);
        
        yield return stringWWW;
        if(stringWWW.error == null)
        {
            feedText = stringWWW.data;
        }
        else
        {
            feedText = stringWWW.error;
        }
        stringWWW.Dispose();
        Debug.Log(feedText);
    }
}
1 Like

nice, thank you very much for the fixes. The Unity Community rocks!! :slight_smile:

We’re here to help. :slight_smile:

Hi That is a fantastic little script! If I wanted that text to appear on say a GUI (If possible) or on a basic object where would I start? Couldn’t find the info as I’m not sure what the question is I am exactly asking!

You would do exactly the same thing and use OnGUI() for displaying the text as a label.

how to use the above script ?? shall i drag it on a plane ??? please guide

A rendered mesh isn’t necessary. Dragging it onto an empty game object would suffice.

If you are having difficulty understanding Unity’s component based architecture, then you should try working you way through the Lerpz tutorial in the resources section of the Unity website.

Hello,
I’ve tried your code but I have a problem. It only reads the first character of my .txt.
Somebody knows what can be happenning?
Thanks!

Hey guys i am a couple years late :stuck_out_tongue: , i’ve used your script , and i get the content of the string in the Debug.log but i’m having difficulties importing the OnGUI() function in your script in order to display the contents of the string on a UI (Button etc). Any thoughts about that?

Ok i figured it out guys , to anyone who wants to apply CorruptedHeart’s code to buttons , all thanks goes to CorruptedHeart of course for providing us his awesome code!!! :smile:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Feed : MonoBehaviour
{
private string feedText = “”;
private string feedText2 =“”;
private string feedText3 =“”;
public Button btn,btn2,btn3; //The Buttons we want to create

void Start()
{
StartCoroutine(getFeed(“http:/mylink”,btn)); // your link
StartCoroutine(getFeed(“http:/mylink”,btn2));
StartCoroutine(getFeed(“http:mylink”,btn3));
}

public IEnumerator getFeed(string URL, Button button)
{
WWW stringWWW = new WWW (URL);

yield return stringWWW;
if (stringWWW.error == null) {
feedText = stringWWW.data;
} else {
feedText = stringWWW.error;
}
stringWWW.Dispose ();
Debug.Log (feedText);
button.GetComponentInChildren ().text = feedText; // Change the component of the text to get the string you have on the link

}
}