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

AssetDatabase.LoadAssetAtPath<TextAsset> returns null for JS file in Unity 2021.2.19f1

Discussion in 'Asset Database' started by pws-devs, Apr 21, 2022.

  1. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    We have a PostBuild script that loads the js file as TextAsset
    We have been having this script since version 2019.2.18f1 and had no issue Loading the js file as TextAsset
    But when we updated the version to 2021.2.19f1 the LoadAssetAtPath for js file returns null.
    But the function LoadAssetAtPath works fine without any issue for the html file type.
    The js files loaded are all present in the Project Assets folder.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    EDIT: disregard my mad ramblings below: I realize you're talking about an editor-time change... I thought you were speaking of final bundling build rules. You might still try renaming it just to see if that's what is in play, and possibly file a bug if it is.

    disregard:

    It's possible that a change to Unity's bundling rules now excludes ALL "source code" files.

    This might be simply to preclude inadvertent data leaks of source code.

    I know if you drop scripts into a
    Resources
    directory, the files are NOT included.

    You could test this instantly just by duping the file, renaming it as TXT file, connecting it and seeing if it works.
     
  3. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Yes its confirmed

    TextAsset testtxt = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/PostBuild/test.txt");
    TextAsset testtxtjs = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/PostBuild/test.js");
    both the above files are present in the PostBuild script and the first one returns the text in test.txt
    whereas the test.js returns NULL.
    I will file a bug.

    Thanks
     
  4. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    An alternate solution to this that I am currently using since the js file wont load in Unity 2021.2.19f1(which is the version I am currently using)
    File.ReadAllBytes and converting the byte array to string.
     
  5. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    51
  6. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    51
    Hi!

    JavaScripts are not text assets since they were dropped in 2017, there was legacy code that still treated them as such. (https://docs.unity3d.com/Manual/class-TextAsset.html)

    By dropping that, we allow the flexibility of allowing users to manage them as custom assets themselves, an
    example here: https://forum.unity.com/threads/loading-a-file-with-a-custom-extension-as-a-textasset.625078/

    It will also allow future dev for support of Unity CloudCode.

    You can have an importer that loads them as TextAssets, but be aware that in the future they will be treated as CloudCode assets if you're using Unity Gaming Services packages. (It will remain an opt-in).

    If you have any more questions, dont hesitate to poke this thread!