NavigationTargetListControl.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. namespace Immersal.Samples.Navigation
  12. {
  13. [RequireComponent(typeof(RectTransform))]
  14. [RequireComponent(typeof(ScrollRect))]
  15. public class NavigationTargetListControl : MonoBehaviour
  16. {
  17. [SerializeField]
  18. private GameObject m_ButtonTemplate = null;
  19. [SerializeField]
  20. private RectTransform m_ContentParent = null;
  21. [SerializeField]
  22. int m_MaxButtonsOnScreen = 4;
  23. private List<GameObject> m_Buttons = new List<GameObject>();
  24. public void GenerateButtons()
  25. {
  26. if (m_Buttons.Count > 0)
  27. {
  28. DestroyButtons();
  29. }
  30. // loops through all navigation categories
  31. foreach (KeyValuePair<NavigationTargets.NavigationCategory, List<GameObject>> entry in NavigationTargets.NavigationTargetsDict)
  32. {
  33. // loops through all targets in each category
  34. foreach (GameObject go in entry.Value)
  35. {
  36. IsNavigationTarget isNavigationTarget = go.GetComponent<IsNavigationTarget>();
  37. string targetName = isNavigationTarget.targetName;
  38. Sprite icon = isNavigationTarget.icon;
  39. GameObject button = Instantiate(m_ButtonTemplate, m_ContentParent);
  40. m_Buttons.Add(button);
  41. button.SetActive(true);
  42. button.name = "button " + targetName;
  43. NavigationTargetListButton navigationTargetListButton = button.GetComponent<NavigationTargetListButton>();
  44. navigationTargetListButton.SetText(targetName);
  45. navigationTargetListButton.SetIcon(icon);
  46. navigationTargetListButton.SetTarget(go);
  47. }
  48. }
  49. // calculate lists RectTransform size
  50. float x = m_ButtonTemplate.GetComponent<RectTransform>().sizeDelta.x;
  51. float y = m_ButtonTemplate.GetComponent<RectTransform>().sizeDelta.y * Mathf.Min(m_Buttons.Count, m_MaxButtonsOnScreen);
  52. RectTransform rectTransform = GetComponent<RectTransform>();
  53. rectTransform.sizeDelta = new Vector2(x, y);
  54. ScrollToTop();
  55. }
  56. private void DestroyButtons()
  57. {
  58. foreach (GameObject button in m_Buttons)
  59. {
  60. Destroy(button);
  61. }
  62. m_Buttons.Clear();
  63. }
  64. private void ScrollToTop()
  65. {
  66. transform.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0, 1);
  67. }
  68. }
  69. }