Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question WebGL json file read problem

Discussion in 'Web' started by agok21, Mar 29, 2024.

  1. agok21

    agok21

    Joined:
    Nov 16, 2021
    Posts:
    4
    Hi,

    Even if my game works as it should in the Windows build and in the editor, the WebGL build seems to have no access to the data from the json files. I am trying the build on itch.io, and I cannot the see the exact error messages, but it is obvious that the problem is the build's inability to reach the json file. I found a few solutions online, but I couldn't implement them. I would be really happy if you could show me the way.

    Here is the part of the code that I think needs adjustments:

    Code (CSharp):
    1.     public SpellConfigs spellConfigs;
    2.  
    3.     void Awake()
    4.     {
    5.         LoadSpellConfigs();
    6.     }
    7.  
    8.     void LoadSpellConfigs()
    9.     {
    10.         string filePath = "Assets/spells.json";
    11.  
    12.         if (File.Exists(filePath))
    13.         {
    14.             string jsonContent = File.ReadAllText(filePath);
    15.             spellConfigs = JsonUtility.FromJson<SpellConfigs>(jsonContent);
    16.             //AdjustCooldownsForAllSpells(spellConfigs);
    17.             Debug.Log("Spell configs loaded successfully.");
    18.         }
    19.         else
    20.         {
    21.             Debug.LogError("Spells configuration file not found.");
    22.         }
    23.     }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,431
    This path does not exist in builds. It only happens to work in the editor.

    You'd have to have the spells.json in a StreamingAssets folder. And then, to access it on the web, you need to use UnityWebRequest because it cannot be access via file I/O.
     
  3. agok21

    agok21

    Joined:
    Nov 16, 2021
    Posts:
    4
    Thanks! It was actually in the StreamingAssets, but I changed its place while trying things... Can you show me how to use the UWR? I am really having a hard time understanding and writing it for some reason :'
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,431
  5. agok21

    agok21

    Joined:
    Nov 16, 2021
    Posts:
    4
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,431
    Json goes into StreamingAssets and the URI is likely just the path but possibly prefixed like "file://". The coroutine is used to periodically check for completion. If execution continues past the yield, the webRequest.downloadHandler.text contains the json contents.

    Sorry I can't recall more details from memory.
     
  7. agok21

    agok21

    Joined:
    Nov 16, 2021
    Posts:
    4
    Thanks a lot! I managed to get it working, but it's great to get this explanation. Thank you very much.
     
    unityruba likes this.