Search Unity

Loading a file with a custom extension as a TextAsset

Discussion in 'Addressables' started by Firewolf34, Feb 6, 2019.

  1. Firewolf34

    Firewolf34

    Joined:
    May 14, 2013
    Posts:
    12
    Perhaps this is not the right subforum to post this in, as I realize the Addressable Assets System is based on the underlying Unity Asset Database and how it interprets files... but I have a similar issue while using the AAS to @MaxBowser in his thread here: https://forum.unity.com/threads/loading-scriptableobjects.622075/

    When I try to load a text file with a custom extension (they are actually a custom Class serialized into JSON and written to disk, marked as Addressable) using this form:
    Addressables.LoadAssets<TextAsset>("Assets/BlockConfigs/Default.bconfig", null).Completed += onLoadHandler;

    I get a bunch of nulls back in the onLoadHandler Result array, because Unity does not see these files as TextAssets. I need Unity to interpret my file as a text file, and load it as a TextAsset, despite it's non-txt extension. Is there any way I could get the Addressable Assets system to help me with this? Perhaps by using a Resource Handler or something?

    Here's what my AddressableAssets database currently looks like, as you can see only two objects exist:


    Everything works fine when the extension of these files end in ".txt", but the system returns nulls if they have my custom extension.

    As you can see, they are pretty simple UTF-8 formatted JSON text files:

    Code (CSharp):
    1. {
    2.     "renderables": {
    3.         "renderable0": {
    4.             "instanceID": 6316
    5.         },
    6.         "renderable1": {
    7.             "instanceID": 0
    8.         },
    9.         "renderable2": {
    10.             "instanceID": 0
    11.         },
    12.         "renderable3": {
    13.             "instanceID": 0
    14.         },
    15.         "renderable4": {
    16.             "instanceID": 0
    17.         }
    18.     },
    19.     "castShadows": false,
    20.     "receiveShadows": false,
    21.     "renderableGroup": {
    22.         "i0": 0,
    23.         "i1": -1,
    24.         "i2": -1,
    25.         "i3": -1,
    26.         "i4": -1
    27.     }
    28. }
    Thanks in advance for your help!!

    Here's the script I used for testing this, if you're interested (it's very short):
    https://pastebin.com/Rn8m826i
     
    Last edited: Feb 6, 2019
    neonblitzer and a0085913 like this.
  2. kleicht

    kleicht

    Joined:
    Aug 1, 2018
    Posts:
    16
    According to the manual:
    "The supported text formats are:

    • .txt
    • .html
    • .htm
    • .xml
    • .bytes
    • .json
    • .csv
    • .yaml
    • .fnt
    "
     
    andrew_pearce_ likes this.
  3. Firewolf34

    Firewolf34

    Joined:
    May 14, 2013
    Posts:
    12
    Oh, nice. I suppose I can just use JSON extension then. Thanks!

    I would still appreciate if anyone had any info about how to do a custom extension though, if anyone knows a way. I did look into the whole Custom Importer experimental stuff, didn't work for my use case though (can't create TextAssets like that). But thanks again, man.
     
  4. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    you can use scriptable asset importer and convert the file to textasset on import. Not sure that it will work with the whole pipeline, but its going to be an actual asset.
     
  5. NamelessPerson

    NamelessPerson

    Joined:
    Apr 17, 2017
    Posts:
    26
    Thanks for mentioning this! After a bit of headache I figured it out:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Experimental.AssetImporters;
    4. using System.IO;
    5.  
    6. [ScriptedImporter( 1, "srt" )]
    7. public class SrtImporter : ScriptedImporter {
    8.     public override void OnImportAsset( AssetImportContext ctx ) {
    9.         TextAsset subAsset = new TextAsset( File.ReadAllText( ctx.assetPath ) );
    10.         ctx.AddObjectToAsset( "text", subAsset );
    11.         ctx.SetMainObject( subAsset );
    12.     }
    13. }
    This is a Scripted Importer to import .srt subtitle files as TextAssets. Do note if you add this to your project it may not automatically re-import all your files with the new type. You can right click one file and choose reimport to test it though!
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    That's super useful! fyi to others in case this isn't obvious, this is an editor script so put it in an Editor folder
     
  7. Mantic

    Mantic

    Joined:
    Jan 5, 2013
    Posts:
    4
    Thaaaaank you! Just what I was looking for!