(I hard coded the file paths when Application.dataPath wasn’t working.)
the LoadTextData looks like this:
public string LoadTextData(string url)
{
www = new WWW(url);
while (!www.isDone)
{
Debug.Log("Loading...");
}
return www.text;
}
This all works just fine in the editor, but when I run the webplayer (both from my computer, dropbox and from the webserver) it freezes and then crashes the webplayer.
NOTE: I have also tried having “http://” in front of the filepath, but got the same result.
Another approach could be to use Resources. Load(textfile). Then you get the file as a TextAsset. Then you can set tw.dataFile = TextAsset.text;
Not sure if this will work, but guess it should.
It may be because of the while loop. If it takes too long to load it can crash your player. I think that is why BFGames suggested a Cououtine. Its essentially a Thread.
I think I had originally done that, but that was while using streamreader. Had to do away with that when I realized streamreader couldn’t be used on the web. Not looking forward to rewriting all of that, only to discover it doesn’t fix it…
I’ll keep tinkering. Anyone else with some thoughts on why I would be hitting this brick wall?
Ah, I see. It shouldn’t take long though. In the editor, it takes 1 frame. They’re small text files, which should load almost instantly (even if you were on a dial-up connection).
EDIT: Of course, the moment I say this, I start getting the error “Could not connect to host” when running in the editor…
Maybe its my crappy ISP? Someone do me a favor, and check out this link. Tell me what you see.
This really should be put in a coroutine. I assume you’re calling this from within a Start or Awake method somewhere. The editor is a special environment and should not be treated as “if it works here, it should work there”, especially when dealing with a different platform such as a web player.
What happens is if it is in a Start or Awake method, the whole start up is blocked by your while loop. And if there is an error (such as missing the protocol portion of the URL, or some other error) your while never exits.
Put it in a coroutine, yield the www and then check if there is an error or not. Almost exactly as it is in the script reference.
Haha. Isn’t that just the epitome of being a programmer though? I would definitely put it in a coroutine, then see it that works. Which it should
Edit:
Btw, when I try and play the player it stops responding and a popup asks if I want to end the process. So that means it is getting stuck in a loop I presume.
Ok, I’ll put it all into a coroutine. My only concern is that a large portion of the program depends on that content being there. If I allow the rest of the program to execute before the content is loaded, I’m afraid it will cause all kinds of problems.
Of course. The coroutine “couldn’t be started”. FML
What I would do is use a public boolean on the script. Make the rest of your program wait for the boolean to be true or maybe execute all of them after it loads? I personally would use the boolean method.
This is really confusing because I used this code:
// Use this for initialization
void Start () {
float t_n = Time.time;
string txt = LoadTextData("www.pillar4games.com/MK_Data/Data/SpeciesData.txt");
Debug.Log("Took about : " + (Time.time - t_n) + "ms to get text");
Debug.Log(txt);
}
// Update is called once per frame
void Update () {
}
public string LoadTextData(string url)
{
WWW www = new WWW(url);
while (!www.isDone)
{
Debug.Log("Loading...");
}
return www.text;
}
And it worked perfectly fine in my scene.
The only other thing I can think of is the fact that you are trying to put it into a TextAsset? Cause it works fine with a string.
Did you run it in a webplayer though? It all works perfectly fine for me from the editor and from an exe, just not the webplayer. Also, I’m not using TextAsset, its all just string variables.
using UnityEngine;
using System.Collections;
public class textget : MonoBehaviour {
string txt = "getting text...";
// Use this for initialization
void Start () {
float t_n = Time.time;
txt = LoadTextData("www.pillar4games.com/MK_Data/Data/SpeciesData.txt");
Debug.Log("Took about : " + (Time.time - t_n) + "ms to get text");
Debug.Log(txt);
}
// Update is called once per frame
void Update () {
}
public string LoadTextData(string url)
{
WWW www = new WWW(url);
while (!www.isDone)
{
Debug.Log("Loading...");
}
return www.text;
}
public void OnGUI()
{
GUI.Label(new Rect(0,0,500,500),txt);
}
}
and it did the same thing when I built it in a webplayer. Is there like some weird way to do WWW on a webplayer or something?
Also tried to build with the code in a webplayer and crashed… Something in the policy not allowing a used port or something? Im out of ideas now. Try the TextAsset though