com.unity.uiextensions.nosa.../Examples/FancyScrollView/02_CellEventHandling/Example02ScrollViewCell.cs

69 lines
2.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
namespace UnityEngine.UI.Extensions.Examples
{
public class Example02ScrollViewCell : FancyScrollViewCell<Example02CellDto, Example02ScrollViewContext>
{
[SerializeField]
Animator animator = null;
[SerializeField]
Text message = null;
[SerializeField]
Image image = null;
[SerializeField]
Button button = null;
static readonly int scrollTriggerHash = Animator.StringToHash("scroll");
void Start()
{
button.onClick.AddListener(OnPressedCell);
}
/// <summary>
/// Updates the content.
/// </summary>
/// <param name="itemData">Item data.</param>
public override void UpdateContent(Example02CellDto 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);
}
}
/// <summary>
/// Updates the position.
/// </summary>
/// <param name="position">Position.</param>
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);
}
}
}