diff options
Diffstat (limited to 'Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps')
8 files changed, 811 insertions, 0 deletions
diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs new file mode 100644 index 0000000..c25d964 --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs @@ -0,0 +1,178 @@ +/* --------------------------------------- + * Author: Martin Pane (martintayx@gmail.com) (@tayx94) + * Collaborators: Lars Aalbertsen (@Rockylars) + * Project: Graphy - Ultimate Stats Monitor + * Date: 15-Dec-17 + * Studio: Tayx + * + * 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.Fps +{ + public class G_FpsGraph : G_Graph + { + /* ----- TODO: ---------------------------- + * Add summaries to the variables. + * Add summaries to the functions. + * Check if we should add a "RequireComponent" for "FpsMonitor". + * --------------------------------------*/ + + #region Variables -> Serialized Private + + [SerializeField] private Image m_imageGraph = null; + + [SerializeField] private Shader ShaderFull = null; + [SerializeField] private Shader ShaderLight = null; + + #endregion + + #region Variables -> Private + + private GraphyManager m_graphyManager = null; + + private G_FpsMonitor m_fpsMonitor = null; + + private int m_resolution = 150; + + private G_GraphShader m_shaderGraph = null; + + private int[] m_fpsArray; + + private int m_highestFps; + + #endregion + + #region Methods -> Unity Callbacks + + private void OnEnable() + { + Init(); + } + + private void Update() + { + UpdateGraph(); + } + + #endregion + + #region Methods -> Public + + public void UpdateParameters() + { + switch (m_graphyManager.GraphyMode) + { + case GraphyManager.Mode.FULL: + m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull; + m_shaderGraph.Image.material = new Material(ShaderFull); + break; + + case GraphyManager.Mode.LIGHT: + m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeLight; + m_shaderGraph.Image.material = new Material(ShaderLight); + break; + } + + m_shaderGraph.InitializeShader(); + + m_resolution = m_graphyManager.FpsGraphResolution; + + CreatePoints(); + } + + #endregion + + #region Methods -> Protected Override + + protected override void UpdateGraph() + { + int fps = (int)(1 / Time.unscaledDeltaTime); + + int currentMaxFps = 0; + + for (int i = 0; i <= m_resolution - 1; i++) + { + if (i >= m_resolution - 1) + { + m_fpsArray[i] = fps; + } + else + { + m_fpsArray[i] = m_fpsArray[i + 1]; + } + + // Store the highest fps to use as the highest point in the graph + + if (currentMaxFps < m_fpsArray[i]) + { + currentMaxFps = m_fpsArray[i]; + } + + } + + m_highestFps = m_highestFps < 1 || m_highestFps <= currentMaxFps ? currentMaxFps : m_highestFps - 1; + + for (int i = 0; i <= m_resolution - 1; i++) + { + m_shaderGraph.Array[i] = m_fpsArray[i] / (float) m_highestFps; + } + + // Update the material values + + m_shaderGraph.UpdatePoints(); + + m_shaderGraph.Average = m_fpsMonitor.AverageFPS / m_highestFps; + m_shaderGraph.UpdateAverage(); + + m_shaderGraph.GoodThreshold = (float)m_graphyManager.GoodFPSThreshold / m_highestFps; + m_shaderGraph.CautionThreshold = (float)m_graphyManager.CautionFPSThreshold / m_highestFps; + m_shaderGraph.UpdateThresholds(); + } + + protected override void CreatePoints() + { + m_shaderGraph.Array = new float[m_resolution]; + + m_fpsArray = new int[m_resolution]; + + for (int i = 0; i < m_resolution; i++) + { + m_shaderGraph.Array[i] = 0; + } + + m_shaderGraph.GoodColor = m_graphyManager.GoodFPSColor; + m_shaderGraph.CautionColor = m_graphyManager.CautionFPSColor; + m_shaderGraph.CriticalColor = m_graphyManager.CriticalFPSColor; + + m_shaderGraph.UpdateColors(); + + m_shaderGraph.UpdateArray(); + } + + #endregion + + #region Methods -> Private + + private void Init() + { + m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>(); + + m_fpsMonitor = GetComponent<G_FpsMonitor>(); + + m_shaderGraph = new G_GraphShader + { + Image = m_imageGraph + }; + + UpdateParameters(); + } + + #endregion + } +}
\ No newline at end of file diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs.meta b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs.meta new file mode 100644 index 0000000..97df8e6 --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsGraph.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 2e119c7747ac400478c7cfcaea03214e +timeCreated: 1511794194 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs new file mode 100644 index 0000000..395b2eb --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs @@ -0,0 +1,257 @@ +/* --------------------------------------- + * Author: Martin Pane (martintayx@gmail.com) (@tayx94) + * Collaborators: Lars Aalbertsen (@Rockylars) + * Project: Graphy - Ultimate Stats Monitor + * Date: 03-Jan-18 + * Studio: Tayx + * + * This project is released under the MIT license. + * Attribution is not required, but it is always welcomed! + * -------------------------------------*/ + +using UnityEngine; +using System.Collections.Generic; +using Tayx.Graphy.UI; +using Tayx.Graphy.Utils; +using UnityEngine.UI; + +namespace Tayx.Graphy.Fps +{ + public class G_FpsManager : MonoBehaviour, IMovable, IModifiableState + { + /* ----- TODO: ---------------------------- + * Check if we can seal this class. + * Add summaries to the variables. + * Add summaries to the functions. + * Check if we should add a "RequireComponent" for "RectTransform". + * Check if we should add a "RequireComponent" for "FpsGraph". + * Check if we should add a "RequireComponent" for "FpsMonitor". + * Check if we should add a "RequireComponent" for "FpsText". + * --------------------------------------*/ + + #region Variables -> Serialized Private + + [SerializeField] private GameObject m_fpsGraphGameObject = null; + + [SerializeField] private List<GameObject> m_nonBasicTextGameObjects = new List<GameObject>(); + + [SerializeField] private List<Image> m_backgroundImages = new List<Image>(); + + #endregion + + #region Variables -> Private + + private GraphyManager m_graphyManager = null; + + private G_FpsGraph m_fpsGraph = null; + private G_FpsMonitor m_fpsMonitor = null; + private G_FpsText m_fpsText = null; + + private RectTransform m_rectTransform = null; + + 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) + { + float xSideOffset = Mathf.Abs(m_rectTransform.anchoredPosition.x); + float ySideOffset = Mathf.Abs(m_rectTransform.anchoredPosition.y); + + 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; + + case GraphyManager.ModulePosition.FREE: + 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: + 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.BASIC: + gameObject.SetActive(true); + m_childrenGameObjects.SetAllActive(true); + m_nonBasicTextGameObjects.SetAllActive(false); + SetGraphActive(false); + + if (m_graphyManager.Background) + { + m_backgroundImages.SetOneActive(2); + } + else + { + m_backgroundImages.SetAllActive(false); + } + + break; + + case GraphyManager.ModuleState.BACKGROUND: + gameObject.SetActive(true); + m_childrenGameObjects.SetAllActive(false); + SetGraphActive(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_fpsGraph .UpdateParameters(); + m_fpsMonitor .UpdateParameters(); + m_fpsText .UpdateParameters(); + + SetState(m_graphyManager.FpsModuleState); + } + + public void RefreshParameters() + { + foreach (var image in m_backgroundImages) + { + image.color = m_graphyManager.BackgroundColor; + } + + m_fpsGraph .UpdateParameters(); + m_fpsMonitor .UpdateParameters(); + m_fpsText .UpdateParameters(); + + SetState(m_currentModuleState, true); + } + + #endregion + + #region Methods -> Private + + private void Init() + { + m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>(); + + m_rectTransform = GetComponent<RectTransform>(); + + m_fpsGraph = GetComponent<G_FpsGraph>(); + m_fpsMonitor = GetComponent<G_FpsMonitor>(); + m_fpsText = GetComponent<G_FpsText>(); + + foreach (Transform child in transform) + { + if (child.parent == transform) + { + m_childrenGameObjects.Add(child.gameObject); + } + } + } + + private void SetGraphActive(bool active) + { + m_fpsGraph.enabled = active; + m_fpsGraphGameObject.SetActive(active); + } + + #endregion + } +}
\ No newline at end of file diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs.meta b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs.meta new file mode 100644 index 0000000..f43ada8 --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsManager.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 221dc0b3655ddb749ace6bad55f0159f +timeCreated: 1514998359 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs new file mode 100644 index 0000000..551681e --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs @@ -0,0 +1,158 @@ +/* --------------------------------------- + * Author: Martin Pane (martintayx@gmail.com) (@tayx94) + * Collaborators: Lars Aalbertsen (@Rockylars) + * Project: Graphy - Ultimate Stats Monitor + * Date: 15-Dec-17 + * Studio: Tayx + * + * This project is released under the MIT license. + * Attribution is not required, but it is always welcomed! + * -------------------------------------*/ + +using UnityEngine; +using System.Collections.Generic; + +namespace Tayx.Graphy.Fps +{ + public class G_FpsMonitor : MonoBehaviour + { + /* ----- TODO: ---------------------------- + * Add summaries to the variables. + * Add summaries to the functions. + * --------------------------------------*/ + + #region Variables -> Serialized Private + + [SerializeField] private int m_averageSamples = 200; + + #endregion + + #region Variables -> Private + + private GraphyManager m_graphyManager; + + private float m_currentFps = 0f; + private float m_avgFps = 0f; + private float m_minFps = 0f; + private float m_maxFps = 0f; + + private List<float> m_averageFpsSamples; + + private int m_timeToResetMinMaxFps = 10; + + private float m_timeToResetMinFpsPassed = 0f; + private float m_timeToResetMaxFpsPassed = 0f; + + private float unscaledDeltaTime = 0f; + + #endregion + + #region Properties -> Public + + public float CurrentFPS { get { return m_currentFps; } } + public float AverageFPS { get { return m_avgFps;} } + + public float MinFPS { get { return m_minFps;} } + public float MaxFPS { get { return m_maxFps;} } + + #endregion + + #region Methods -> Unity Callbacks + + private void Awake() + { + Init(); + } + + private void Update() + { + unscaledDeltaTime = Time.unscaledDeltaTime; + + m_timeToResetMinFpsPassed += unscaledDeltaTime; + m_timeToResetMaxFpsPassed += unscaledDeltaTime; + + // Update fps and ms + + m_currentFps = 1 / unscaledDeltaTime; + + // Update avg fps + + m_avgFps = 0; + + if (m_averageFpsSamples.Count >= m_averageSamples) + { + m_averageFpsSamples.Add(m_currentFps); + m_averageFpsSamples.RemoveAt(0); + } + else + { + m_averageFpsSamples.Add(m_currentFps); + } + + for (int i = 0; i < m_averageFpsSamples.Count; i++) + { + m_avgFps += m_averageFpsSamples[i]; + } + + m_avgFps /= m_averageSamples; + + // Checks to reset min and max fps + + if ( m_timeToResetMinMaxFps > 0 + && m_timeToResetMinFpsPassed > m_timeToResetMinMaxFps) + { + m_minFps = 0; + m_timeToResetMinFpsPassed = 0; + } + + if ( m_timeToResetMinMaxFps > 0 + && m_timeToResetMaxFpsPassed > m_timeToResetMinMaxFps) + { + m_maxFps = 0; + m_timeToResetMaxFpsPassed = 0; + } + + // Update min fps + + if (m_currentFps < m_minFps || m_minFps <= 0) + { + m_minFps = m_currentFps; + + m_timeToResetMinFpsPassed = 0; + } + + // Update max fps + + if (m_currentFps > m_maxFps || m_maxFps <= 0) + { + m_maxFps = m_currentFps; + + m_timeToResetMaxFpsPassed = 0; + } + } + + #endregion + + #region Methods -> Public + + public void UpdateParameters() + { + m_timeToResetMinMaxFps = m_graphyManager.TimeToResetMinMaxFps; + } + + #endregion + + #region Methods -> Private + + private void Init() + { + m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>(); + + m_averageFpsSamples = new List<float>(); + + UpdateParameters(); + } + + #endregion + } +}
\ No newline at end of file diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs.meta b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs.meta new file mode 100644 index 0000000..02bc149 --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsMonitor.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: b205584e495e4634aa3a332a78102a19 +timeCreated: 1513376950 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs new file mode 100644 index 0000000..1a6ef9f --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs @@ -0,0 +1,170 @@ +/* --------------------------------------- + * Author: Martin Pane (martintayx@gmail.com) (@tayx94) + * Collaborators: Lars Aalbertsen (@Rockylars) + * Project: Graphy - Ultimate Stats Monitor + * Date: 22-Nov-17 + * Studio: Tayx + * + * 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.Fps +{ + public class G_FpsText : MonoBehaviour + { + /* ----- TODO: ---------------------------- + * Add summaries to the variables. + * Add summaries to the functions. + * Check if we should add a "RequireComponent" for "FpsMonitor". + * Improve the IntString Init to come from the core instead. + * --------------------------------------*/ + + #region Variables -> Serialized Private + + [SerializeField] private Text m_fpsText = null; + [SerializeField] private Text m_msText = null; + + [SerializeField] private Text m_avgFpsText = null; + [SerializeField] private Text m_minFpsText = null; + [SerializeField] private Text m_maxFpsText = null; + + #endregion + + #region Variables -> Private + + private GraphyManager m_graphyManager = null; + + private G_FpsMonitor m_fpsMonitor = null; + + private int m_updateRate = 4; // 4 updates per sec. + + private int m_frameCount = 0; + + private float m_deltaTime = 0f; + + private float m_fps = 0f; + + private const int m_minFps = 0; + private const int m_maxFps = 10000; + + private const string m_msStringFormat = "0.0"; + + #endregion + + #region Methods -> Unity Callbacks + + private void Awake() + { + Init(); + } + + private void Update() + { + m_deltaTime += Time.unscaledDeltaTime; + + m_frameCount++; + + // Only update texts 'm_updateRate' times per second + + if (m_deltaTime > 1f / m_updateRate) + { + m_fps = m_frameCount / m_deltaTime; + + // Update fps and ms + + m_fpsText.text = Mathf.RoundToInt(m_fps).ToStringNonAlloc(); + m_msText.text = (m_deltaTime / m_frameCount * 1000f).ToStringNonAlloc(m_msStringFormat); + + // Update min fps + + m_minFpsText.text = m_fpsMonitor.MinFPS.ToInt().ToStringNonAlloc(); + + SetFpsRelatedTextColor(m_minFpsText, m_fpsMonitor.MinFPS); + + // Update max fps + + m_maxFpsText.text = m_fpsMonitor.MaxFPS.ToInt().ToStringNonAlloc(); + + SetFpsRelatedTextColor(m_maxFpsText, m_fpsMonitor.MaxFPS); + + // Update avg fps + + m_avgFpsText.text = m_fpsMonitor.AverageFPS.ToInt().ToStringNonAlloc(); + + SetFpsRelatedTextColor(m_avgFpsText, m_fpsMonitor.AverageFPS); + + // Reset variables + + m_deltaTime = 0f; + m_frameCount = 0; + } + } + + #endregion + + #region Methods -> Public + + public void UpdateParameters() + { + m_updateRate = m_graphyManager.FpsTextUpdateRate; + } + + #endregion + + #region Methods -> Private + + /// <summary> + /// Assigns color to a text according to their fps numeric value and + /// the colors specified in the 3 categories (Good, Caution, Critical). + /// </summary> + /// + /// <param name="text"> + /// UI Text component to change its color + /// </param> + /// + /// <param name="fps"> + /// Numeric fps value + /// </param> + private void SetFpsRelatedTextColor(Text text, float fps) + { + if (fps > m_graphyManager.GoodFPSThreshold) + { + text.color = m_graphyManager.GoodFPSColor; + } + else if (fps > m_graphyManager.CautionFPSThreshold) + { + text.color = m_graphyManager.CautionFPSColor; + } + else + { + text.color = m_graphyManager.CriticalFPSColor; + } + } + + private void Init() + { + //TODO: Replace this with one activated from the core and figure out the min value. + if (!G_IntString.Inited || G_IntString.MinValue > m_minFps || G_IntString.MaxValue < m_maxFps) + { + G_IntString.Init + ( + minNegativeValue: m_minFps, + maxPositiveValue: m_maxFps + ); + } + + m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>(); + + m_fpsMonitor = GetComponent<G_FpsMonitor>(); + + UpdateParameters(); + } + + #endregion + } +} diff --git a/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs.meta b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs.meta new file mode 100644 index 0000000..88db3fa --- /dev/null +++ b/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Fps/G_FpsText.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f74bbf307d92b0d4e81ae60b9eb1e42f +timeCreated: 1511555604 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |
