public void SelectElement(UICarouselElement element) { if (!element) { return; } EventSystem.current.SetSelectedGameObject(element.gameObject); OnElementSelect(element); }
public void SelectElement(int index) { if (IsEmpty) { return; } UICarouselElement element = m_Elements[Mathf.Clamp(index, 0, m_Elements.Count - 1)]; SelectElement(element); }
protected virtual void ScrollToElement(UICarouselElement element) { /// Where the element is inside the scroll rect Vector3 elementCenterPos = GetWorldPointInWidget(m_RectTransform, GetWidgetWorldPoint(element.RectTransform)); /// Where the element should be inside the scroll rect Vector3 targetPos = GetWorldPointInWidget(m_RectTransform, GetWidgetWorldPoint(scrollMask)); Vector3 difference = targetPos - elementCenterPos; if (!horizontal) { difference.x = 0f; } if (!vertical) { difference.y = 0f; } Vector2 normalizedDifference; Rect rect = content.rect, rect1 = m_RectTransform.rect; normalizedDifference.x = difference.x / (rect.size.x - rect1.size.x); normalizedDifference.y = difference.y / (rect.size.y - rect1.size.y); Vector2 newNormalizedPosition = normalizedPosition - normalizedDifference; if (movementType != MovementType.Unrestricted) { newNormalizedPosition.x = Mathf.Clamp01(newNormalizedPosition.x); newNormalizedPosition.y = Mathf.Clamp01(newNormalizedPosition.y); } //normalizedPosition = newNormalizedPosition; DG.Tweening.DOTween.To(() => normalizedPosition, (x) => normalizedPosition = x, newNormalizedPosition, 1f / scrollSpeed).SetEase(scrollEase); }
public virtual UICarouselElement CreateElement(int id, bool selected) { GameObject elementGO = Instantiate(elementPrefab, content); var element = elementGO.GetComponent <UICarouselElement>(); element.ID = id; element.onSelectElement += OnElementSelect; element.onDeselectElement += OnElementDeselect; m_Elements.Add(element); if (selected) { element.PlaySelectAnim(); SelectedElement = element; } else { element.PlayDeselectAnim(); } return(element); }
protected virtual void OnElementDeselect(UICarouselElement element) => onDeselectElement?.Invoke(element);
protected virtual void OnElementSelect(UICarouselElement element) { SelectedElement = element; ScrollToElement(element); onSelectElement?.Invoke(element); }