ListBase.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. UIPanel itemUIPanel = itemTransform.GetComponent<UIPanel>();
  47. if (itemUIPanel != null)
  48. {
  49. Destroy(itemUIPanel);
  50. }
  51. }
  52. }