2026-06-19 UI, UI로직
This commit is contained in:
133
Assets/My project/TruthFountain/Scripts/TruthChoiceButtonUI.cs
Normal file
133
Assets/My project/TruthFountain/Scripts/TruthChoiceButtonUI.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Button))]
|
||||
public class TruthChoiceButtonUI : MonoBehaviour
|
||||
{
|
||||
[Header("UI References")]
|
||||
[SerializeField] private Button button;
|
||||
[SerializeField] private Image backgroundImage;
|
||||
[SerializeField] private TMP_Text choiceText;
|
||||
|
||||
[Header("Optional Sprites")]
|
||||
[SerializeField] private Sprite normalSprite;
|
||||
[SerializeField] private Sprite selectedSprite;
|
||||
[SerializeField] private Sprite truthSprite;
|
||||
[SerializeField] private Sprite lieSprite;
|
||||
|
||||
[Header("Fallback Colors")]
|
||||
[SerializeField] private Color normalColor = new Color(0.08f, 0.35f, 0.42f, 0.9f);
|
||||
[SerializeField] private Color selectedColor = new Color(0.2f, 0.75f, 0.85f, 1f);
|
||||
[SerializeField] private Color truthColor = new Color(0.25f, 0.85f, 0.65f, 1f);
|
||||
[SerializeField] private Color lieColor = new Color(0.85f, 0.35f, 0.35f, 1f);
|
||||
|
||||
private TruthChoiceData currentChoice;
|
||||
private TruthFountainGameManager gameManager;
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
button = GetComponent<Button>();
|
||||
backgroundImage = GetComponent<Image>();
|
||||
choiceText = GetComponentInChildren<TMP_Text>(true);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (button == null)
|
||||
{
|
||||
button = GetComponent<Button>();
|
||||
}
|
||||
|
||||
if (backgroundImage == null)
|
||||
{
|
||||
backgroundImage = GetComponent<Image>();
|
||||
}
|
||||
|
||||
if (choiceText == null)
|
||||
{
|
||||
choiceText = GetComponentInChildren<TMP_Text>(true);
|
||||
}
|
||||
|
||||
button.onClick.RemoveListener(HandleClick);
|
||||
button.onClick.AddListener(HandleClick);
|
||||
}
|
||||
|
||||
public void Setup(TruthChoiceData choiceData, TruthFountainGameManager manager)
|
||||
{
|
||||
currentChoice = choiceData;
|
||||
gameManager = manager;
|
||||
|
||||
bool hasChoice = currentChoice != null && currentChoice.IsValid();
|
||||
gameObject.SetActive(hasChoice);
|
||||
|
||||
if (!hasChoice)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (choiceText != null)
|
||||
{
|
||||
choiceText.text = currentChoice.ChoiceText;
|
||||
}
|
||||
|
||||
SetInteractable(true);
|
||||
SetVisual(normalSprite, normalColor);
|
||||
}
|
||||
|
||||
public void SetInteractable(bool value)
|
||||
{
|
||||
if (button != null)
|
||||
{
|
||||
button.interactable = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowSelectedFeedback()
|
||||
{
|
||||
SetVisual(selectedSprite, selectedColor);
|
||||
}
|
||||
|
||||
public void ShowTruthOrLieFeedback()
|
||||
{
|
||||
if (currentChoice != null && currentChoice.MarkAsTruthChoice)
|
||||
{
|
||||
SetVisual(truthSprite, truthColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetVisual(lieSprite, lieColor);
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetVisual()
|
||||
{
|
||||
SetVisual(normalSprite, normalColor);
|
||||
}
|
||||
|
||||
private void HandleClick()
|
||||
{
|
||||
if (currentChoice == null || gameManager == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ShowSelectedFeedback();
|
||||
gameManager.SelectChoice(currentChoice, this);
|
||||
}
|
||||
|
||||
private void SetVisual(Sprite sprite, Color color)
|
||||
{
|
||||
if (backgroundImage == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (sprite != null)
|
||||
{
|
||||
backgroundImage.sprite = sprite;
|
||||
}
|
||||
|
||||
backgroundImage.color = color;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user