SampleSelectorUI.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Best.HTTP.Examples.Helpers.SelectorUI
  7. {
  8. class SampleSelectorUI : MonoBehaviour
  9. {
  10. #pragma warning disable 0649, 0169
  11. public string BaseURL = "https://besthttpwebgldemo.azurewebsites.net";
  12. [HideInInspector]
  13. public SampleBase selectedExamplePrefab;
  14. [SerializeField]
  15. private Category _categoryListItemPrefab;
  16. [SerializeField]
  17. private ExampleListItem _exampleListItemPrefab;
  18. [SerializeField]
  19. private ExampleInfo _exampleInfoPrefab;
  20. [SerializeField]
  21. private RectTransform _listRoot;
  22. [SerializeField]
  23. private RectTransform _dyncamicContentRoot;
  24. [SerializeField]
  25. private List<SampleBase> _samples = new List<SampleBase>();
  26. private ExampleListItem selectedSample;
  27. private GameObject dynamicContent;
  28. #pragma warning restore
  29. private void Start()
  30. {
  31. DisplayExamples();
  32. }
  33. private void DisplayExamples()
  34. {
  35. // Sort examples by category
  36. this._samples.Sort((a, b) => {
  37. if (a == null || b == null)
  38. return 0;
  39. int result = a.Category.CompareTo(b.Category);
  40. if (result == 0)
  41. result = a.DisplayName.CompareTo(b.DisplayName);
  42. return result;
  43. });
  44. string currentCategory = null;
  45. for (int i = 0; i < this._samples.Count; ++i)
  46. {
  47. var examplePrefab = this._samples[i];
  48. if (examplePrefab == null)
  49. continue;
  50. if (examplePrefab.BannedPlatforms.Contains(UnityEngine.Application.platform))
  51. continue;
  52. if (currentCategory != examplePrefab.Category)
  53. {
  54. var category = Instantiate<Category>(this._categoryListItemPrefab, this._listRoot, false);
  55. category.SetLabel(examplePrefab.Category);
  56. currentCategory = examplePrefab.Category;
  57. }
  58. var listItem = Instantiate<ExampleListItem>(this._exampleListItemPrefab, this._listRoot, false);
  59. listItem.Setup(this, examplePrefab);
  60. if (this.selectedExamplePrefab == null)
  61. {
  62. SelectSample(listItem);
  63. }
  64. }
  65. }
  66. public void SelectSample(ExampleListItem item)
  67. {
  68. this.selectedExamplePrefab = item.ExamplePrefab;
  69. if (this.dynamicContent != null)
  70. Destroy(this.dynamicContent);
  71. var example = Instantiate<ExampleInfo>(this._exampleInfoPrefab, this._dyncamicContentRoot, false);
  72. example.Setup(this, item.ExamplePrefab);
  73. this.dynamicContent = example.gameObject;
  74. }
  75. public void ExecuteExample(SampleBase example)
  76. {
  77. if (this.dynamicContent != null)
  78. Destroy(this.dynamicContent);
  79. this.dynamicContent = Instantiate(example, this._dyncamicContentRoot, false).gameObject;
  80. }
  81. }
  82. }