Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to read a json file with Unity Tiny

Discussion in 'Project Tiny' started by LauraLaureus, Dec 12, 2018.

  1. LauraLaureus

    LauraLaureus

    Joined:
    Oct 5, 2018
    Posts:
    9
    Hi everyone!

    I'm playing around with Unity Tiny. I don't have experience with typescript and I need to read parameters from a JSON file. How should I proceed?

    Thank you in advantage
     
  2. TheRoccoB

    TheRoccoB

    Joined:
    Jun 29, 2017
    Posts:
    54
    Have you tried a simple JSON.parse(your json) call?

    It looked like standard browser commands are supported in typescript
     
  3. LauraLaureus

    LauraLaureus

    Joined:
    Oct 5, 2018
    Posts:
    9
    After your recommendation I tried but there wasn't any luck.
    I checked the "sources" tab in the "Developers tools" of my browser and it seems that the json file isn't served.
    I hope any admin or dev can help me with this doubt.
     
  4. etienne_unity

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    102
    Hi @LauraLaureus

    You can reference any type of asset in your Tiny project. To do so, drag the asset from the Unity Project window into the Tiny > Assets panel of the Project Settings window, like so:

    upload_2018-12-13_8-49-27.png

    In this case, I dragged a json file named hello, which contains this:
    Code (JavaScript):
    1. {
    2.     "hello": "world"
    3. }
    One way to use this asset in your game would be to request it like any other external resource. This snippet would work for Json assets:
    Code (JavaScript):
    1. namespace ut {
    2.  
    3.   export class JsonUtility {
    4.  
    5.     static loadAssetAsync(assetName: string, callback: (error?: any, jsonResponse?: any) => void): void {
    6.       this.loadAsync(UT_ASSETS[assetName], callback);
    7.     }
    8.  
    9.     static loadAsync(url: string, callback: (error?: any, jsonResponse?: any) => void): void {
    10.       var xhr = new XMLHttpRequest();
    11.       xhr.open('GET', url, true);
    12.       xhr.responseType = 'json';
    13.  
    14.       xhr.onload = function () {
    15.         var status = xhr.status;
    16.         if (status == 200) {
    17.           callback(null, xhr.response);
    18.         } else {
    19.           callback(status);
    20.         }
    21.       };
    22.  
    23.       xhr.send();
    24.     }
    25.   }
    26. }
    Usage:

    Code (JavaScript):
    1. ut.JsonUtility.loadAssetAsync("hello", (error, data) => {
    2.   if (error) {
    3.     console.error(error);
    4.     return;
    5.   }
    6.   console.log("hello: " + data.hello);
    7. });
    Would print
    hello: world
    in the browser console.

    I hope this helps!
     
  5. LauraLaureus

    LauraLaureus

    Joined:
    Oct 5, 2018
    Posts:
    9
    Hi @etienne_unity sorry for the late reply but I don't have time to test you solution until today. It works thanks for your time!
     
  6. Deleted User

    Deleted User

    Guest

    I can't drop my *.json or *.txt files onto Assets in Tiny Preferences window.
    I see prohibition sign.
    what can be a reason?
     
  7. raymondyunity

    raymondyunity

    Unity Technologies

    Joined:
    Apr 30, 2018
    Posts:
    122
    @arekb Drag the file from the project window in Unity, not from your file explorer.
     
  8. Deleted User

    Deleted User

    Guest

    Works! Thanks!
     
  9. badiba

    badiba

    Joined:
    Apr 9, 2018
    Posts:
    9
    I cannot do it too. Even though I drag the file from the project window in Unity as @raymondyunity mentioned above.