Files
MI-38/Heli_with_panels/Assets/Scripts/Panel2D/0115/Variometer.cs
2022-07-04 13:15:35 +03:00

119 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Variometer : MonoBehaviour//frame2 0115
{
public GameObject arrow;
[HideInInspector]
public string textValue_ = "";
private string defaultValue_ = "0";
//private Text text_;
private TextMeshPro text_;
private int k=0; //текущее значение счетчика в float
private int max = 30;
private int min = -30;
bool reverse = false; //направление
float waitTime = 0.2f;//время для корутины
bool point = false;//нажатие на счетчик
private Coroutine coroutine;
void Start()
{
//text_ = this.gameObject.GetComponent<Text>();
//text_.text = (textValue_ == "") ? defaultValue_ : textValue_;
text_ = this.GetComponent<TextMeshPro>();
var txt = (textValue_ == "") ? defaultValue_ : textValue_;
text_.SetText(txt);
}
private void VarText()
{
if (k >= 0)
{
if (arrow.gameObject.GetComponent<ButtonCheckable3>() != null)
{
arrow.gameObject.GetComponent<ButtonCheckable3>().EnableSprite(0);
}
}
else
{
if (arrow.gameObject.GetComponent<ButtonCheckable3>() != null)
{
arrow.gameObject.GetComponent<ButtonCheckable3>().EnableSprite(1);
}
}
textValue_ = k.ToString();
if (k < 0)
{
//text_.text = textValue_.Substring(1, textValue_.Length - 1);
var txt = textValue_.Substring(1, textValue_.Length - 1);
text_.SetText(txt);
}
else
{
//text_.text = textValue_;
text_.SetText(textValue_);
}
}
private void OnMouseDown()//нажатие на счетчик
{
point = !point;
if (point)
{
Rever();
if (reverse)
{
k--;
coroutine = StartCoroutine(VarNumber());
}
else
{
k++;
coroutine = StartCoroutine(VarNumber());
}
}
else
{
if (coroutine != null) StopCoroutine(coroutine);
}
}
private void Rever()
{
if (k <= min)
{
reverse = false;
}
else if (k >= max)
{
reverse = true;
}
}
IEnumerator VarNumber()
{
while (point)
{
VarText();
if (reverse)
{
k--;
}
else
{
k++;
}
Rever();
yield return new WaitForSeconds(waitTime);
}
}
}