I searched the wiki for an answer and found that there is no Loader for text files (see list here).
So I followed the instructions on the same page to write my own loader (the instructions can be found here). This is what I came up with:
The Text class is basically a wrapper around a simple String:
package mygame.assets; import com.badlogic.gdx.files.FileHandle; public class Text { private String string; public Text() { this.string = new String("".getBytes()); } public Text(byte[] data) { this.string = new String(data); } public Text(String string) { this.string = new String(string.getBytes()); } public Text(FileHandle file) { this.string = new String(file.readBytes()); } public Text(Text text) { this.string = new String(text.getString().getBytes()); } public void setString(String string) { this.string = string; } public String getString() { return this.string; } public void clear() { this.string = new String("".getBytes()); } }
The TextLoader class is the corresponding loader class for an asset of type Text:
package mygame.assets.loaders; import com.badlogic.gdx.assets.AssetDescriptor; import com.badlogic.gdx.assets.AssetLoaderParameters; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.AsynchronousAssetLoader; import com.badlogic.gdx.assets.loaders.FileHandleResolver; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.Array; import mygame.assets.Text; public class TextLoader extends AsynchronousAssetLoader<Text, TextLoader.TextParameter> { public TextLoader(FileHandleResolver resolver) { super(resolver); } Text text; @Override public void loadAsync(AssetManager manager, String fileName, FileHandle file, TextParameter parameter) { this.text = null; this.text = new Text(file); } @Override public Text loadSync(AssetManager manager, String fileName, FileHandle file, TextParameter parameter) { Text text = this.text; this.text = null; return text; } @SuppressWarnings("rawtypes") @Override public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, TextParameter parameter) { return null; } public static class TextParameter extends AssetLoaderParameters<Text> { } }
Then, to register the Text asset and its TextLoader, in the create() method of the main class:
@Override public void create() { // ... // Assuming that the main class contains the global AssetManager assetManager.setLoader( Text.class, new TextLoader( new InternalFileHandleResolver() ) ); // ... }
To use it:
assetLoader.load( new AssetDescriptor< Text >( "data/colors.txt", Text.class, new TextLoader.TextParameter() ); String string = assetLoader.get( "data/colors.txt", Text.class ).getString(); // Process the string