无法通过 AssetManager 从 zip 存档加载 TextureAtlas - libgdx

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

我有 2 个 AssetManager 实例:一个用于基本纹理,一个用于高质量纹理。基本纹理位于 "android/assets" 文件夹中,高质量纹理打包在 zip 文件中。此文件夹中的内容(文件名)相同 - zip 存档中只有质量更好的纹理。

AssetManager 抛出异常:"Couldn't load dependencies of asset: teamLogo.png" 当我尝试从 zip 文件加载 TextureAtlas 时。当我加载纹理文件时,一切正常。加载 TextureAtlas 仅适用于 'android/assets' 文件夹。

AssetManager 使用 'android/assets' - 一切正常:

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

AssetManager 使用 zip 存档 - 无法加载 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 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();
}

我做错了什么?

您必须设置 AssetManager 的加载程序才能加载 Textureatlus 像这样

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

是的,我找到了 :) ArchiveFileHandle 无法加载 TextureAtlas 的依赖项,因为他找不到那些文件。查看 zip 存档时,您必须将“\”字符替换为“/”。该错误存在于 ArchiveFileHandle 构造函数之一中。这一行:

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

应该是:

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

现在一切正常