Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to read text files when packaged as webGL

Discussion in 'Web' started by tianlongw208, Aug 24, 2023.

  1. tianlongw208

    tianlongw208

    Joined:
    Aug 24, 2023
    Posts:
    5
    I have a text file that I want to put in the folder of the program, which is the folder where index.html is located. I want my text to read the content of this text file, (because the text content will change at any time, so I did not put it in the Unity package folder, such as Application.streamingAssetsPath) How should I do it?

    I use the IIS that comes with Windows, and set Localhost:90 as the packaging path:


    UnityWebRequest www;
    www = UnityWebRequest. Get("http://localhost:90/Script.txt" );
    www.SendWebRequest();
    string fileContent = www.downloadHandler.text;


    But it doesn't seem to work.

    Better give me an example, thanks!



    My version is: 2018.3.4f
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    843
    did you check if it errored? are you sure its listening on port 90? dfoes that url work normally through a browser? define "not work"
     
  3. tianlongw208

    tianlongw208

    Joined:
    Aug 24, 2023
    Posts:
    5
    Yes, the game can be played normally by entering Localhost:90 in the browser, but there is no text to get the txt content. It is still the default state. There is no code other than the above.
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    843
    that kinda wasnt what i asked, does http://localhost:90/Script.txt work? if theres no other code thats not normally how you would use that sort of code... normally you use coroutines, and wait for the webrequest to complete, check if it errored or not....
     
  5. tianlongw208

    tianlongw208

    Joined:
    Aug 24, 2023
    Posts:
    5
    At the same time, I can open the text in the webpage by typing Localhost:90/Script.txt in the browser.
     
  6. tianlongw208

    tianlongw208

    Joined:
    Aug 24, 2023
    Posts:
    5
    Actually, I'm a novice, the above code I ask GPT-3.5 to get the result. I just want to ask how to achieve this function.
     
  7. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    843
    this is why the nice people at unity gave us manuals, often including examples of how and why chat gpt is a pile of donkey :p because, while I am capable of coding, i thought id try it, i asked it a real simple thing, and it produced code that didnt even compile, i told it so, it said my bad, produced more uncompileable code.. ive tried a few times, and so far never got some code that actually compiled, and did what i asked for..

    go look up the manual as it gives an example on how to check for errors and everything
     
  8. tianlongw208

    tianlongw208

    Joined:
    Aug 24, 2023
    Posts:
    5
    IEnumerator LoadText()
    {
    UnityWebRequest request = UnityWebRequest.Get("http://localhost:90/Build/Script.txt");
    yield return request.SendWebRequest();

    if (request.result == UnityWebRequest.Result.Success)
    {
    string text = request.downloadHandler.text;
    // Your code
    }
    } This is another solution given by GPT.
     
  9. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,308
    You know that if it were going to work that this is only ever going to work on your system, right?

    Also, if you develop for WebGL you should REALLY upgrade to a newer Unity version. There have been tremendous improvements for WebGL builds since Unity 2018.