无法通过AssetManager – libgdx从zip存档加载TextureAtlas
我有2个AssetManager实例:一个用于基本纹理,一个用于高质量纹理。 基本纹理位于“android / assets”文件夹中,高质量纹理包装在zip文件中。 此文件夹中的内容(文件名)是相同的 – 在zip存档中只有更好的质量纹理。
当我尝试从zip文件加载TextureAtlas时,AssetManager会抛出exception:“无法加载asset:teamLogo.png的依赖项”。 当我加载纹理文件时一切正常。 加载TextureAtlas仅适用于’android / assets’文件夹。
使用’android / assets’的AssetManager – 一切正常:
AssetManager am = new AssetManager(); am.load( "images/image.png", Texture.class ); am.load( "images/teamLogo.pack", TextureAtlas.class );
使用zip存档的AssetManager – 无法加载TextureAtlas:
ZipFile archive = new ZipFile(expansionFileHandle.file()); ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive); AssetManager amHQ = new AssetManager(resolver);
这很好用:
amHQ.load( "images/image.png", Texture.class );
这不起作用:
amHQ.load( "images/teamLogo.pack", TextureAtlas.class );
ArchiveFileHandle类:
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(); }
我究竟做错了什么?
Yeaaahhh,我发现它:) ArchiveFileHandle无法加载TextureAtlas的依赖项,因为他找不到那些文件。 查看zip存档时,您必须将’\’char替换为’/’。 该错误发生在ArchiveFileHandle构造函数之一。 这一行:
archiveEntry = this.archive.getEntry(file.getPath());
应该:
archiveEntry = this.archive.getEntry(file.getPath().replace('\\', '/'));
现在一切正常
你必须设置AssetManager的加载器来加载这样的Textureatlus
amHQ.setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver));