1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.UI;
- public class SingleMaterialItem : MonoBehaviour
- {
- public event Action<MaterialObl, bool> OnSelectMaterial;
- private Button m_Btn;
- public Image m_Icon;
- public Text m_aame;
- private MaterialObl m_Data;
- private bool m_IsLocal;
- private void Awake()
- {
- m_Btn = GetComponent<Button>();
- m_Btn.onClick.AddListener(OnClickBtn);
- // m_Icon = transform.Find("Image").GetComponent<Image>();
- // m_Name = transform.Find("Name").GetComponent<Text>();
- }
- private void OnClickBtn()
- {
- if (OnSelectMaterial != null)
- {
- OnSelectMaterial(m_Data, m_IsLocal);
- }
- }
- public void SetData(MaterialObl material, bool islocal)
- {
- m_Data = material;
- m_IsLocal = islocal;
- if (m_IsLocal)
- {
- CalLocaldata();
- }
- else
- {
- CalUpdateData();
- }
- }
- private void CalLocaldata()
- {
- m_aame.text = m_Data.name;
- m_Icon.sprite = ResMgr.Instance.Load<Sprite>(m_Data.icon);
- }
- private void CalUpdateData()
- {
- DownLoadMaterial data = new DownLoadMaterial();
- string filename = Path.GetFileName(m_Data.icon);
- // data.name = filename;
- data.type = "-9";
- data.downLoadPath = m_Data.icon;
- data.localLoadPath =Application.persistentDataPath + "/Material/" + filename;
- data.updataTime = m_Data.updateTime;
- if (!string.IsNullOrWhiteSpace((data.downLoadPath)))
- {
- //DownloadManager.Instance.LoadMap(data, m_Icon, (tex) =>
- //{
- // Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
- // m_Icon.sprite = sprite;
- //});
- MsgHandler.AddListener(data.downLoadPath, HandleMsg);
- DownloadResManager.Instance.DownLoad(data);
- }
- m_aame.text = string.IsNullOrWhiteSpace(m_Data.name) ? "" : m_Data.name;
- }
- private void HandleMsg(Msg msg)
- {
- if(msg.Value!=null)
- {
- Texture2D tex = new Texture2D(1, 1);
- tex.LoadImage((byte[])msg.Value);
- Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
- m_Icon.sprite = sprite;
- }
-
- }
- }
|