SampleButton.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. #if UNITY_UGUI
  5. using UnityEngine.UI;
  6. #endif
  7. namespace Animancer.Samples
  8. {
  9. /// <summary>A simple system for dynamically creating a list of buttons based on a template.</summary>
  10. /// <remarks>
  11. /// <strong>Documentation:</strong>
  12. /// <see href="https://kybernetik.com.au/animancer/docs/samples/basics/scene-setup#buttons">
  13. /// Basic Scene Setup - Buttons</see>
  14. /// </remarks>
  15. /// https://kybernetik.com.au/animancer/api/Animancer.Samples/SampleButton
  16. [AddComponentMenu(Strings.SamplesMenuPrefix + "Sample Button")]
  17. [AnimancerHelpUrl(typeof(SampleButton))]
  18. public class SampleButton : MonoBehaviour
  19. {
  20. /************************************************************************************************************************/
  21. #if UNITY_UGUI
  22. /************************************************************************************************************************/
  23. [SerializeField] private RectTransform _Transform;
  24. [SerializeField] private Button _Button;
  25. [SerializeField] private Text _Text;
  26. [SerializeField] private float _Spacing = 10;
  27. /************************************************************************************************************************/
  28. /// <summary>Automatically gathers the serialized fields.</summary>
  29. protected virtual void OnValidate()
  30. {
  31. gameObject.GetComponentInParentOrChildren(ref _Transform);
  32. gameObject.GetComponentInParentOrChildren(ref _Button);
  33. gameObject.GetComponentInParentOrChildren(ref _Text);
  34. }
  35. /************************************************************************************************************************/
  36. /// <summary>
  37. /// Initializes this button when called with `index` 0.
  38. /// Otherwise, creates a copy of this button and initializes it instead.
  39. /// </summary>
  40. public SampleButton AddButton(
  41. int index,
  42. string text,
  43. UnityAction onClick)
  44. {
  45. SampleButton button = this;
  46. if (index != 0)
  47. {
  48. button = Instantiate(button, button._Transform.parent);
  49. Vector2 position = button._Transform.anchoredPosition;
  50. position.y -= index * (button._Transform.sizeDelta.y + button._Spacing);
  51. button._Transform.anchoredPosition = position;
  52. }
  53. button._Button.onClick.AddListener(onClick);
  54. button._Text.text = text;
  55. button.name = text;
  56. return button;
  57. }
  58. /************************************************************************************************************************/
  59. #else
  60. /************************************************************************************************************************/
  61. protected virtual void Awake()
  62. {
  63. SampleReadMe.LogMissingUnityUIModuleError(this);
  64. }
  65. /************************************************************************************************************************/
  66. public SampleButton AddButton(
  67. int index,
  68. string text,
  69. UnityAction onClick)
  70. => this;
  71. /************************************************************************************************************************/
  72. #endif
  73. /************************************************************************************************************************/
  74. }
  75. }