using UnityEngine; using UnityEngine.UI; namespace UnityEngine.UI.Extensions.Examples { public class Example03ScrollViewCell : FancyScrollViewCell { [SerializeField] Animator animator; [SerializeField] Text message; [SerializeField] Image image; [SerializeField] Button button; static readonly int scrollTriggerHash = Animator.StringToHash("scroll"); void Start() { var rectTransform = transform as RectTransform; rectTransform.anchorMax = Vector2.one; rectTransform.anchorMin = Vector2.zero; rectTransform.anchoredPosition3D = Vector3.zero; button.onClick.AddListener(OnPressedCell); } /// /// Updates the content. /// /// Item data. public override void UpdateContent(Example03CellDto itemData) { message.text = itemData.Message; if (Context != null) { var isSelected = Context.SelectedIndex == DataIndex; image.color = isSelected ? new Color32(0, 255, 255, 100) : new Color32(255, 255, 255, 77); } } /// /// Updates the position. /// /// Position. public override void UpdatePosition(float position) { currentPosition = position; animator.Play(scrollTriggerHash, -1, position); animator.speed = 0; } void OnPressedCell() { if (Context != null) { Context.OnPressedCell(this); } } // GameObject が非アクティブになると Animator がリセットされてしまうため // 現在位置を保持しておいて OnEnable のタイミングで現在位置を再設定します float currentPosition = 0; void OnEnable() { UpdatePosition(currentPosition); } } }