Files
back_cantanBuilding/Assets/Scripts/EventBingo/EventBingoNumberItemView.cs
2026-05-26 16:15:54 +08:00

53 lines
1.8 KiB
C#

using asap.core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class EventBingoNumberItemView : MonoBehaviour
{
[SerializeField] TMP_Text text_number;
[SerializeField] Image icon;
[SerializeField] Animation ani;
System.Action<EventBingoNumberItemView> _onPassThreshold;
private const string ItemRollAnimation = "baseball_rotate";
public void Init(EventBingoNumberRollerItemData data, GameObject parent, Vector2 anchoredPos, System.Action<EventBingoNumberItemView> onPassThreshold = null)
{
text_number.text = data.number.ToString();
var url = GContext.container.Resolve<EventBingoModel>().Ctx.GetItemIconByIdx(data.iconIdx);
GContext.container.Resolve<IUIService>().SetImageSprite(icon, url);
gameObject.transform.SetParent(parent.transform);
transform.localScale = Vector3.one;
gameObject.GetComponent<RectTransform>().anchoredPosition = anchoredPos;
gameObject.name = "NumberItem_" + data.number;
gameObject.SetActive(true);
_onPassThreshold = onPassThreshold;
}
public async void Move(float gap, float threshold)
{
try
{
var rect = transform as RectTransform;
var endValue = rect.anchoredPosition.x - gap;
var clip = ani.GetClip(ItemRollAnimation);
ani.Play(ItemRollAnimation);
await rect.DOAnchorPosX(endValue, clip.length).AsyncWaitForCompletion();
if (rect.anchoredPosition.x < threshold)
_onPassThreshold?.Invoke(this);
}
catch (System.Exception e)
{
Debug.LogError("[EventBingo] RollItemError: ");
Debug.LogError(e);
}
}
public float GetItemRollDuration()
{
var clip = ani.GetClip(ItemRollAnimation);
return clip.length;
}
}