loadTextureAtlas static method
Loads a texture atlas from an image and XML/JSON data file.
The imagePath
argument is the path to the image file, while the optional
dataPath
argument is the path to the XML data file. The resolution
argument sets the resolution of the texture. The cacheId
argument sets
the ID to be used to cache the texture atlas in memory.
If the cacheId
argument is not null
, and there is a texture atlas
already cached with the same ID, this method will return the cached atlas
instead of loading a new one.
If the dataPath
argument is not provided, this method will attempt to
guess the path to the XML data file based on the imagePath
argument.
Returns a Future that completes with a GTextureAtlas
object.
Implementation
static Future<GTextureAtlas> loadTextureAtlas(
String imagePath, {
String? dataPath,
double resolution = 1.0,
String? cacheId,
}) async {
if (cacheId != null && atlasCache.containsKey(cacheId)) {
return atlasCache[cacheId]!;
}
if (dataPath == null) {
/// if no index at the end, guess it.
var lastIndex = imagePath.lastIndexOf('.');
if (lastIndex == -1) {
lastIndex = imagePath.length;
}
var basePath = imagePath.substring(0, lastIndex);
dataPath = '$basePath.xml';
if (kDebugMode) {
print('Warning: using default xml data: $dataPath');
}
}
var texture = await ResourceLoader.loadTexture(imagePath, resolution);
var xmlData = await ResourceLoader.loadString(dataPath);
final atlas = GTextureAtlas(texture, xmlData);
if (cacheId != null) {
atlasCache[cacheId] = atlas;
}
return atlas;
}