Skip to content

Commit

Permalink
Added features. Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
inc8877 committed Dec 21, 2021
1 parent f0ea12a commit 0e49730
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 51 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## [1.1.0]

### Added
- Global `RSM` settings
- Automatic initialization at startup
- Canvas sort order
- Don't destroy when scene changed
- Changelog

### Changed
- encapsulated public `RSMCore` parameters
- improved code architecture
3 changes: 3 additions & 0 deletions CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Editor/InstantiateStatsMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#if UNITY_EDITOR
using RightStatsMonitor.Core;
using UnityEditor;
using UnityEngine;

namespace RightStatsMonitor.Editor
{
public class InstantiateStatsMonitor : MonoBehaviour
internal sealed class InstantiateStatsMonitor : MonoBehaviour
{
[MenuItem("Tools/RightStatsMonitor/Settings")]
private static void OpenRsmSettings()
{
Selection.activeObject = RsmSettings.Instance;
}

[MenuItem("Tools/RightStatsMonitor/Add to scene")]
private static void InstStatsMonitorInTheScene()
{
if (GameObject.Find("RSM")) EditorUtility.DisplayDialog("RightStatsMonitor is already in the scene",
if (FindObjectOfType<RSMCore>()) EditorUtility.DisplayDialog("RightStatsMonitor is already in the scene",
"", "OK");
else Instantiate(Resources.Load("RightStatsMonitor")).name = "RSM";
}
}
}
#endif
3 changes: 2 additions & 1 deletion Editor/inc8877.RightStatsMonitor.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "inc8877.RightStatsMonitor.Editor",
"rootNamespace": "",
"references": [
"GUID:343deaaf83e0cee4ca978e7df0b80d21"
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
"GUID:cf1500d7110284458ad5af39c28251a4"
],
"includePlatforms": [
"Editor"
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ Useful tool for displaying FPS with high precision. Very lightweight and fast. G

## How to use

1. [Install](#installation) RSM
2. Add RSM to scene by following this path `Tools > RightStatsMonitor > Add to scene`
1. [Install](#installation) `RSM`
2. Use available actions to interact with the `RSM` via `Tools > RightStatsMonitor`

The `RSM` has global settings like target frame rate, canvas sort order, initialize at startup, and more.
You can customize the way you want, to do this, just open the settings under this path `Tools> RightStatsMonitor> Preferences`.

## Installation

Expand Down
20 changes: 20 additions & 0 deletions Resources/RSM Settings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e01fa188869148b4a03249774d8c64ab, type: 3}
m_Name: RSM Settings
m_EditorClassIdentifier:
targetFrameRate: 60
fpsUpdateTime: 500
ramUpdateTime: 1
initializeAtRuntime: 1
dontDestroy: 1
canvasSortOrder: 1000
8 changes: 8 additions & 0 deletions Resources/RSM Settings.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 36 additions & 31 deletions Resources/RightStatsMonitor.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 347657964079713668}
- component: {fileID: 2478994870295797370}
- component: {fileID: 1349660970666911523}
- component: {fileID: 5868185528139508119}
- component: {fileID: 2478994870295797370}
m_Layer: 5
m_Name: holder
m_TagString: Untagged
Expand All @@ -29,6 +29,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children:
- {fileID: 8875935558411196955}
- {fileID: 8875935558876371609}
Expand All @@ -42,32 +43,6 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &2478994870295797370
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 38949635530650448}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 59f8146938fff824cb5fd77236b75775, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Padding:
m_Left: 12
m_Right: 0
m_Top: 3
m_Bottom: 3
m_ChildAlignment: 0
m_Spacing: 0
m_ChildForceExpandWidth: 1
m_ChildForceExpandHeight: 1
m_ChildControlWidth: 1
m_ChildControlHeight: 1
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
m_ReverseArrangement: 0
--- !u!222 &1349660970666911523
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -106,6 +81,32 @@ MonoBehaviour:
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 2
--- !u!114 &2478994870295797370
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 38949635530650448}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 59f8146938fff824cb5fd77236b75775, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Padding:
m_Left: 20
m_Right: 0
m_Top: 3
m_Bottom: 3
m_ChildAlignment: 0
m_Spacing: 0
m_ChildForceExpandWidth: 1
m_ChildForceExpandHeight: 1
m_ChildControlWidth: 1
m_ChildControlHeight: 1
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
m_ReverseArrangement: 0
--- !u!1 &8875935558411196948
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -134,6 +135,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 347657964079713668}
m_RootOrder: 0
Expand Down Expand Up @@ -268,6 +270,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children:
- {fileID: 347657964079713668}
m_Father: {fileID: 8875935559098112118}
Expand Down Expand Up @@ -306,6 +309,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 347657964079713668}
m_RootOrder: 2
Expand Down Expand Up @@ -442,6 +446,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 347657964079713668}
m_RootOrder: 1
Expand Down Expand Up @@ -578,6 +583,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children:
- {fileID: 8875935558490432179}
m_Father: {fileID: 0}
Expand Down Expand Up @@ -607,7 +613,7 @@ Canvas:
m_SortingBucketNormalizedSize: 0
m_AdditionalShaderChannelsFlag: 25
m_SortingLayerID: 0
m_SortingOrder: -10
m_SortingOrder: 1000
m_TargetDisplay: 0
--- !u!114 &8875935559098112112
MonoBehaviour:
Expand Down Expand Up @@ -661,13 +667,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 23c4ae52bafdb47e8b29bc51d0e09092, type: 3}
m_Name:
m_EditorClassIdentifier:
targetFPS: 120
fpsUpdateTime: 500
RAMUpdateTime: 1
fpsText: {fileID: 8875935558411196954}
allocatedRamText: {fileID: 8875935558876371608}
reservedRamText: {fileID: 8875935558695309443}
monoRamText: {fileID: 8875935560091874572}
canvas: {fileID: 8875935559098112119}
--- !u!1 &8875935560091874574
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -696,6 +700,7 @@ RectTransform:
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 347657964079713668}
m_RootOrder: 3
Expand Down
46 changes: 32 additions & 14 deletions Runtime/RSMCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,51 @@

namespace RightStatsMonitor.Core
{
public class RSMCore : MonoBehaviour
public sealed class RSMCore : MonoBehaviour
{
public int targetFPS = 120;
[Tooltip("in milliseconds")] public long fpsUpdateTime = 500;
[Tooltip("in seconds")] public float RAMUpdateTime = 1f;
public TMP_Text fpsText, allocatedRamText, reservedRamText, monoRamText;

public static float CurrentFPS { get; private set; }


public TMP_Text fpsText;
public TMP_Text allocatedRamText;
public TMP_Text reservedRamText;
public TMP_Text monoRamText;

[SerializeField] private Canvas canvas;

private RsmSettings _rsmSettings;
private Stopwatch _stopwatch;


private int _targetFPS;
private long _fpsUpdateTime;
private float _ramUpdateTime;

private int _passedFrames;
private long _passedMilliseconds;
private float _passedSeconds, _currentFrameTime, _timeCount = 1f;

private float _passedSeconds;
private float _currentFrameTime;
private float _timeCount = 1f;

private void Start()
{
_rsmSettings = RsmSettings.Instance;

if (_rsmSettings.DontDestroy) DontDestroyOnLoad(this);

canvas.sortingOrder = _rsmSettings.CanvasSortOrder;

_targetFPS = _rsmSettings.TargetFrameRate;
_fpsUpdateTime = _rsmSettings.FpsUpdateTime;
_ramUpdateTime = _rsmSettings.RamUpdateTime;

QualitySettings.vSyncCount = 0;
Application.targetFrameRate = targetFPS;
Application.targetFrameRate = _targetFPS;

_stopwatch = Stopwatch.StartNew();
}

private void Update()
{
if (_timeCount >= RAMUpdateTime)
if (_timeCount >= _ramUpdateTime)
{
_timeCount = 0f;
allocatedRamText.SetText("Allocated: {0:1}", Profiler.GetTotalAllocatedMemoryLong()/ 1048576f);
Expand All @@ -48,12 +66,12 @@ private void Update()

_passedMilliseconds = _stopwatch.ElapsedMilliseconds;

if (_passedMilliseconds < fpsUpdateTime) return;
if (_passedMilliseconds < _fpsUpdateTime) return;

_passedSeconds = _passedMilliseconds / 1000f;

CurrentFPS = _passedFrames / _passedSeconds;
_currentFrameTime = _passedMilliseconds / (float)_passedFrames;
_currentFrameTime = _passedMilliseconds / (_passedFrames * 1f); // `*1f` instead of explicit conversion

fpsText.SetText("FPS: {0:1} (ms {1:1})", CurrentFPS, _currentFrameTime);

Expand Down
23 changes: 23 additions & 0 deletions Runtime/RsmInitializationProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnityEngine;

namespace RightStatsMonitor.Core
{
internal static class RsmInitializationProcessor
{
[RuntimeInitializeOnLoadMethod]
private static void InitializationProcessor()
{
var settings = RsmSettings.Instance;

if (settings.InitializeAtRuntime)
{
if (GameObject.FindObjectsOfType<RSMCore>().Length > 1)
{
Debug.Log("RSM is already added to scene");
return;
}
GameObject.Instantiate(Resources.Load("RightStatsMonitor")).name = "RSM";
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/RsmInitializationProcessor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0e49730

Please sign in to comment.