更新拉取图片的缓存代码

This commit is contained in:
suntao committed 2021-05-08 14:38:23 +08:00
1 parent 696fd256a8
commit 6645310601
22 files changed
+149 -119

No files matched your search

+2 -7
View File
@@ -44,7 +44,7 @@ public static class App
public static IPEndPoint UdpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 11000);
public static IPEndPoint TcpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 21001);
public static Dictionary<string, Dictionary<dynamic, Texture>> propTextureCache;
static App()
{
#if !UNITY_EDITOR
@@ -52,11 +52,6 @@ public static class App
UdpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21000);
TcpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21001);
#endif
propTextureCache = new Dictionary<string, Dictionary<dynamic, Texture>>()
{
{"mapImgCache",new Dictionary<dynamic, Texture>() },
{"mapHBCache",new Dictionary<dynamic, Texture>() },
{"recordCache",new Dictionary<dynamic, Texture>() },
};
}
}
+4 -7
View File
@@ -434,7 +434,7 @@ public class LoginController : MonoBehaviour
var r = await ConfigHelper.userApi.GetHeadImg(s);
if (!string.IsNullOrEmpty(r))
{
Utils.DisplayImage(StartCoroutine, loginContainer.Find("Avatar").GetComponent<RawImage>(), r);
Utils.DisplayImage(loginContainer.Find("Avatar").GetComponent<RawImage>(), r);
avatarName = s;
}
else
@@ -568,13 +568,11 @@ public class LoginController : MonoBehaviour
if (data.Value<string>("success") != null && data.Value<string>("success") == "False")
{
wxLogin2.gameObject.SetActive(true);
Utils.DisplayImage(StartCoroutine,
wxLogin2.Find("Avatar").GetComponent<RawImage>(),
Utils.DisplayImage(wxLogin2.Find("Avatar").GetComponent<RawImage>(),
wxInfoJson.Value<string>("headimgurl"));
wxLogin2.Find("NickName").GetComponent<Text>().text = wxInfoJson.Value<string>("nickname");
wxLogin3.gameObject.SetActive(true);
Utils.DisplayImage(StartCoroutine,
wxLogin3.Find("Avatar").GetComponent<RawImage>(),
Utils.DisplayImage(wxLogin3.Find("Avatar").GetComponent<RawImage>(),
wxInfoJson.Value<string>("headimgurl"));
wxLogin3.Find("NickName").GetComponent<Text>().text = wxInfoJson.Value<string>("nickname");
pageNums = 5;
@@ -790,8 +788,7 @@ public class LoginController : MonoBehaviour
}
else
{
Utils.DisplayImage(StartCoroutine,
wxLogin3.Find("Avatar").GetComponent<RawImage>(),
Utils.DisplayImage(wxLogin3.Find("Avatar").GetComponent<RawImage>(),
data.WxHeadImg);
}
wxLogin3.Find("Wx").gameObject.SetActive(type == 1);
+1 -1
View File
@@ -117,7 +117,7 @@ public class Tips : MonoBehaviour
//altitudeGraph
Utils.DisplayImage(StartCoroutine, altitudeGraph, _model.AltitudeGraph);
Utils.DisplayImage(altitudeGraph, _model.AltitudeGraph);
//Utils.DisplayImage(StartCoroutine, altitudeGraph, "http://192.168.0.97:5082/Map/AltitudeGraph?id=1215");
}
@@ -31,7 +31,7 @@ public class QuickLoginUser : MonoBehaviour
this.user = user;
if (!string.IsNullOrEmpty(user.Avatar))
{
Utils.DisplayImage(StartCoroutine, transform.Find("Avatar").GetComponent<RawImage>(), user.Avatar);
Utils.DisplayImage(transform.Find("Avatar").GetComponent<RawImage>(), user.Avatar);
}
else
{
+4 -24
View File
@@ -115,30 +115,10 @@ public class MapItem : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler,
UIManager.Instance.collectDict[myMap.IsFavorite];
transform.Find("BtnInfo").GetComponent<Button>().onClick.AddListener(Info);
transform.Find("BtnRide").GetComponent<Button>().onClick.AddListener(Ride);
if (App.propTextureCache["mapImgCache"].ContainsKey(myMap.Id))
{
transform.Find("MapTitleImg").GetComponent<RawImage>().texture =
App.propTextureCache["mapImgCache"][myMap.Id];
}
else
{
Utils.DisplayImage(StartCoroutine, transform.Find("MapTitleImg").GetComponent<RawImage>(), myMap.CoverImage, (t) =>
{
App.propTextureCache["mapImgCache"].Add(myMap.Id, t);
});
}
if (App.propTextureCache["mapHBCache"].ContainsKey(myMap.Id))
{
transform.Find("MapHBImg").GetComponent<RawImage>().texture =
App.propTextureCache["mapHBCache"][myMap.Id];
}
else
{
Utils.DisplayImage(StartCoroutine, transform.Find("MapHBImg").GetComponent<RawImage>(), myMap.AltitudeGraph,(t)=>
{
App.propTextureCache["mapHBCache"].Add(myMap.Id, t);
});
}
Utils.DisplayImage(transform.Find("MapTitleImg").GetComponent<RawImage>(), myMap.CoverImage, true);
Utils.DisplayImage(transform.Find("MapHBImg").GetComponent<RawImage>(), myMap.AltitudeGraph, true);
SetActive4Button(false);
}
@@ -137,7 +137,7 @@ public class BigMapController : PFUIPanel
//headImage.material = material;
SetRounded(headImage.transform, rect.height);
Utils.DisplayImage(StartCoroutine, headImage, App.CurrentUser.WxHeadImg);
Utils.DisplayImage(headImage, App.CurrentUser.WxHeadImg);
UIManager.AddEvent(headImage.gameObject, EventTriggerType.PointerClick, (e) =>
{
@@ -187,7 +187,7 @@ public class EditUserController : PFUIPanel
if (!string.IsNullOrWhiteSpace(currentUser.WxHeadImg))
{
Utils.DisplayImage(StartCoroutine, mHeadImage, currentUser.WxHeadImg);
Utils.DisplayImage(mHeadImage, currentUser.WxHeadImg);
}
else
{
@@ -355,7 +355,7 @@ public class EditUserController : PFUIPanel
if (r.result)
{
var img = JObject.FromObject(r.data).Value<string>("WxHeadImg");
Utils.DisplayImage(StartCoroutine, mHeadImage, img);
Utils.DisplayImage(mHeadImage, img);
var userinfos = UIManager.Instance.userInfos;
var i = userinfos.FindIndex(x => x.Id == App.CurrentUser.Id);
if (i != -1)
@@ -151,7 +151,7 @@ public class HomeController : PFUIPanel
}
if (!string.IsNullOrEmpty(App.CurrentUser.WxHeadImg))
{
Utils.DisplayImage(StartCoroutine, userInfo.Find("Avatar").GetComponent<RawImage>(), App.CurrentUser.WxHeadImg);
Utils.DisplayImage(userInfo.Find("Avatar").GetComponent<RawImage>(), App.CurrentUser.WxHeadImg);
}
else
{
@@ -223,7 +223,7 @@ public class MapListController : PFUIPanel
headImage.material = material;
//if (App.FromLogin)
//{
Utils.DisplayImage(StartCoroutine, headImage, App.CurrentUser.WxHeadImg);
Utils.DisplayImage(headImage, App.CurrentUser.WxHeadImg);
//}
@@ -80,7 +80,7 @@ public class UserInfoController : PFUIPanel
var user = App.CurrentUser;
if (!string.IsNullOrEmpty(user.WxHeadImg))
{
Utils.DisplayImage(StartCoroutine, infoPanel.Find("Avatar").GetComponent<RawImage>(), user.WxHeadImg);
Utils.DisplayImage(infoPanel.Find("Avatar").GetComponent<RawImage>(), user.WxHeadImg);
}
else
{
@@ -34,17 +34,9 @@ public class RouteItem : MonoBehaviour, IPointerExitHandler, IPointerEnterHandle
left = transform.Find("Left");
row1 = left.Find("Main").Find("Row1");
row2 = left.Find("Main").Find("Row2");
if (App.propTextureCache["recordCache"].ContainsKey(result.Id))
{
transform.Find("CoverImage").GetComponent<RawImage>().texture = App.propTextureCache["recordCache"][result.Id];
}
else
{
Utils.DisplayImage(StartCoroutine, transform.Find("CoverImage").GetComponent<RawImage>(), result.RouteImage,(t)=>
{
App.propTextureCache["recordCache"].Add(result.Id, t);
});
}
Utils.DisplayImage(transform.Find("CoverImage").GetComponent<RawImage>(), result.RouteImage, true);
left.Find("Main").Find("Name").GetComponent<Text>().text = result.RouteName;
left.Find("Main").Find("Time").GetComponent<Text>().text = result.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
row1.Find("Time").GetComponent<Text>().text = $"<color={titleColor}>Riding time:</color>{result.TrainingTime}";
+2 -1
View File
@@ -16,6 +16,7 @@ using System;
public class UIManager : MonoBehaviour
{
[SerializeField]
private PFUIPanel mMainPanel;
public PFUIPanel MainPanel
@@ -404,7 +405,7 @@ public class UIManager : MonoBehaviour
public static void ShowMapListPanel()
{
Debug.Log("清空");
App.propTextureCache["recordCache"].Clear();
//App.propTextureCache["recordCache"].Clear();
UIManager.Show(UIManager.Instance.MapListPanel, UIManager.Instance.MainPanel);
}
public static void ShowMapDetailPanel(int id)
+30 -2
View File
@@ -205,16 +205,44 @@ namespace Assets.Scripts
/*删除父亲节点下的所有孩子*/
/*显示网络图片*/
private static Dictionary<string, Texture> propTextureCache = new Dictionary<string, Texture>();
public delegate Coroutine StartCoroutine(IEnumerator routine);
public static void DisplayImage(StartCoroutine startCoroutine,RawImage img, string url, Action<Texture> action=null)
public static void DisplayImage(RawImage img, string url, bool cache = false)
{
startCoroutine(DownloadImage(img, url,action));
//可以考虑缓存到硬盘里
//Texture2D tex = null;
//byte[] fileData;
//
//if (File.Exists(filePath))
//{
// fileData = File.ReadAllBytes(filePath);
// tex = new Texture2D(2, 2);
// tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
//}
if (cache)
{
if (propTextureCache.ContainsKey(url))
{
img.texture = propTextureCache[url];
return;
}
}
UIManager.Instance.StartCoroutine(DownloadImage(img, url,(texture)=> {
if (cache)
{
propTextureCache.Add(url, texture);
}
}));
}
public static void DisplayImageAysnc(StartCoroutine startCoroutine, RawImage img, string url,Action<string> action)
{
startCoroutine(DownloadImageCallBack(img, url, action));
}
static IEnumerator DownloadImage(RawImage img,string MediaUrl, Action<Texture> action = null)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);