SampleSelectorUI.cs 3.1 KB

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