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. Dismiss Notice

Access json file in WebGL build

Discussion in 'WebGL' started by Opticallo, Apr 3, 2016.

  1. Opticallo

    Opticallo

    Joined:
    Jul 29, 2015
    Posts:
    1
    Hello, I am trying whole day to access json file from StreamingAssets folder. Easy way to do this on PC platforms is:

    string filePath = Path.Combine(Application.streamingAssetsPath, "highScoreData.json");
    jsonString = File.ReadAllText(filePath);

    This unfortunately does not work when running on web browser. I tryed using this code:

    if (filePath.Contains("://"))
    {
    WWW www = new WWW(filePath);
    yield return www;
    jsonString = www.text;
    }

    but this does not work... Can you please show me how to write and read to json file that is placed somewhere inside project?
     
    NathTechDev and yodda like this.
  2. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
    So now WWW is UnityWebRequest
    And this is what i am looking for in this clog desperately.
     
    NathTechDev and nx-sm like this.
  3. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
  4. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
    so basically i did this and put google url into it it's from the unityWebRequest Manual.
    with firefox it didn't work but with chrome just few lines of HTML (jaggy)
    inside unity editor it reads perfectly
    i will add the code also...any ideas why it is so laggy? anyone....

    using UnityEngine;
    using UnityEngine.Networking;
    using System.Collections;
    using UnityEngine.UI;
    using System.Threading;
    // UnityWebRequest.Get example

    // Access a website and use UnityWebRequest.Get to download a page.
    // Also try to download a non-existing page. Display the error.

    public class Example : MonoBehaviour
    {
    GameObject @object;
    public Text textObj;
    public InputField inputField;
    public Button button;
    public string address = string.Empty;
    private bool isOkToStart = false;

    //i use start as coroutine
    IEnumerator Start()
    {

    if (isOkToStart)
    {
    yield return new WaitForSeconds(5f);
    StartCoroutine(GetRequest(address));
    }
    }
    ////when clicked the test button will take the adress into inputfield
    public void GetUrlFromInputString(string uri)
    {
    inputField = inputField.GetComponent<InputField>();
    address = inputField.text;
    isOkToStart = true;
    }
    public void InsertAddress()
    {

    address = inputField.text;


    if (address==string.Empty)
    {

    return;
    }
    else
    {
    address = inputField.text;

    isOkToStart = true;
    //Debug.Log("GetRequest("+address+") called ", gameObject);
    StartCoroutine(GetRequest(address));
    //Debug.Log("GetRequest(" + address + ") called ", gameObject);
    }

    }

    IEnumerator GetRequest(string uri)
    {
    using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    {
    // Request and wait for the desired page.
    yield return webRequest.SendWebRequest();

    string[] pages = uri.Split('/');
    int page = pages.Length - 1;

    if (webRequest.isNetworkError)
    {
    //Debug.Log(pages[page] + ": Error: " + webRequest.error);
    textObj.text = pages[page] + ": Error: " + webRequest.error;
    }
    else
    {
    //Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
    textObj.text = pages[page] + ":\nReceived: " + webRequest.downloadHandler.text;
    }
    }
    }
    }
     

    Attached Files:

    Last edited: Oct 4, 2020
  5. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
    btw need to run it in a from the web not from local directory (it is WebGl) so put it inside a localhost and test.
     
  6. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
    So if You Read Your Files With this Little App Then You Can Access Your Online Data File (like Json and so).
    i did it just to see if the webGl Unity Can Read From Urls When Its Outside of The Editor Of Unity.
     
  7. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22
    Also you Can See The Use Of Input Field Reader Example. not pretty but working.
     
  8. yodda

    yodda

    Joined:
    Nov 3, 2016
    Posts:
    22