SingleMaterialItem.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public Image m_Icon;
  12. public Text m_aame;
  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_aame.text = m_Data.name;
  45. m_Icon.sprite = ResMgr.Instance.Load<Sprite>(m_Data.icon);
  46. }
  47. private void CalUpdateData()
  48. {
  49. DownLoadMaterial data = new DownLoadMaterial();
  50. string filename = Path.GetFileName(m_Data.icon);
  51. // data.name = filename;
  52. data.type = "-9";
  53. data.downLoadPath = m_Data.icon;
  54. data.localLoadPath =Application.persistentDataPath + "/Material/" + filename;
  55. data.updataTime = 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. MsgHandler.AddListener(data.downLoadPath, HandleMsg);
  64. DownloadResManager.Instance.DownLoad(data);
  65. }
  66. m_aame.text = string.IsNullOrWhiteSpace(m_Data.name) ? "" : m_Data.name;
  67. }
  68. private void HandleMsg(Msg msg)
  69. {
  70. if(msg.Value!=null)
  71. {
  72. Texture2D tex = new Texture2D(1, 1);
  73. tex.LoadImage((byte[])msg.Value);
  74. Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  75. m_Icon.sprite = sprite;
  76. }
  77. }
  78. }