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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What type is a JSON file for using LoadAssetAtPath?

Discussion in 'Scripting' started by James-Sullivan, Apr 13, 2018.

  1. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    I'm trying to open a JSON file via a script using AssetDatabase.OpenAsset. But in order to open it, I first have to load it using AssetDatabase.LoadAssetAtPath. If this was a C# script that I was loading, I'd use the type MonoScript, but that isn't working for my JSON file. What type do I need to use?

    Code (CSharp):
    1.  var lScript = (MonoScript)AssetDatabase.LoadAssetAtPath("Assets/StreamingAssets/TextDatabase.json", typeof(MonoScript));
    2.              if (lScript != null)
    3.              {
    4.                  Debug.Log("Found script");
    5.                  AssetDatabase.OpenAsset(lScript, 1);
    6.              }
    Additonaly, I need to search that JSON file for a specific string, and then open it at the line where that string is, so if you know how to do that, I'd greatly appricaite you sharing, thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Json files are basically just text files. So you'll use a TextAsset type.

    Also you're most likely going to want to use JsonUtility or Json.net to deserialize the json string into a class.
     
    James-Sullivan likes this.
  3. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Unless something else is wrong, I don't think TextAsset is it. It's still returning null.

    Code (CSharp):
    1.                 TextAsset lScript = (TextAsset) AssetDatabase.LoadAssetAtPath("Assets/StreamingAssets/TextDatabase.json",
    2.                                                                               typeof(TextAsset));
    3.                 Debug.Log("See Text " + lScript);
    4.  
    5.                 if (lScript != null)
    6.                 {
    7.                     Debug.Log("Found script");
    8.                     AssetDatabase.OpenAsset(lScript, 1);
    9.                 }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Well, considering I use them all the time, I'm pretty certain it is correct. Now, I haven't used the AssetDatabase, but I know streamingAssets is a different folder in how it handles things. It tends to not really know what is in it. If you stick a png file in there, you'll notice you don't get import settings on it. The same is true of a text file (which is all the json file is), if you click on it, you notice the inspector doesn't show you the text.

    My guess is it may not be able to find it, because it doesn't actually know what the file is. Try a different folder to test this theory with the json file and see if it works.
     
    James-Sullivan likes this.
  5. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    You're right. Moving it outside of StreamingAssets worked. I didn't know that there was anything particularly special about that folder, I just blindly followed some online guides. Thanks.

    Do you know of any fancy tools to search text files? I need to pass in a string and have it return the line that the string is on. Could probably write it myself, but I'd rather not do work someone else has already done for me.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I'm not aware of a tool for it. Wouldn't surprise me if there is something out there though.

    Yeah, streaming assets is a special folder. Be aware that all files in that folder will be included in your build even if you don't use the files.
     
    James-Sullivan likes this.
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I don't know the purpose of your code, I'm guessing you're making some sort of Editor tool, just be warned AssetDatabase is in the UnityEditor and will only work in the Unity Editor, not in builds.
    For builds you will need to use System.IO, to read files from disk, Resources.Load to load resources bundled with build, or a plain serialized reference to a TextAsset object.
     
    James-Sullivan likes this.
  8. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    Oh, I did not know that , thanks.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Or...the WWW or UnityWebRequest on some platforms such as Android.
    Resources.Load I think also only works with stuff included in a Resources folder.