我有2个AssetManager实例:一个用于基本纹理,一个用于高质量纹理。基本纹理位于" android / assets"文件夹和高质量的纹理包装在zip文件中。此文件夹中的内容(文件名)是相同的 - 在zip存档中只有更好的质量纹理。
AssetManager抛出异常:"无法加载资产的依赖关系: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 );
使用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();
}
我做错了什么?
答案 0 :(得分:2)
Yeaaahhh,我发现了它:) ArchiveFileHandle无法加载TextureAtlas的依赖项,因为他无法找到这些文件。查看zip存档时,您必须将'\'char替换为'/'。该错误发生在ArchiveFileHandle构造函数之一。这一行:
archiveEntry = this.archive.getEntry(file.getPath());
应该是:
archiveEntry = this.archive.getEntry(file.getPath().replace('\\', '/'));
现在一切正常
答案 1 :(得分:0)
您必须设置AssetManager的加载程序以加载Textureatlus 像这样
amHQ.setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver));