#region Includes using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; #endregion /// /// This class represents a single dot indicator used for navigation in a paginated view. /// It provides properties for its active state and index, and events for state changes and presses. /// public class PageDot : MonoBehaviour { #region Variables [Header("Configuration")] /// /// Determines whether it should change the Image component color on state changes /// [Tooltip("Determines whether it should change the Image component color on state changes")] [SerializeField] private bool _useImageComponent; /// /// Specifies the default color used when the page dot is unselected. /// [Tooltip("Specifies the default color used when the page dot is unselected")] [SerializeField] private Color _defaultColor; /// /// Specifies the default color used when the page dot is selected. /// [Tooltip("Specifies the default color used when the page dot is selected")] [SerializeField] private Color _selectedColor; private Vector2 _defaultSize = new Vector2(20, 20); private Vector2 _selectedSize = new Vector2(40, 40); [Header("Events")] /// /// UnityEvent with a boolean parameter that is invoked when the active state of the dot changes. /// The parameter is True if the dot becomes active, False if it becomes inactive. /// [Tooltip("Invoked when the active state of the dot changes: True if active, False if inactive")] public UnityEvent OnActiveStateChanged; /// /// UnityEvent with an integer parameter that is invoked when the dot is pressed. /// The parameter represents the index of the pressed dot. /// [Tooltip("Invoked when the dot is pressed with it's index")] public UnityEvent OnPressed; /// /// Gets the active state of the page dot. /// public bool IsActive { get; private set; } /// /// Gets or sets the index of the page dot within the paginated view. /// public int Index { get; set; } private Image _image; #endregion private void Awake() { if (_useImageComponent && !TryGetComponent(out _image)) { Debug.LogError("No Image Component found"); } } private void Start() { // HACK: Ideally the dot shouldn't change it's state. // But the second dot was always active and I don't know why. // So I'm forcing the dot to update it's state on Start. ChangeActiveState(IsActive); } /// /// Changes the active state of the page dot and invokes the OnActiveStateChanged event. /// /// True to set the dot active, False to set it inactive. public virtual void ChangeActiveState(bool active) { IsActive = active; if (_useImageComponent && _image != null) { _image.color = active ? _selectedColor : _defaultColor; _image.rectTransform.sizeDelta = active ? _selectedSize : _defaultSize; } OnActiveStateChanged?.Invoke(active); } /// /// Invokes the OnPressed event with the dot's index when the dot is pressed. /// public void Press() { OnPressed?.Invoke(Index); } }