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 tredpro · Dec 18, 2013 at 11:57 PM · wwwreloadrefresh

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!

Comment
Add comment · Show 4
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 Julien-Lynge · Dec 19, 2013 at 12:09 AM 0
Share

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?

avatar image tredpro · Dec 19, 2013 at 12:12 AM 0
Share

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

avatar image Julien-Lynge · Dec 19, 2013 at 12:15 AM 0
Share

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?

avatar image tredpro · Dec 19, 2013 at 12:16 AM 0
Share

the call is only made once.

2 Replies

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

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();
         
     }
 }
Comment
Add comment · Show 7 · 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
avatar image tredpro · Dec 19, 2013 at 12:39 AM 0
Share

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?

avatar image robertbu · Dec 19, 2013 at 12:45 AM 0
Share

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.

avatar image tredpro · Dec 19, 2013 at 12:53 AM 0
Share

What about making a pull down to refresh?

avatar image robertbu · Dec 19, 2013 at 12:58 AM 0
Share

Link to pop up code:

http://wiki.unity3d.com/index.php?title=PopupList

avatar image tredpro · Dec 19, 2013 at 01:00 AM 0
Share

no i mean like on the twitter app. if you press on the news feed and pull it down, it refreshes the news feed

Show more comments
avatar image
0

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
   }
 }
Comment
Add comment · Show 4 · 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
avatar image tredpro · Dec 19, 2013 at 12:27 AM 0
Share

thanks for the help but that didn't work.

avatar image tredpro · Dec 19, 2013 at 12:28 AM 0
Share

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
         }
     }
avatar image Julien-Lynge · Dec 19, 2013 at 12:31 AM 0
Share

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

avatar image tredpro · Dec 19, 2013 at 12:34 AM 0
Share

well it loads the file. Just doesn't update. I don't understand the 2nd part

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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