83 lines
1.6 KiB
C#
83 lines
1.6 KiB
C#
using PrizeSystem;
|
|
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class QrPanel : MonoBehaviour
|
|
{
|
|
private bool initialized = false;
|
|
public event Action OnComplete;
|
|
|
|
[SerializeField] private Image QrImage;
|
|
[SerializeField] private TextMeshProUGUI Label;
|
|
|
|
[SerializeField] private float timer = 0;
|
|
private float timeToAct = 3;
|
|
|
|
private void OnEnable()
|
|
{
|
|
initialized = false;
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
initialized = false;
|
|
Clear();
|
|
}
|
|
|
|
public void Initialize(Prize prize)
|
|
{
|
|
if (initialized) return;
|
|
|
|
|
|
|
|
|
|
if (prize is QrPrize qrPrize)
|
|
{
|
|
var tex = QrMaker.MakePrizeTexture(qrPrize);
|
|
tex.filterMode = FilterMode.Point;
|
|
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
|
QrImage.sprite = sprite;
|
|
Label.text = qrPrize.PrizeText;
|
|
}
|
|
else
|
|
{
|
|
OnComplete?.Invoke();
|
|
}
|
|
initialized = true;
|
|
}
|
|
private void Clear()
|
|
{
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!initialized) return;
|
|
var input = CollectInputs();
|
|
|
|
if (input)
|
|
{
|
|
timer = Mathf.Clamp(timer + Time.deltaTime, 0, timeToAct);
|
|
}
|
|
else
|
|
{
|
|
timer = 0;
|
|
}
|
|
|
|
|
|
if (timer >= timeToAct)
|
|
{
|
|
OnComplete?.Invoke();
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
bool CollectInputs()
|
|
{
|
|
bool action = In.GetAction(JoystickSide.Both, InteractMode.Continious);
|
|
return action;
|
|
}
|
|
|
|
}
|