ListBase.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. public abstract class ListBase : MonoBehaviour
  4. {
  5. public Transform itemsAnchor;
  6. public GameObject listEmptyLabel;
  7. const float VERTICAL_SPACE_PER_ITEM = 150;
  8. const float OFFSET_Y = 100;
  9. #if UNITY_EDITOR
  10. void OnEnable()
  11. {
  12. CreateRandomRequests();
  13. }
  14. void CreateRandomRequests()
  15. {
  16. listEmptyLabel.SetActive(false);
  17. for (int i = 0; i < 5; i++)
  18. {
  19. PlaceItem(CreateRandomItemForTestingInEditor(i), i);
  20. }
  21. }
  22. #else
  23. void OnEnable()
  24. {
  25. if (HasItems())
  26. {
  27. listEmptyLabel.SetActive(false);
  28. CreateItems();
  29. } else
  30. {
  31. listEmptyLabel.SetActive(true);
  32. }
  33. }
  34. #endif
  35. protected abstract bool HasItems();
  36. protected abstract void CreateItems();
  37. protected abstract Transform CreateRandomItemForTestingInEditor(int index);
  38. protected void PlaceItem(Transform itemTransform, int row)
  39. {
  40. Vector3 originalScale = itemTransform.localScale;
  41. itemTransform.parent = itemsAnchor;
  42. itemTransform.localScale = originalScale;
  43. itemTransform.localPosition = new Vector3(0, (row * -VERTICAL_SPACE_PER_ITEM) + OFFSET_Y);
  44. //HACK:For some unknown reason, NGUI is creating a UIPanel on the second viewing on mobiles.
  45. //Removing it, otherwise no proper clipping is done by the parent UIPanel.
  46. }
  47. }