ImageLoadComponent.cs 981 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [RequireComponent(typeof(Image))]
  6. public class ImageLoadComponent : MonoBehaviour {
  7. public string iconName;
  8. private Image loadImage;
  9. // Use this for initialization
  10. void Awake () {
  11. loadImage = LoadImage();
  12. }
  13. private void OnEnable()
  14. {
  15. if (loadImage == null || loadImage.sprite == null)
  16. {
  17. loadImage = LoadImage();
  18. }
  19. }
  20. public Image LoadImage()
  21. {
  22. Image image = GetComponent<Image>();
  23. if (image)
  24. {
  25. if (!string.IsNullOrEmpty(iconName))
  26. UGUITool.SetImageSprite(image, iconName);
  27. }
  28. else
  29. {
  30. Debug.LogError(" Dont have Image!!!");
  31. }
  32. return image;
  33. }
  34. private void OnDestroy()
  35. {
  36. if (!string.IsNullOrEmpty(iconName))
  37. AssetsPoolManager.DestroyByPool(iconName);
  38. }
  39. }