rabidus-test/Assets/Graphy - Ultimate Stats Mon.../Runtime/Audio/G_AudioText.cs

90 lines
2.3 KiB
C#
Raw Permalink Normal View History

2023-09-11 15:44:17 +03:00
/* ---------------------------------------
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
* Project: Graphy - Ultimate Stats Monitor
* Date: 15-Dec-17
* Studio: Tayx
*
* Git repo: https://github.com/Tayx94/graphy
*
* This project is released under the MIT license.
* Attribution is not required, but it is always welcomed!
* -------------------------------------*/
using UnityEngine;
using UnityEngine.UI;
using Tayx.Graphy.Utils.NumString;
namespace Tayx.Graphy.Audio
{
public class G_AudioText : MonoBehaviour
{
#region Variables -> Serialized Private
[SerializeField] private Text m_DBText = null;
#endregion
#region Variables -> Private
private GraphyManager m_graphyManager = null;
private G_AudioMonitor m_audioMonitor = null;
private int m_updateRate = 4;
private float m_deltaTimeOffset = 0;
#endregion
#region Methods -> Unity Callbacks
private void Awake()
{
Init();
}
private void Update()
{
if (m_audioMonitor.SpectrumDataAvailable)
{
if (m_deltaTimeOffset > 1f / m_updateRate)
{
m_deltaTimeOffset = 0f;
m_DBText.text = Mathf.Clamp((int)m_audioMonitor.MaxDB, -80, 0).ToStringNonAlloc();
}
else
{
m_deltaTimeOffset += Time.deltaTime;
}
}
}
#endregion
#region Methods -> Public
public void UpdateParameters()
{
m_updateRate = m_graphyManager.AudioTextUpdateRate;
}
#endregion
#region Methods -> Private
private void Init()
{
G_IntString.Init( -80, 0 ); // dB range
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
m_audioMonitor = GetComponent<G_AudioMonitor>();
UpdateParameters();
}
#endregion
}
}