namespace XmobiTea.UIEffect.FPS { using UnityEngine; using System.Collections.Generic; using System; using XmobiTea; public class HFPS : MonoBehaviour { private static HFPS Instance; private float deltaTime = 0.0f; private float fps = 0; private List ListStrings = new List(); private Vector2 scrollPosition = Vector2.zero; private bool _isShowDebug = false; private GUIStyle _styleText; private GUIStyle _styleFPS; private GUIStyle _styleButton; private int _width; private int _height; private Rect _rectClearDebugButton; private Rect _rectDebugBox; private Rect _rectDebugScrollBox; private Rect _rectDebugViewScrollBox; private bool _initialized = false; private GUIContent _contentBug; private int _textHeight; private bool _isPause = false; public static bool Enabled { get { return Instance.enabled; } set { Instance.enabled = value; } } public static void Log(object obj) { if (Instance == null) { Debug.LogError("[Effect] HFPS instance null"); return; } string message = string.Empty; if (obj != null) { message = obj.ToString(); } else { message = obj.GetType() + " null"; } if (!Instance._isPause) { Instance.ListStrings.Insert(0, message); if (Instance.ListStrings.Count > 50) { Instance.ListStrings.RemoveAt(Instance.ListStrings.Count - 1); } } Instance.enabled = true; } static HFPS() { if (Instance == null) { Instance = new GameObject { name = "Debug mobile" }.AddComponent(); Instance.enabled = false; DontDestroyOnLoad(Instance); } } private void Update() { deltaTime += (Time.deltaTime - deltaTime) * 0.1f; } private void OnGUI() { Init(); DrawDebug(); DrawFPS(); } private void Init() { if (_initialized) { return; } _initialized = true; var texBG = new Texture2D(2, 2); var colorsBG = new Color[4]; for (int i = 0; i < colorsBG.Length; i++) { colorsBG[i] = Color.black;//Normal BG color of list colorsBG[i].a = 0.8f; } texBG.SetPixels(colorsBG); texBG.Apply(); _width = Screen.width; _height = Screen.height; _textHeight = _height / 25; _styleText = GUI.skin.label; _styleText.alignment = TextAnchor.UpperLeft; _styleText.fontSize = _textHeight; _styleText.wordWrap = true; _styleText.normal.background = texBG; _styleButton = GUI.skin.button; _styleButton.fontSize = _textHeight; _styleFPS = GUI.skin.label; _styleFPS.alignment = TextAnchor.MiddleLeft; _styleFPS.fontSize = _textHeight; _styleFPS.normal.background = texBG; _styleFPS.padding = new RectOffset(10, 10, 0, 0); _textHeight += 10; _rectDebugBox = new Rect(0, _textHeight + 10, _width - 50, _height - _textHeight); _rectDebugScrollBox = new Rect(0, _textHeight, _width - 20, _height - _textHeight); _contentBug = new GUIContent(); } private void DrawDebug() { DrawDebugButton(); if (_isShowDebug && ListStrings.Count > 0) { scrollPosition = GUI.BeginScrollView(_rectDebugScrollBox, scrollPosition, _rectDebugBox, false, true); string text = string.Empty; var count = ListStrings.Count; for (int i = 0; i < count; i++) { text += " [" + i + "]: " + ListStrings[i] + "\n"; } if (text.Length > 2000) { text = text.Substring(0, 2000); } _contentBug.text = text; _rectDebugBox.height = _styleText.CalcHeight(_contentBug, _rectDebugBox.width); GUI.backgroundColor = Color.black; GUI.Label(_rectDebugBox, text, _styleText); GUI.EndScrollView(); } } private void DrawClearButton(float x, float y) { string labelButton = "Clear"; float mixW, maxW; GUIContent content = new GUIContent(labelButton); _styleButton.CalcMinMaxWidth(content, out mixW, out maxW); var rectButton = new Rect(x, y, maxW, _textHeight); if (GUI.Button(rectButton, "Clear", _styleButton)) { ListStrings.Clear(); _isShowDebug = false; } } private void DrawPauseButton(float x, float y) { string labelButton = _isPause ? "Start" : "Stop"; float mixW, maxW; GUIContent content = new GUIContent(labelButton); _styleButton.CalcMinMaxWidth(content, out mixW, out maxW); var rectButton = new Rect(x, y, maxW, _textHeight); if (GUI.Button(rectButton, labelButton, _styleButton)) { _isPause = !_isPause; if (_isPause) { ListStrings.Insert(0, "Stop Tracking Log"); } else { ListStrings.Insert(0, "Start Tracking Log"); } } } private void DrawDebugButton() { string labelButton = (!_isShowDebug ? "Show Debug" : "Hide Debug") + (ListStrings.Count > 0 ? "(" + ListStrings.Count + ")" : "(Empty)"); float mixW, maxW; GUIContent content = new GUIContent(labelButton); _styleButton.CalcMinMaxWidth(content, out mixW, out maxW); var rectShowDebugButton = new Rect((_width / 2) - (maxW / 2), 0, maxW, _textHeight); if (GUI.Button(rectShowDebugButton, labelButton, _styleButton)) { _isShowDebug = !_isShowDebug; } if (_isShowDebug && ListStrings.Count > 0) { DrawClearButton(rectShowDebugButton.x + rectShowDebugButton.width, rectShowDebugButton.y); } if (_isShowDebug) { content = new GUIContent(_isPause ? "Start" : "Stop"); _styleButton.CalcMinMaxWidth(content, out mixW, out maxW); DrawPauseButton(rectShowDebugButton.x - maxW, rectShowDebugButton.y); } } private void DrawFPS() { fps = 1.0f / deltaTime; var cc = GUI.color; GUI.color = Color.red; string textFPS = string.Format("FPS: {0:00.00}", Math.Round(fps, 2)); float mixW, maxW; GUIContent content = new GUIContent(textFPS); _styleFPS.CalcMinMaxWidth(content, out mixW, out maxW); var rectFPS = new Rect(_width - maxW, 0, maxW, _textHeight); GUI.Label(rectFPS, textFPS, _styleFPS); GUI.color = cc; } } }