SingleMaterialItem.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class SingleMaterialItem : MonoBehaviour
  8. {
  9. public event Action<MaterialObl, bool> OnSelectMaterial;
  10. private Button m_Btn;
  11. private Image m_Icon;
  12. private Text m_Name;
  13. private MaterialObl m_Data;
  14. private bool m_IsLocal;
  15. private void Awake()
  16. {
  17. m_Btn = GetComponent<Button>();
  18. m_Btn.onClick.AddListener(OnClickBtn);
  19. m_Icon = transform.Find("Image").GetComponent<Image>();
  20. m_Name = transform.Find("Name").GetComponent<Text>();
  21. }
  22. private void OnClickBtn()
  23. {
  24. if (OnSelectMaterial != null)
  25. {
  26. OnSelectMaterial(m_Data, m_IsLocal);
  27. }
  28. }
  29. public void SetData(MaterialObl material, bool islocal)
  30. {
  31. m_Data = material;
  32. m_IsLocal = islocal;
  33. if (m_IsLocal)
  34. {
  35. CalLocaldata();
  36. }
  37. else
  38. {
  39. CalUpdateData();
  40. }
  41. }
  42. private void CalLocaldata()
  43. {
  44. m_Name.text = m_Data.name;
  45. m_Icon.sprite = ResMgr.Instance.Load<Sprite>(m_Data.icon);
  46. }
  47. private void CalUpdateData()
  48. {
  49. DownloadData data = new DownloadData();
  50. string filename = Path.GetFileName(m_Data.icon);
  51. data.name = filename;
  52. data.type = 1;
  53. data.downloadPath = m_Data.icon;
  54. data.localSavePath = DownloadManager.Instance.LocaDataPath + "/Icon/" + filename;
  55. data.updateTime = m_Data.updateTime;
  56. if (!string.IsNullOrWhiteSpace((data.downloadPath)))
  57. {
  58. DownloadManager.Instance.LoadMap(data, m_Icon, (tex) =>
  59. {
  60. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  61. m_Icon.sprite = sprite;
  62. });
  63. }
  64. m_Name.text = string.IsNullOrWhiteSpace(m_Data.name) ? "" : m_Data.name;
  65. }
  66. }