87 lines
2.2 KiB
C#
87 lines
2.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Framework;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class UIMedalCountNode : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private uint medalItemId;
|
|
[SerializeField]
|
|
private TMP_Text numText;
|
|
[SerializeField]
|
|
private uint medalItemCount;
|
|
[SerializeField]
|
|
private CanvasGroup canvasGroup;
|
|
|
|
private bool isShow=false;
|
|
private Func<uint,uint> GetMedalCountFunc;
|
|
|
|
private void EnsureUIReferences()
|
|
{
|
|
if (numText == null)
|
|
numText = GetComponentInChildren<TMP_Text>(true);
|
|
if (canvasGroup == null)
|
|
canvasGroup = GetComponentInChildren<CanvasGroup>(true);
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
EnsureUIReferences();
|
|
if (canvasGroup != null)
|
|
canvasGroup.alpha = 0;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
EventManager.Instance.Register<uint>(EventManager.EventName.ItemChange,Refresh);
|
|
}
|
|
|
|
private void Refresh(uint id)
|
|
{
|
|
if(id!=medalItemId||!isShow) return;
|
|
|
|
EnsureUIReferences();
|
|
medalItemCount = GetMedalCountFunc?.Invoke(medalItemId) ?? 0;
|
|
if (canvasGroup != null)
|
|
canvasGroup.alpha = medalItemCount > 0 ? 1 : 0;
|
|
if (numText != null)
|
|
numText.text = medalItemCount.ToString();
|
|
}
|
|
|
|
public void SetNode(uint id,Func<uint,uint> getMedalCountFunc,int repeat=0)
|
|
{
|
|
if (repeat == 0)
|
|
{
|
|
EnsureUIReferences();
|
|
if (canvasGroup != null)
|
|
canvasGroup.alpha = 0;
|
|
isShow = false;
|
|
return;
|
|
}
|
|
|
|
isShow = true;
|
|
medalItemId = id;
|
|
GetMedalCountFunc = getMedalCountFunc;
|
|
medalItemCount = GetMedalCountFunc?.Invoke(medalItemId) ?? 0;
|
|
|
|
gameObject.SetActive(true);
|
|
gameObject.transform.parent.gameObject.SetActive(true);
|
|
EnsureUIReferences();
|
|
Refresh(medalItemId);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
EventManager.Instance.Unregister<uint>(EventManager.EventName.ItemChange,Refresh);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
EventManager.Instance.Unregister<uint>(EventManager.EventName.ItemChange,Refresh);
|
|
GetMedalCountFunc = null;
|
|
}
|
|
}
|