- Home /
I need WWW to reload information
I call a file that works as a news feed from the web using WWW and load it into a GUILayout.Label but I need this file to reload/refresh. I would like for it to preferably be a pull down function like twitter but if that isn't possible, any reload/refresh feature would be great!
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class news : MonoBehaviour {
public string myNews;
public string url = "https://feed.txt";
IEnumerator Start() {
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0,Screen.height-Screen.height/4*3,Screen.width,Screen.height/3*2));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width-10), GUILayout.Height(Screen.height/3*2));
GUILayout.Label(myNews);
GUILayout.EndScrollView();
GUILayout.EndArea();
}
}
thanks for all future help!
I'm not sure I understand - do you mean that when you call WWW it's pulling a cached version of the file? Or do you mean that after you download the file, your local copy should refresh at some regular interval?
the text file is loaded into the WWW and it is then loaded into the Label.
I'm actually not 100% sure if its cached or downloaded "pretty sure cached" but the only way right now to reload the file (like if i posted an update) would be to restart the app
So what you're saying is that if you make a WWW call to download the file, then make another WWW call later, it doesn't download the updated version of the file?
Answer by robertbu · Dec 19, 2013 at 12:32 AM
You need to make your text download into a separate coroutine form Start() and then call it as you will:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode()]
public class News : MonoBehaviour {
public string url = "https://feed.txt";;
private string myNews;
private Vector2 scrollPosition = new Vector2(0,100);
void Start() {
StartCoroutine(LoadText());
}
IEnumerator LoadText() {
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
}
void OnGUI()
{
if (GUI.Button(new Rect(0,0,100,50), "Reload")) {
StartCoroutine(LoadText());
}
GUILayout.BeginArea(new Rect(0,Screen.height-Screen.height/4*3,Screen.width,Screen.height/3*2));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width-10), GUILayout.Height(Screen.height/3*2));
GUILayout.Label(myNews);
GUILayout.EndScrollView();
GUILayout.EndArea();
}
}
ok that works. now I have 2 questions
1st is why did you use private?
2nd does it have to be pressed by a button or can it be made automatic?
You can make it reload without a button just like you did:
using UnityEngine;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode()]
public class News : $$anonymous$$onoBehaviour {
public string url = "https://feed.txt";
private string myNews;
private Vector2 scrollPosition = new Vector2(0,100);
void Start() {
StartCoroutine(LoadText());
}
IEnumerator LoadText() {
while (true) {
myNews = "";
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
yield return new WaitForSeconds(5f);
}
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0,Screen.height-Screen.height/4*3,Screen.width,Screen.height/3*2));
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width-10), GUILayout.Height(Screen.height/3*2));
GUILayout.Label(myNews);
GUILayout.EndScrollView();
GUILayout.EndArea();
}
}
As for 'private', it did not appear that 'myNews' was something you would be initializing in the inspector. I may be wrong. I try to be careful about what I make public.
Link to pop up code:
no i mean like on the twitter app. if you press on the news feed and pull it down, it refreshes the news feed
Answer by Julien-Lynge · Dec 19, 2013 at 12:21 AM
Judging by the comment thread above, the solution is to make another WWW call whenever you want an updated version of the file. Once you download the file, that's it - it's not linked to the site you downloaded it from in any way, and it won't be updated if the site changes.
You could do something like a coroutine loop to download a new file every X seconds and overwrite the existing file. E.g.:
IEnumerator updateFile()
{
while(true)
{
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
yield return new WaitForSeconds(30f); //download every 30 seconds
}
}
i added it like
IEnumerator Start() {
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
}
IEnumerator updateFile()
{
while(true)
{
WWW guiwww = new WWW(url);
yield return guiwww;
myNews = guiwww.text;
yield return new WaitForSeconds(1f); //download every 30 seconds
}
}
There are a couple issues there:
First of all, I don't think you can turn Start() into an IEnumerator in C# - I think that's JS only.
Second, for an IEnumerator to work, you have to tell Unity when to start running it. So you have to have something like StartCoroutine: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html
well it loads the file. Just doesn't update. I don't understand the 2nd part
Your answer
![](https://koobas.hobune.stream/wayback/20220613124443im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
opening a webpage? 1 Answer
How can I have a while(www is loading) loop? 1 Answer
Retrieving data from a WWW php call? 1 Answer
WWW.Post works fine in Editor, but not in WebPlayer Build 0 Answers