저번에 만든 코인 Reward Effect에 DOTween을 적용해서 코인이 올라가는 효과를 구현해보자. 먼더 Uxml의 Label을 연결해서 text를 수정해주는 코드를 추가해준다.
using UnityEngine;
using UnityEngine.UIElements;
public class UIScreenController : MonoBehaviour
{
[Header("UI Elements")]
[Tooltip("UI Document")]
[SerializeField] UIDocument m_Document;
[SerializeField] Button m_CoinButton;
[SerializeField] Button m_ResetButton;
[SerializeField] Label m_CoinLabel;
VisualElement root;
private const string coinButtonID = "Button--Coin";
private const string coinResetID = "Button--Reset";
private const string coinLabelID = "Label--Coin";
private const string coinSequenceId = "CoinSeq";
void OnEnable() {
root = m_Document.rootVisualElement;
m_CoinButton = root.Q<Button>(coinButtonID);
m_ResetButton = root.Q<Button>(coinResetID);
m_CoinLabel = root.Q<Label>(coinLabelID);
m_CoinButton.RegisterCallback<ClickEvent>(OnClickCoinButton);
m_ResetButton.RegisterCallback<ClickEvent>(OnClickResetButton);
CoinEvents.CoinUpdate += OnUpdateCoinText;
}
void OnDisable() {
m_CoinButton.UnregisterCallback<ClickEvent>(OnClickCoinButton);
m_ResetButton.UnregisterCallback<ClickEvent>(OnClickResetButton);
CoinEvents.CoinUpdate -= OnUpdateCoinText;
}
void OnClickCoinButton(ClickEvent evt) {
Vector2 clickPos = new Vector2(evt.position.x, evt.position.y);
Vector2 screenPos = clickPos.GetScreenCoordinate(root);
Debug.Log($"[UIScreenController] OnClickCoinButton : clickPos ({clickPos.x}, {clickPos.y})");
Debug.Log($"[UIScreenController] OnClickCoinButton : screenPos ({screenPos.x}, {screenPos.y})");
CoinEvents.CoinButtonClick?.Invoke(screenPos);
GameDataManager.AddCoin();
}
void OnUpdateCoinText(int CoinValue) {
m_CoinLabel.text = CoinValue.ToString();
}
void OnClickResetButton(ClickEvent evt) {
return;
}
}
위에서 보다 싶이 CoinEvents에 Coin Update 액션을 추가해놨다.
using System;
using UnityEngine;
public static class CoinEvents
{
// Click the Button on UI Document
public static Action<Vector2> CoinButtonClick;
public static Action<Vector2> ResetButtonClick;
// Update Coin Text
public static Action<int> CoinUpdate;
}
Coin의 데이터를 가지고 Coin의 값을 추가해주는 GameDataManager 스크립트를 하나 추가작성해준다.
using System;
using UnityEngine;
public static class CoinEvents
{
// Click the Button on UI Document
public static Action<Vector2> CoinButtonClick;
public static Action<Vector2> ResetButtonClick;
// Update Coin Text
public static Action<int> CoinUpdate;
}
누를 때마다 값이 잘 늘어난다.
DOTween을 지원하는지를 아직 잘 몰라서 추후에 확인해서 한 번 바꿔보자.