C# Unity iOS应用程序映像缓存问题
我已经接管了一个应用程序的开发,这个应用程序完全没有一个bug,需要通过互联网连接来加载图像,因为尽管有人试图这样做,但它无法从缓存中访问图像 有人能帮我找出下面的错误吗C# Unity iOS应用程序映像缓存问题,c#,ios,url,unity3d,caching,C#,Ios,Url,Unity3d,Caching,我已经接管了一个应用程序的开发,这个应用程序完全没有一个bug,需要通过互联网连接来加载图像,因为尽管有人试图这样做,但它无法从缓存中访问图像 有人能帮我找出下面的错误吗 public class SpriteCache : Singleton<SpriteCache> { Dictionary<string, Sprite> _cache = new Dictionary<string, Sprite>(); public void Loa
public class SpriteCache : Singleton<SpriteCache>
{
Dictionary<string, Sprite> _cache = new Dictionary<string, Sprite>();
public void LoadSprite(string url, Action<Sprite> callback)
{
StartCoroutine(LoadSpriteCoroutine(url, callback));
}
public IEnumerator LoadSpriteCoroutine(string url, Action<Sprite> callback)
{
if (_cache.ContainsKey(url))
{
callback(_cache[url]);
yield break;
}
var www = new WWW(url);
while (!www.isDone)
{
yield return www;
}
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogErrorFormat("Tried to obtain texture at '{0}' but received error '{1}'", url, www.error);
yield break;
}
var texture = www.texture;
if (texture == null)
{
Debug.LogErrorFormat("No texture found at '{0}'!", url);
yield break;
}
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
_cache[url] = sprite;
callback(sprite);
}
这会抓取存储在服务器上的图像,据我所知,在抓取图像的一个实例之后,这会将该项目放在缓存中以供以后使用
我已经尝试使用以下更新的方法,看看是否可以修复它
var www = WWW.LoadFromCacheOrDownload(url, 1)
这导致它无法以任何容量工作,并且从不更改占位符中的图像
“LoadSpriteCorroutine”中的第一个if语句应该通过检查url是否有键来捕获sprite是否已经存在于“\u cache”字典中,该键应该位于第一个运行实例和internet连接之后
从_缓存加载图像(如果图像在其中):
if (_cache.ContainsKey(url))
{
callback(_cache[url]);
yield break;
}
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
_cache[url] = sprite;
将图像添加到_缓存(如果以前不在该缓存中):
if (_cache.ContainsKey(url))
{
callback(_cache[url]);
yield break;
}
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2, texture.height / 2));
_cache[url] = sprite;
所以我想出了一个解决办法 对于iOS来说,所有其他将图像保存到缓存的方法似乎都失败了,我删除了所有以前的“\u缓存”内容 此保存功能对我有效:
//if file doesn't exist then save it for fututre reference
if (!File.Exists(_FileLocation + texName))
{
byte[] bytes;
if (Path.GetExtension(texName) == ".png")
{
bytes = texture.EncodeToPNG();
}
else
{
bytes = texture.EncodeToJPG();
}
File.WriteAllBytes(Application.persistentDataPath + texName, bytes);
}
该部分放在“回调(精灵)”之后
对已保存项目的检查放在旧的“if(_cache.ContainsKey(url))”部分的位置
texName = Path.GetFileName(texName);
if (File.Exists( _FileLocation + texName))
{
url = _FileLocation + "/"+ texName;
}
请注意,“LoadSpriteCoroutine”现在在其调用中使用了一个额外的字符串参数“texName”,它只是url的一个缩短实例,然后将其剪切为文件名和扩展名。如果它找到了匹配项,那么它将用持久文件路径+文件名替换url以在本地获取它,而不是像正常情况那样通过网络获取它
_FileLocation的定义如下
string _FileLocation;
public void Start()
{
_FileLocation = Application.persistentDataPath;
}
在该方法中引用它的原因是为了节省通过应用程序调用地址的性能,如果您正在处理跨平台解决方案,那么保存的数据路径可能需要更改,这样您就可以使用检测if或case语句来更改_FileLocation以满足您的需要。您的问题不是清楚的您需要解释您将要加载的文件放在何处,以及此代码是如何失败的。您可以尝试一下