TextAsset

I’d like to store textfiles with other file endings than “.txt” as TextAssets.
Is it possible to override the TextAssets class to allow for other file extensions to be recognized as text files?

There are some .po files in the project edited and updated with external translation software. Now they have to be renamed each time from “.po” to “.txt”.

All the supported types are listed here. I don’t know that you can override the class honestly.

You can still load any file with System.IO instead if you have to. Its nearly as simple as using Text Asset, and more flexible.
There are plenty of examples out there. Just use File.ReadAllText() or look into StreamReader for example.

For your translation files you could just do this:

string filepath = path+"lang_"+MyLanguage+".po";
if(File.Exists(filepath))
var languageFile = File.ReadAllText(filepath);

This is actually really easy to handle with the relatively new ScriptedImporter API:

[ScriptedImporter(0, "po")]
public class PoConverter : ScriptedImporter {
    public override void OnImportAsset(AssetImportContext ctx)
    {
        var contents = File.ReadAllText(ctx.assetPath);
        var text = new TextAsset(contents);
        ctx.AddObjectToAsset("Text", text);
    }
}

Your .po objects are now treated as TextAssets. You might have to reimport them after adding that script, to make them run. Or maybe not, don’t have that much experience with the API.

1 Like

Looks like it could be very helpful!

Perfect, thanks.
This kind of works on 2017.2

[ScriptedImporter(0, "po")]
public class PoConverter : ScriptedImporter {
    public override void OnImportAsset(AssetImportContext ctx)
    {
        var contents = File.ReadAllText(ctx.assetPath);
        File.WriteAllText (ctx.assetPath + ".txt", contents);
        AssetDatabase.Refresh ();
    }
}

It gets the job done, but of course throws an error when breaking the pipe and not providing no main asset for the ScriptedImporter.