我有 2 个 AssetManager 实例:一个用于基本纹理,一个用于高质量纹理。基本纹理位于“android/assets”文件夹中,高质量纹理打包在 zip 文件中。此文件夹中的内容(文件名)相同 - zip 存档中只有质量更好的纹理。
当我尝试从 zip 文件加载 TextureAtlas 时,AssetManager 引发异常:“无法加载资产的依赖项:teamLogo.png”。当我加载纹理文件时,一切正常。加载 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 类:
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();
}
我究竟做错了什么?