102 lines
2.8 KiB
C#
102 lines
2.8 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Events;
|
|
|
|
public class taskPrefabController : MonoBehaviour
|
|
{
|
|
[Header("basic infos")]
|
|
public TextMeshProUGUI taskNumber;
|
|
public Text taskText;
|
|
public Image taskProgress_fillAmount_img;
|
|
|
|
[Header("the rewards")]
|
|
public Button rewardButton;
|
|
public Image rewardImg;
|
|
public Text rewardAmmount;
|
|
|
|
[Header("materials")]
|
|
[SerializeField] private Material unavailableRewardMaterial;
|
|
|
|
public void Setup(DailyTaskViewData taskView, int displayIndex, Sprite rewardSprite, UnityAction onRewardClicked, Material overrideUnavailableRewardMaterial = null)
|
|
{
|
|
if (taskView == null || taskView.definition == null || taskView.runtimeEntry == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (overrideUnavailableRewardMaterial != null)
|
|
{
|
|
unavailableRewardMaterial = overrideUnavailableRewardMaterial;
|
|
}
|
|
|
|
if (taskNumber != null)
|
|
{
|
|
taskNumber.text = displayIndex.ToString("00");
|
|
}
|
|
|
|
if (taskText != null)
|
|
{
|
|
taskText.text = taskView.definition.description;
|
|
}
|
|
|
|
if (taskProgress_fillAmount_img != null)
|
|
{
|
|
float targetValue = Mathf.Max(0.0001f, taskView.definition.targetValue);
|
|
float clampedProgress = Mathf.Clamp(taskView.runtimeEntry.progress, 0f, targetValue);
|
|
taskProgress_fillAmount_img.fillAmount = clampedProgress / targetValue;
|
|
}
|
|
|
|
if (rewardImg != null)
|
|
{
|
|
rewardImg.sprite = rewardSprite;
|
|
rewardImg.enabled = rewardSprite != null;
|
|
}
|
|
|
|
if (rewardAmmount != null)
|
|
{
|
|
rewardAmmount.text = taskView.definition.rewardAmount.ToString();
|
|
}
|
|
|
|
if (rewardButton == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
rewardButton.onClick.RemoveAllListeners();
|
|
if (onRewardClicked != null)
|
|
{
|
|
rewardButton.onClick.AddListener(onRewardClicked);
|
|
}
|
|
|
|
bool canClaim = taskView.runtimeEntry.isCompleted && !taskView.runtimeEntry.isClaimed;
|
|
rewardButton.interactable = canClaim;
|
|
|
|
ApplyRewardAvailabilityMaterial(taskView.runtimeEntry.isCompleted);
|
|
}
|
|
|
|
private void ApplyRewardAvailabilityMaterial(bool isCompleted)
|
|
{
|
|
Material targetMaterial = isCompleted ? null : unavailableRewardMaterial;
|
|
|
|
if (rewardButton != null)
|
|
{
|
|
Graphic buttonGraphic = rewardButton.targetGraphic;
|
|
if (buttonGraphic != null)
|
|
{
|
|
buttonGraphic.material = targetMaterial;
|
|
}
|
|
}
|
|
|
|
if (rewardImg != null)
|
|
{
|
|
rewardImg.material = targetMaterial;
|
|
}
|
|
|
|
if (rewardAmmount != null)
|
|
{
|
|
rewardAmmount.material = targetMaterial;
|
|
}
|
|
}
|
|
}
|