83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class VitrinaBox : MonoBehaviour
|
|
{
|
|
[SerializeField] TextMeshProUGUI numberLabel;
|
|
[SerializeField] RectTransform ImageTransform;
|
|
public int Number { get; private set; }
|
|
public RectTransform RectTransform { get; private set; }
|
|
|
|
bool animated;
|
|
public bool shake;
|
|
Coroutine pingpongCoroutine;
|
|
|
|
[Header("Настройки анимации")]
|
|
[SerializeField] private float amplitude = 0.1f;
|
|
[SerializeField] private float frequency = 7;
|
|
private void OnEnable()
|
|
{
|
|
RectTransform = GetComponent<RectTransform>();
|
|
StartCoroutine(Shake());
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
StopAllCoroutines();
|
|
}
|
|
public void SetNumber(int number)
|
|
{
|
|
Number = number;
|
|
numberLabel.text = number.ToString();
|
|
}
|
|
|
|
public void SetEnabled(bool enabled)
|
|
{
|
|
if (enabled)
|
|
{
|
|
if (pingpongCoroutine != null) StopCoroutine(pingpongCoroutine);
|
|
animated = true;
|
|
pingpongCoroutine = StartCoroutine(Animation());
|
|
}
|
|
else
|
|
{
|
|
animated = false;
|
|
}
|
|
}
|
|
|
|
IEnumerator Animation()
|
|
{
|
|
while (animated)
|
|
{
|
|
ImageTransform.localScale = Vector3.one * (1 + amplitude * Mathf.Sin(Time.time * frequency));
|
|
yield return null;
|
|
}
|
|
|
|
//Возврат в исходную
|
|
while (ImageTransform.localScale != Vector3.one)
|
|
{
|
|
ImageTransform.localScale = Vector3.MoveTowards(ImageTransform.localScale, Vector3.one, Time.deltaTime);
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
IEnumerator Shake()
|
|
{
|
|
while (true)
|
|
{
|
|
if (shake)
|
|
{
|
|
float noise = Mathf.PerlinNoise(Time.time * 8, 0f);
|
|
float angle = noise * 2 - 1;
|
|
ImageTransform.rotation = Quaternion.Euler(0, 0, angle * 15);
|
|
}
|
|
else
|
|
{
|
|
ImageTransform.rotation = Quaternion.Euler(0, 0, 0);
|
|
}
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|