- Home /
How to record the time of pressing the UI buttons, and repeat the press.
How to record the time of pressing the UI buttons, and repeat the press. For example: clicked on the first button (the object changed color), after 2 seconds, clicked on the second (the object increased), after a second clicked on the third (the object became as before). After he pressed a special button and all the actions were reproduced. Perhaps you know examples or plugins.
You can e.g. add a Dictionary<float,int> clicks
which records the click times (Time.time) as well as the button index (int). Alongside, a float startTime
(initialized on every round via Time.time), which you later keep comparing against in your Update()
. You can also have bool record
which is flipped based on the state. When recording (record is true), you push new times into the list using clicks.Add(Time.time, buttonIndex). When not recording, you play it all back from the list.
Answer by logicandchaos · Feb 04, 2020 at 04:27 PM
You just need variables to keep track of when you press buttons. I would probably put it all in a class. Like have an action class and then have a float timePressed in it. Then you can keep track of when every action was executed and you can make a recording of it by putting it in a list. Then you execute it based on your timePressed value.
Answer by varan941 · Feb 10, 2020 at 10:59 PM
I did. Algorithm: 1.add the desired button to the script 1. add her OnClick event with the desired method from this script 3.create coroutine in the script if it is not 2. call it a separate method by pressing
using UnityEngine;
using UnityEngine.UI;
public class Click_tracking : MonoBehaviour
{
//массивы для записи времени нажатий каждой кнопки
public float[] _time; public float[] _time1; public float[] _time2; public float[] _time3; public float[] _time4; public float[] _time5; public float[] _time6; public float[] _time7; public float[] _time8; public float[] _time9; public float[] _time10;
public int[] _counts; //счётчики количества нажатий
public Button[] _button;
bool _cheakTimer = false;
public float _timer; //таймер
private void Update()
{
_timer += Time.deltaTime;
}
public void NewMethod() // методы для фиксации времени нажатий
{
if (!_cheakTimer)
{
_time[_counts[0]] = _timer;
_counts[0] += 1;
}
Debug.Log(_button[0]);
}
public void NewMethod1()
{
if (!_cheakTimer)
{
_time1[_counts[1]] = _timer;
_counts[1] += 1;
}
}
public void Reproduce() // метод для запуска корутин
{
_cheakTimer = true;
_timer = 0.0f;
StartCoroutine("Zero");
StartCoroutine("First");
StartCoroutine("Second");
Debug.Log("Старт корутины");
}
public IEnumerator Zero() // корутины для вызова нажатий
{
for (int i = 0; i < _counts[0]; i++)
{
if (i == 0)
{
yield return new WaitForSeconds(_time[i]);
_button[0].onClick.Invoke();
Debug.Log("Есть первое пробитие");
}
else
{
yield return new WaitForSeconds(_time[i] - _time[i - 1]);
_button[0].onClick.Invoke();
Debug.Log("Есть второе пробитие");
}
}
}
public IEnumerator First()
{
for (int i = 0; i < _counts[1]; i++)
{
if (i == 0)
{
yield return new WaitForSeconds(_time1[i]);
_button[1].onClick.Invoke();
Debug.Log("Есть первое пробитие");
}
else
{
yield return new WaitForSeconds(_time1[i] - _time1[i - 1]);
_button[1].onClick.Invoke();
Debug.Log("Есть второе пробитие");
}
}
}
public IEnumerator Second()
{
for (int i = 0; i < _counts[2]; i++)
{
if (i == 0)
{
yield return new WaitForSeconds(_time2[i]);
_button[2].onClick.Invoke();
Debug.Log("Есть первое пробитие");
}
else
{
yield return new WaitForSeconds(_time2[i] - _time2[i - 1]);
_button[2].onClick.Invoke();
Debug.Log("Есть второе пробитие");
}
}
}
public IEnumerator Third()
{
for (int i = 0; i < _counts[3]; i++)
{
if (i == 0)
{
yield return new WaitForSeconds(_time3[i]);
_button[3].onClick.Invoke();
Debug.Log("Есть первое пробитие");
}
else
{
yield return new WaitForSeconds(_time3[i] - _time3[i - 1]);
_button[3].onClick.Invoke();
Debug.Log("Есть второе пробитие");
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612222720im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2017 build ui turning purple 1 Answer
Dropdown List covering up mouse cursor 1 Answer
How to change the height of a button according to the height of the text in it? 2 Answers
How to change the shape and opacity of buttons? 1 Answer
Color change of GUI button in custom property drawer is unresponsive 1 Answer