Couldn't load TextureAtlas from zip archive via AssetManager - libgdx

喜欢而已 提交于 2019-12-08 04:32:33

问题


I have 2 instances of AssetManager: one for basic textures and one for high quality textures. Basic textures are located in "android/assets" folder and high quality textures are packed in zip file. Content (file names) in this folders are the same - there are only better quality textures in zip archive.

AssetManager throws an exception: "Couldn't load dependencies of asset: teamLogo.png" when I'm trying to load TextureAtlas from zip file. When I'm loading Texture file everything is ok. Loading TextureAtlas works only from 'android/assets' folder.

AssetManager using 'android/assets' - everything is ok:

AssetManager am = new AssetManager();
am.load( "images/image.png", Texture.class );
am.load( "images/teamLogo.pack", TextureAtlas.class );

AssetManager using zip archive - can't load TextureAtlas:

ZipFile archive = new ZipFile(expansionFileHandle.file());
ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive);
AssetManager amHQ = new AssetManager(resolver);

This works fine:

amHQ.load( "images/image.png", Texture.class );

This doesn't work:

amHQ.load( "images/teamLogo.pack", TextureAtlas.class );

ArchiveFileHandle class:

public class ArchiveFileHandle extends FileHandle 
{ 
final ZipFile archive;
final ZipEntry archiveEntry;

public ArchiveFileHandle (ZipFile archive, File file) 
{
    super(file, FileType.Classpath);
    this.archive = archive;
    archiveEntry = this.archive.getEntry(file.getPath());
}

public ArchiveFileHandle (ZipFile archive, String fileName) 
{
    super(fileName.replace('\\', '/'), FileType.Classpath);
    this.archive = archive;
    this.archiveEntry = archive.getEntry(fileName.replace('\\', '/'));
}

@Override
public FileHandle child (String name) 
{
    name = name.replace('\\', '/');
    if (file.getPath().length() == 0) 
        return new ArchiveFileHandle(archive, new File(name));
    return new ArchiveFileHandle(archive, new File(file, name));
}

@Override
public FileHandle sibling (String name) 
{
    name = name.replace('\\', '/');
    if (file.getPath().length() == 0) 
        throw new GdxRuntimeException("Cannot get the sibling of the root.");
    return new ArchiveFileHandle(archive, new File(file.getParent(), name));
}

@Override
public FileHandle parent () 
{
    File parent = file.getParentFile();
    if (parent == null) 
    {
        if (type == FileType.Absolute)
            parent = new File("/");
        else
            parent = new File("");
    }
    return new ArchiveFileHandle(archive, parent);
}

@Override
public InputStream read () 
{
    try 
    {
        return archive.getInputStream(archiveEntry);
    } 
    catch (IOException e) 
    {
        throw new GdxRuntimeException("File not found: " + file + " (Archive)");
    }
}

@Override
public boolean exists() 
{
    return archiveEntry != null;
}

@Override
public long length () 
{
    return archiveEntry.getSize();
}

@Override
public long lastModified () 
{
    return archiveEntry.getTime();
}

What am I doing wrong?


回答1:


Yeaaahhh, I found it :) ArchiveFileHandle couldn't load dependencies of TextureAtlas because he can't find those files. When looking in zip archive you have to replace '\' char to '/'. The bug was in one of ArchiveFileHandle constructors. This line:

archiveEntry = this.archive.getEntry(file.getPath());

should be:

archiveEntry = this.archive.getEntry(file.getPath().replace('\\', '/'));

Now everything works fine




回答2:


You have to set loader of AssetManager for loading Textureatlus like this

amHQ.setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver));


来源:https://stackoverflow.com/questions/38018352/couldnt-load-textureatlas-from-zip-archive-via-assetmanager-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!