特效新内容。修复gameplay致命bug和分数保存bug
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@martinTayx)
|
||||
* 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 Tayx.Graphy.Graph;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
public class G_AudioGraph : G_Graph
|
||||
{
|
||||
#region Variables -> Serialized Private
|
||||
|
||||
[SerializeField] private Image m_imageGraph = null;
|
||||
[SerializeField] private Image m_imageGraphHighestValues = null;
|
||||
|
||||
[SerializeField] private Shader ShaderFull = null;
|
||||
[SerializeField] private Shader ShaderLight = null;
|
||||
|
||||
[SerializeField] private bool m_isInitialized = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Private
|
||||
|
||||
private GraphyManager m_graphyManager = null;
|
||||
|
||||
private G_AudioMonitor m_audioMonitor = null;
|
||||
|
||||
private int m_resolution = 40;
|
||||
|
||||
private G_GraphShader m_shaderGraph = null;
|
||||
private G_GraphShader m_shaderGraphHighestValues = null;
|
||||
|
||||
private float[] m_graphArray;
|
||||
private float[] m_graphArrayHighestValue;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
/* ----- NOTE: ----------------------------
|
||||
* We used to Init() here regardless of
|
||||
* whether this module was enabled.
|
||||
* The reason we don't Init() here
|
||||
* anymore is that some users are on
|
||||
* platforms that do not support the arrays
|
||||
* in the Shaders.
|
||||
*
|
||||
* See: https://github.com/Tayx94/graphy/issues/17
|
||||
*
|
||||
* Even though we don't Init() competely
|
||||
* here anymore, we still need
|
||||
* m_audioMonitor for in Update()
|
||||
* --------------------------------------*/
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if( m_audioMonitor.SpectrumDataAvailable )
|
||||
{
|
||||
UpdateGraph();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
if( m_shaderGraph == null )
|
||||
{
|
||||
// While Graphy is disabled (e.g. by default via Ctrl+H) and while in Editor after a Hot-Swap,
|
||||
// the OnApplicationFocus calls this while m_shaderGraph == null, throwing a NullReferenceException
|
||||
return;
|
||||
}
|
||||
|
||||
switch( m_graphyManager.GraphyMode )
|
||||
{
|
||||
case GraphyManager.Mode.FULL:
|
||||
m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull;
|
||||
m_shaderGraph.Image.material = new Material( ShaderFull );
|
||||
|
||||
m_shaderGraphHighestValues.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull;
|
||||
m_shaderGraphHighestValues.Image.material = new Material( ShaderFull );
|
||||
break;
|
||||
|
||||
case GraphyManager.Mode.LIGHT:
|
||||
m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeLight;
|
||||
m_shaderGraph.Image.material = new Material( ShaderLight );
|
||||
|
||||
m_shaderGraphHighestValues.ArrayMaxSize = G_GraphShader.ArrayMaxSizeLight;
|
||||
m_shaderGraphHighestValues.Image.material = new Material( ShaderLight );
|
||||
break;
|
||||
}
|
||||
|
||||
m_shaderGraph.InitializeShader();
|
||||
m_shaderGraphHighestValues.InitializeShader();
|
||||
|
||||
m_resolution = m_graphyManager.AudioGraphResolution;
|
||||
|
||||
CreatePoints();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Protected Override
|
||||
|
||||
protected override void UpdateGraph()
|
||||
{
|
||||
// Since we no longer initialize by default OnEnable(),
|
||||
// we need to check here, and Init() if needed
|
||||
if( !m_isInitialized )
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
int incrementPerIteration = Mathf.FloorToInt( m_audioMonitor.Spectrum.Length / (float) m_resolution );
|
||||
|
||||
// Current values -------------------------
|
||||
|
||||
for( int i = 0; i <= m_resolution - 1; i++ )
|
||||
{
|
||||
float currentValue = 0;
|
||||
|
||||
for( int j = 0; j < incrementPerIteration; j++ )
|
||||
{
|
||||
currentValue += m_audioMonitor.Spectrum[ i * incrementPerIteration + j ];
|
||||
}
|
||||
|
||||
// Uses 3 values for each bar to accomplish that look
|
||||
|
||||
if( (i + 1) % 3 == 0 && i > 1 )
|
||||
{
|
||||
float value =
|
||||
(
|
||||
m_audioMonitor.dBNormalized( m_audioMonitor.lin2dB( currentValue / incrementPerIteration ) )
|
||||
+ m_graphArray[ i - 1 ]
|
||||
+ m_graphArray[ i - 2 ]
|
||||
) / 3;
|
||||
|
||||
m_graphArray[ i ] = value;
|
||||
m_graphArray[ i - 1 ] = value;
|
||||
m_graphArray[ i - 2 ] =
|
||||
-1; // Always set the third one to -1 to leave gaps in the graph and improve readability
|
||||
}
|
||||
else
|
||||
{
|
||||
m_graphArray[ i ] =
|
||||
m_audioMonitor.dBNormalized( m_audioMonitor.lin2dB( currentValue / incrementPerIteration ) );
|
||||
}
|
||||
}
|
||||
|
||||
for( int i = 0; i <= m_resolution - 1; i++ )
|
||||
{
|
||||
m_shaderGraph.ShaderArrayValues[ i ] = m_graphArray[ i ];
|
||||
}
|
||||
|
||||
m_shaderGraph.UpdatePoints();
|
||||
|
||||
|
||||
// Highest values -------------------------
|
||||
|
||||
for( int i = 0; i <= m_resolution - 1; i++ )
|
||||
{
|
||||
float currentValue = 0;
|
||||
|
||||
for( int j = 0; j < incrementPerIteration; j++ )
|
||||
{
|
||||
currentValue += m_audioMonitor.SpectrumHighestValues[ i * incrementPerIteration + j ];
|
||||
}
|
||||
|
||||
// Uses 3 values for each bar to accomplish that look
|
||||
|
||||
if( (i + 1) % 3 == 0 && i > 1 )
|
||||
{
|
||||
float value =
|
||||
(
|
||||
m_audioMonitor.dBNormalized( m_audioMonitor.lin2dB( currentValue / incrementPerIteration ) )
|
||||
+ m_graphArrayHighestValue[ i - 1 ]
|
||||
+ m_graphArrayHighestValue[ i - 2 ]
|
||||
) / 3;
|
||||
|
||||
m_graphArrayHighestValue[ i ] = value;
|
||||
m_graphArrayHighestValue[ i - 1 ] = value;
|
||||
m_graphArrayHighestValue[ i - 2 ] =
|
||||
-1; // Always set the third one to -1 to leave gaps in the graph and improve readability
|
||||
}
|
||||
else
|
||||
{
|
||||
m_graphArrayHighestValue[ i ] =
|
||||
m_audioMonitor.dBNormalized( m_audioMonitor.lin2dB( currentValue / incrementPerIteration ) );
|
||||
}
|
||||
}
|
||||
|
||||
for( int i = 0; i <= m_resolution - 1; i++ )
|
||||
{
|
||||
m_shaderGraphHighestValues.ShaderArrayValues[ i ] = m_graphArrayHighestValue[ i ];
|
||||
}
|
||||
|
||||
m_shaderGraphHighestValues.UpdatePoints();
|
||||
}
|
||||
|
||||
protected override void CreatePoints()
|
||||
{
|
||||
// Init Arrays
|
||||
if( m_shaderGraph.ShaderArrayValues == null || m_shaderGraph.ShaderArrayValues.Length != m_resolution )
|
||||
{
|
||||
m_graphArray = new float[m_resolution];
|
||||
m_graphArrayHighestValue = new float[m_resolution];
|
||||
m_shaderGraph.ShaderArrayValues = new float[m_resolution];
|
||||
m_shaderGraphHighestValues.ShaderArrayValues = new float[m_resolution];
|
||||
}
|
||||
|
||||
for( int i = 0; i < m_resolution; i++ )
|
||||
{
|
||||
m_shaderGraph.ShaderArrayValues[ i ] = 0;
|
||||
m_shaderGraphHighestValues.ShaderArrayValues[ i ] = 0;
|
||||
}
|
||||
|
||||
// Color
|
||||
m_shaderGraph.GoodColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.CautionColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.CriticalColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.UpdateColors();
|
||||
|
||||
m_shaderGraphHighestValues.GoodColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.CautionColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.CriticalColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.UpdateColors();
|
||||
|
||||
// Threshold
|
||||
m_shaderGraph.GoodThreshold = 0;
|
||||
m_shaderGraph.CautionThreshold = 0;
|
||||
m_shaderGraph.UpdateThresholds();
|
||||
|
||||
m_shaderGraphHighestValues.GoodThreshold = 0;
|
||||
m_shaderGraphHighestValues.CautionThreshold = 0;
|
||||
m_shaderGraphHighestValues.UpdateThresholds();
|
||||
|
||||
// Update Array
|
||||
m_shaderGraph.UpdateArrayValuesLength();
|
||||
m_shaderGraphHighestValues.UpdateArrayValuesLength();
|
||||
|
||||
// Average
|
||||
m_shaderGraph.Average = 0;
|
||||
m_shaderGraph.UpdateAverage();
|
||||
|
||||
m_shaderGraphHighestValues.Average = 0;
|
||||
m_shaderGraphHighestValues.UpdateAverage();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
|
||||
m_shaderGraph = new G_GraphShader
|
||||
{
|
||||
Image = m_imageGraph
|
||||
};
|
||||
|
||||
m_shaderGraphHighestValues = new G_GraphShader
|
||||
{
|
||||
Image = m_imageGraphHighestValues
|
||||
};
|
||||
|
||||
UpdateParameters();
|
||||
|
||||
m_isInitialized = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2d6ca19dafe21b4b983441274e7f12a
|
||||
timeCreated: 1513169449
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 105778
|
||||
packageName: '[Graphy] - Ultimate FPS Counter - Stats Monitor & Debugger'
|
||||
packageVersion: 3.0.5
|
||||
assetPath: Assets/Graphy - Ultimate Stats Monitor/Runtime/Audio/G_AudioGraph.cs
|
||||
uploadId: 626547
|
||||
@@ -0,0 +1,239 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@martinTayx)
|
||||
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
|
||||
* Project: Graphy - Ultimate Stats Monitor
|
||||
* Date: 03-Jan-18
|
||||
* 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 System.Collections.Generic;
|
||||
using Tayx.Graphy.UI;
|
||||
using Tayx.Graphy.Utils;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
public class G_AudioManager : MonoBehaviour, IMovable, IModifiableState
|
||||
{
|
||||
#region Variables -> Serialized Private
|
||||
|
||||
[SerializeField] private GameObject m_audioGraphGameObject = null;
|
||||
[SerializeField] private Text m_audioDbText = null;
|
||||
|
||||
[SerializeField] private List<Image> m_backgroundImages = new List<Image>();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Private
|
||||
|
||||
private GraphyManager m_graphyManager = null;
|
||||
|
||||
private G_AudioGraph m_audioGraph = null;
|
||||
private G_AudioMonitor m_audioMonitor = null;
|
||||
private G_AudioText m_audioText = null;
|
||||
|
||||
private RectTransform m_rectTransform = null;
|
||||
private Vector2 m_origPosition = Vector2.zero;
|
||||
|
||||
private List<GameObject> m_childrenGameObjects = new List<GameObject>();
|
||||
|
||||
private GraphyManager.ModuleState m_previousModuleState = GraphyManager.ModuleState.FULL;
|
||||
private GraphyManager.ModuleState m_currentModuleState = GraphyManager.ModuleState.FULL;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdateParameters();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void SetPosition( GraphyManager.ModulePosition newModulePosition, Vector2 offset )
|
||||
{
|
||||
if ( newModulePosition == GraphyManager.ModulePosition.FREE )
|
||||
return;
|
||||
|
||||
m_rectTransform.anchoredPosition = m_origPosition;
|
||||
|
||||
float xSideOffset = Mathf.Abs( m_rectTransform.anchoredPosition.x ) + offset.x;
|
||||
float ySideOffset = Mathf.Abs( m_rectTransform.anchoredPosition.y ) + offset.y;
|
||||
|
||||
m_audioDbText.alignment = TextAnchor.UpperRight;
|
||||
|
||||
switch( newModulePosition )
|
||||
{
|
||||
case GraphyManager.ModulePosition.TOP_LEFT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.up;
|
||||
m_rectTransform.anchorMin = Vector2.up;
|
||||
m_rectTransform.anchoredPosition = new Vector2( xSideOffset, -ySideOffset );
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.TOP_RIGHT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.one;
|
||||
m_rectTransform.anchorMin = Vector2.one;
|
||||
m_rectTransform.anchoredPosition = new Vector2( -xSideOffset, -ySideOffset );
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.BOTTOM_LEFT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.zero;
|
||||
m_rectTransform.anchorMin = Vector2.zero;
|
||||
m_rectTransform.anchoredPosition = new Vector2( xSideOffset, ySideOffset );
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.BOTTOM_RIGHT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.right;
|
||||
m_rectTransform.anchorMin = Vector2.right;
|
||||
m_rectTransform.anchoredPosition = new Vector2( -xSideOffset, ySideOffset );
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetState( GraphyManager.ModuleState state, bool silentUpdate = false )
|
||||
{
|
||||
if( !silentUpdate )
|
||||
{
|
||||
m_previousModuleState = m_currentModuleState;
|
||||
}
|
||||
|
||||
m_currentModuleState = state;
|
||||
|
||||
switch( state )
|
||||
{
|
||||
case GraphyManager.ModuleState.FULL:
|
||||
gameObject.SetActive( true );
|
||||
m_childrenGameObjects.SetAllActive( true );
|
||||
SetGraphActive( true );
|
||||
|
||||
if( m_graphyManager.Background )
|
||||
{
|
||||
m_backgroundImages.SetOneActive( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_backgroundImages.SetAllActive( false );
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.TEXT:
|
||||
case GraphyManager.ModuleState.BASIC:
|
||||
gameObject.SetActive( true );
|
||||
m_childrenGameObjects.SetAllActive( true );
|
||||
SetGraphActive( false );
|
||||
|
||||
if( m_graphyManager.Background )
|
||||
{
|
||||
m_backgroundImages.SetOneActive( 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_backgroundImages.SetAllActive( false );
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.BACKGROUND:
|
||||
gameObject.SetActive( true );
|
||||
SetGraphActive( false );
|
||||
m_childrenGameObjects.SetAllActive( false );
|
||||
|
||||
m_backgroundImages.SetAllActive( false );
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.OFF:
|
||||
gameObject.SetActive( false );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestorePreviousState()
|
||||
{
|
||||
SetState( m_previousModuleState );
|
||||
}
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
foreach( var image in m_backgroundImages )
|
||||
{
|
||||
image.color = m_graphyManager.BackgroundColor;
|
||||
}
|
||||
|
||||
m_audioGraph.UpdateParameters();
|
||||
m_audioMonitor.UpdateParameters();
|
||||
m_audioText.UpdateParameters();
|
||||
|
||||
SetState( m_graphyManager.AudioModuleState );
|
||||
}
|
||||
|
||||
public void RefreshParameters()
|
||||
{
|
||||
foreach( var image in m_backgroundImages )
|
||||
{
|
||||
image.color = m_graphyManager.BackgroundColor;
|
||||
}
|
||||
|
||||
m_audioGraph.UpdateParameters();
|
||||
m_audioMonitor.UpdateParameters();
|
||||
m_audioText.UpdateParameters();
|
||||
|
||||
SetState( m_currentModuleState, true );
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
m_rectTransform = GetComponent<RectTransform>();
|
||||
m_origPosition = m_rectTransform.anchoredPosition;
|
||||
|
||||
m_audioGraph = GetComponent<G_AudioGraph>();
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
m_audioText = GetComponent<G_AudioText>();
|
||||
|
||||
foreach( Transform child in transform )
|
||||
{
|
||||
if( child.parent == transform )
|
||||
{
|
||||
m_childrenGameObjects.Add( child.gameObject );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetGraphActive( bool active )
|
||||
{
|
||||
m_audioGraph.enabled = active;
|
||||
m_audioGraphGameObject.SetActive( active );
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c0448d8db852b54480670d291c04f1a
|
||||
timeCreated: 1514998347
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 105778
|
||||
packageName: '[Graphy] - Ultimate FPS Counter - Stats Monitor & Debugger'
|
||||
packageVersion: 3.0.5
|
||||
assetPath: Assets/Graphy - Ultimate Stats Monitor/Runtime/Audio/G_AudioManager.cs
|
||||
uploadId: 626547
|
||||
@@ -0,0 +1,211 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@martinTayx)
|
||||
* 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.SceneManagement;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
/// <summary>
|
||||
/// Note: this class only works with Unity's AudioListener.
|
||||
/// If you're using a custom audio engine (like FMOD or WWise) it won't work,
|
||||
/// although you can always adapt it.
|
||||
/// </summary>
|
||||
public class G_AudioMonitor : MonoBehaviour
|
||||
{
|
||||
#region Variables -> Private
|
||||
|
||||
private const float m_refValue = 1f;
|
||||
|
||||
private GraphyManager m_graphyManager = null;
|
||||
|
||||
private AudioListener m_audioListener = null;
|
||||
|
||||
private GraphyManager.LookForAudioListener m_findAudioListenerInCameraIfNull =
|
||||
GraphyManager.LookForAudioListener.ON_SCENE_LOAD;
|
||||
|
||||
private FFTWindow m_FFTWindow = FFTWindow.Blackman;
|
||||
|
||||
private int m_spectrumSize = 512;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties -> Public
|
||||
|
||||
/// <summary>
|
||||
/// Current audio spectrum from the specified AudioListener.
|
||||
/// </summary>
|
||||
public float[] Spectrum { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Highest audio spectrum from the specified AudioListener in the last few seconds.
|
||||
/// </summary>
|
||||
public float[] SpectrumHighestValues { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum DB registered in the current spectrum.
|
||||
/// </summary>
|
||||
public float MaxDB { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if there is a reference to the audio listener.
|
||||
/// </summary>
|
||||
public bool SpectrumDataAvailable => m_audioListener != null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if( m_audioListener != null )
|
||||
{
|
||||
// Use this data to calculate the dB value
|
||||
|
||||
AudioListener.GetOutputData( Spectrum, 0 );
|
||||
|
||||
float sum = 0;
|
||||
|
||||
for( int i = 0; i < Spectrum.Length; i++ )
|
||||
{
|
||||
sum += Spectrum[ i ] * Spectrum[ i ]; // sum squared samples
|
||||
}
|
||||
|
||||
float rmsValue = Mathf.Sqrt( sum / Spectrum.Length ); // rms = square root of average
|
||||
|
||||
MaxDB = 20 * Mathf.Log10( rmsValue / m_refValue ); // calculate dB
|
||||
|
||||
if( MaxDB < -80 ) MaxDB = -80; // clamp it to -80dB min
|
||||
|
||||
// Use this data to draw the spectrum in the graphs
|
||||
|
||||
AudioListener.GetSpectrumData( Spectrum, 0, m_FFTWindow );
|
||||
|
||||
for( int i = 0; i < Spectrum.Length; i++ )
|
||||
{
|
||||
// Update the highest value if its lower than the current one
|
||||
if( Spectrum[ i ] > SpectrumHighestValues[ i ] )
|
||||
{
|
||||
SpectrumHighestValues[ i ] = Spectrum[ i ];
|
||||
}
|
||||
|
||||
// Slowly lower the value
|
||||
else
|
||||
{
|
||||
SpectrumHighestValues[ i ] = Mathf.Clamp
|
||||
(
|
||||
value: SpectrumHighestValues[ i ] - SpectrumHighestValues[ i ] * Time.deltaTime * 2,
|
||||
min: 0,
|
||||
max: 1
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( m_audioListener == null
|
||||
&& m_findAudioListenerInCameraIfNull == GraphyManager.LookForAudioListener.ALWAYS )
|
||||
{
|
||||
m_audioListener = FindAudioListener();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
m_findAudioListenerInCameraIfNull = m_graphyManager.FindAudioListenerInCameraIfNull;
|
||||
|
||||
m_audioListener = m_graphyManager.AudioListener;
|
||||
m_FFTWindow = m_graphyManager.FftWindow;
|
||||
m_spectrumSize = m_graphyManager.SpectrumSize;
|
||||
|
||||
if( m_audioListener == null
|
||||
&& m_findAudioListenerInCameraIfNull != GraphyManager.LookForAudioListener.NEVER )
|
||||
{
|
||||
m_audioListener = FindAudioListener();
|
||||
}
|
||||
|
||||
Spectrum = new float[m_spectrumSize];
|
||||
SpectrumHighestValues = new float[m_spectrumSize];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts spectrum values to decibels using logarithms.
|
||||
/// </summary>
|
||||
/// <param name="linear"></param>
|
||||
/// <returns></returns>
|
||||
public float lin2dB( float linear )
|
||||
{
|
||||
return Mathf.Clamp( Mathf.Log10( linear ) * 20.0f, -160.0f, 0.0f );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes a value in decibels between 0-1.
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
public float dBNormalized( float db )
|
||||
{
|
||||
return (db + 160f) / 160f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
/// <summary>
|
||||
/// Tries to find an audio listener in the main camera.
|
||||
/// </summary>
|
||||
private AudioListener FindAudioListener()
|
||||
{
|
||||
Camera mainCamera = Camera.main;
|
||||
|
||||
if( mainCamera != null && mainCamera.TryGetComponent( out AudioListener audioListener ) )
|
||||
{
|
||||
return audioListener;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded( Scene scene, LoadSceneMode loadSceneMode )
|
||||
{
|
||||
if( m_findAudioListenerInCameraIfNull == GraphyManager.LookForAudioListener.ON_SCENE_LOAD )
|
||||
{
|
||||
m_audioListener = FindAudioListener();
|
||||
}
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
UpdateParameters();
|
||||
|
||||
UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2216f4eff6a7a8a43b38b180fdd2fd9e
|
||||
timeCreated: 1513377074
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 105778
|
||||
packageName: '[Graphy] - Ultimate FPS Counter - Stats Monitor & Debugger'
|
||||
packageVersion: 3.0.5
|
||||
assetPath: Assets/Graphy - Ultimate Stats Monitor/Runtime/Audio/G_AudioMonitor.cs
|
||||
uploadId: 626547
|
||||
@@ -0,0 +1,90 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@martinTayx)
|
||||
* 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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 766a588f9a6cb55499c66ea772072e11
|
||||
timeCreated: 1513377063
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 105778
|
||||
packageName: '[Graphy] - Ultimate FPS Counter - Stats Monitor & Debugger'
|
||||
packageVersion: 3.0.5
|
||||
assetPath: Assets/Graphy - Ultimate Stats Monitor/Runtime/Audio/G_AudioText.cs
|
||||
uploadId: 626547
|
||||
Reference in New Issue
Block a user