From fcbf323219fdfb2dd8700f788b710fe8913eb26c Mon Sep 17 00:00:00 2001 From: in0finite Date: Sat, 25 Apr 2020 19:48:35 +0200 Subject: [PATCH] convert FPS display --- Assets/Prefabs/UI.prefab | 266 +++++++++++++++++++++- Assets/Scripts/Utilities/UI/FPSDisplay.cs | 43 ++-- 2 files changed, 273 insertions(+), 36 deletions(-) diff --git a/Assets/Prefabs/UI.prefab b/Assets/Prefabs/UI.prefab index 05bffb1a..72790e32 100644 --- a/Assets/Prefabs/UI.prefab +++ b/Assets/Prefabs/UI.prefab @@ -363,9 +363,10 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] + m_Children: + - {fileID: 200450069953410140} m_Father: {fileID: 4450559263798022} - m_RootOrder: 2 + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &114042572616448592 MonoBehaviour: @@ -379,6 +380,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a6f440c6b65774755ba61e110bc899d1, type: 3} m_Name: m_EditorClassIdentifier: + fpsImage: {fileID: 6132537284630574696} + fpsText: {fileID: 8790997500914401142} --- !u!1 &1266252102531166 GameObject: m_ObjectHideFlags: 0 @@ -619,7 +622,7 @@ Transform: m_Children: - {fileID: 8427225095601868331} m_Father: {fileID: 4450559263798022} - m_RootOrder: 4 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &114727961998957292 MonoBehaviour: @@ -735,10 +738,10 @@ Transform: m_Children: - {fileID: 4105821110255708} - {fileID: 4163146413860628} - - {fileID: 4139429664069144} - {fileID: 5719983600610341389} - {fileID: 4649684838142242} - {fileID: 9213750055200343700} + - {fileID: 4139429664069144} - {fileID: 6831965866518684118} - {fileID: 2330455679951064125} m_Father: {fileID: 0} @@ -1959,7 +1962,7 @@ Transform: m_Children: - {fileID: 2221193767689923803} m_Father: {fileID: 4450559263798022} - m_RootOrder: 5 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &6976359054445641093 MonoBehaviour: @@ -2020,7 +2023,7 @@ Transform: m_Children: - {fileID: 7060217674643716524} m_Father: {fileID: 4450559263798022} - m_RootOrder: 3 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &3389315516088317126 MonoBehaviour: @@ -4616,6 +4619,106 @@ MonoBehaviour: m_VerticalOverflow: 0 m_LineSpacing: 1 m_Text: "\u25C0" +--- !u!1 &4288050171810510817 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 200450069953410140} + - component: {fileID: 7266881340723243798} + - component: {fileID: 2995113739688689129} + - component: {fileID: 8776795243884254577} + m_Layer: 0 + m_Name: Canvas + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &200450069953410140 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4288050171810510817} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 0, y: 0, z: 0} + m_Children: + - {fileID: 3738999075733896444} + - {fileID: 1383366114373465418} + m_Father: {fileID: 4139429664069144} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0, y: 0} +--- !u!223 &7266881340723243798 +Canvas: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4288050171810510817} + m_Enabled: 1 + serializedVersion: 3 + m_RenderMode: 0 + m_Camera: {fileID: 0} + m_PlaneDistance: 100 + m_PixelPerfect: 0 + m_ReceivesEvents: 1 + m_OverrideSorting: 0 + m_OverridePixelPerfect: 0 + m_SortingBucketNormalizedSize: 0 + m_AdditionalShaderChannelsFlag: 0 + m_SortingLayerID: 0 + m_SortingOrder: 0 + m_TargetDisplay: 0 +--- !u!114 &2995113739688689129 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4288050171810510817} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 1980459831, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_UiScaleMode: 1 + m_ReferencePixelsPerUnit: 100 + m_ScaleFactor: 1 + m_ReferenceResolution: {x: 1280, y: 720} + m_ScreenMatchMode: 0 + m_MatchWidthOrHeight: 0 + m_PhysicalUnit: 3 + m_FallbackScreenDPI: 96 + m_DefaultSpriteDPI: 96 + m_DynamicPixelsPerUnit: 1 +--- !u!114 &8776795243884254577 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4288050171810510817} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 1301386320, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_IgnoreReversedGraphics: 1 + m_BlockingObjects: 0 + m_BlockingMask: + serializedVersion: 2 + m_Bits: 4294967295 --- !u!1 &4539774699302292208 GameObject: m_ObjectHideFlags: 0 @@ -4695,6 +4798,85 @@ MonoBehaviour: m_VerticalOverflow: 0 m_LineSpacing: 1 m_Text: Enter +--- !u!1 &4667189319374599351 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1383366114373465418} + - component: {fileID: 4096536276451913193} + - component: {fileID: 8790997500914401142} + m_Layer: 0 + m_Name: FPSText + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1383366114373465418 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4667189319374599351} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 200450069953410140} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 1, y: 0} + m_AnchorMax: {x: 1, y: 0} + m_AnchoredPosition: {x: -142.3, y: 17.5} + m_SizeDelta: {x: 100, y: 25} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &4096536276451913193 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4667189319374599351} + m_CullTransparentMesh: 0 +--- !u!114 &8790997500914401142 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4667189319374599351} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_RaycastTarget: 0 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI, + Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + m_FontData: + m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_FontSize: 14 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 10 + m_MaxSize: 40 + m_Alignment: 5 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: 50 fps --- !u!1 &4755809051094174587 GameObject: m_ObjectHideFlags: 0 @@ -6412,6 +6594,78 @@ MonoBehaviour: m_Calls: [] m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null +--- !u!1 &6471814800377377771 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 3738999075733896444} + - component: {fileID: 3541661662474480608} + - component: {fileID: 6132537284630574696} + m_Layer: 0 + m_Name: FPSTexture + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &3738999075733896444 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6471814800377377771} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 200450069953410140} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 1, y: 0} + m_AnchorMax: {x: 1, y: 0} + m_AnchoredPosition: {x: -45, y: 17.5} + m_SizeDelta: {x: 80, y: 25} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &3541661662474480608 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6471814800377377771} + m_CullTransparentMesh: 0 +--- !u!114 &6132537284630574696 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6471814800377377771} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: -98529514, guid: f70555f144d8491a825f0804e09c671c, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 0 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI, + Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + m_Texture: {fileID: 0} + m_UVRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 --- !u!1 &6553587779773763042 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Utilities/UI/FPSDisplay.cs b/Assets/Scripts/Utilities/UI/FPSDisplay.cs index f39cc33e..6e513a14 100644 --- a/Assets/Scripts/Utilities/UI/FPSDisplay.cs +++ b/Assets/Scripts/Utilities/UI/FPSDisplay.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using UnityEngine; using System.Linq; +using UnityEngine.UI; namespace SanAndreasUnity.Utilities { @@ -19,7 +20,8 @@ namespace SanAndreasUnity.Utilities { private static bool _showFPS = true; - GUIStyle labelStyle = null; + public RawImage fpsImage; + public Text fpsText; @@ -29,6 +31,7 @@ namespace SanAndreasUnity.Utilities { colors = new Color[fpsTexture.width * fpsTexture.height]; + this.fpsImage.texture = this.fpsTexture; } void Update () { @@ -38,9 +41,17 @@ namespace SanAndreasUnity.Utilities { if (Input.GetKeyDown(KeyCode.F10)) _showFPS = !_showFPS; - + if (_showFPS) + { UpdateTexture(1.0f / fpsDeltaTime); + + float msec = fpsDeltaTime * 1000.0f; + float fps = 1.0f / fpsDeltaTime; + string text = string.Format("{0:0.}fps ({1:0.0}ms)", fps, msec); + if (this.fpsText.text != text) + this.fpsText.text = text; + } } @@ -96,34 +107,6 @@ namespace SanAndreasUnity.Utilities { } - void OnGUI() { - - // if (Event.current.type != EventType.Repaint) - // return; - - if (null == labelStyle) - labelStyle = new GUIStyle("label") { alignment = TextAnchor.MiddleLeft }; - - if (_showFPS) - { - float msec = fpsDeltaTime * 1000.0f; - float fps = 1.0f / fpsDeltaTime; - - // Show FPS counter - Rect rect = GUIUtils.GetCornerRect(ScreenCorner.BottomRight, 100, 25, new Vector2(15 + fpsTexture.width, 10)); - UnityEngine.Profiling.Profiler.BeginSample("Draw label"); - GUI.Label(rect, string.Format("{0:0.}fps ({1:0.0}ms)", fps, msec), labelStyle); - UnityEngine.Profiling.Profiler.EndSample(); - - // Show FPS history - UnityEngine.Profiling.Profiler.BeginSample("Draw texture"); - GUI.DrawTexture(GUIUtils.GetCornerRect(ScreenCorner.BottomRight, fpsTexture.width, fpsTexture.height, new Vector2(5, fpsTexture.height - 15)), fpsTexture); - UnityEngine.Profiling.Profiler.EndSample(); - - } - - } - } }