Skip to content

Commit

Permalink
Add memory usage info to palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Klara committed Dec 16, 2024
1 parent 716dd0b commit e569c52
Show file tree
Hide file tree
Showing 3 changed files with 349 additions and 1 deletion.
252 changes: 251 additions & 1 deletion Assets/Prefabs/CameraRig.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,7 @@ Transform:
m_Children:
- {fileID: 4908724992920432}
- {fileID: 4657463438926638}
- {fileID: 5076219253652055847}
m_Father: {fileID: 4136544352420854}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1058421795516628
Expand Down Expand Up @@ -30445,7 +30446,7 @@ Camera:
m_Depth: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294952671
m_Bits: 4294952703
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
Expand Down Expand Up @@ -74905,6 +74906,121 @@ Camera:
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!1 &6249299646841875550
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5076219253652055847}
- component: {fileID: 4506482073504366338}
- component: {fileID: 2095997645222470298}
- component: {fileID: 4216401758512156658}
- component: {fileID: 4674249550964265817}
m_Layer: 5
m_Name: Profiler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &5076219253652055847
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6249299646841875550}
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: -0.2139}
m_LocalScale: {x: 0.0006, y: 0.0006, z: 0.0006}
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 6483366733439243235}
m_Father: {fileID: 4219182568934276}
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0.0561, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!223 &4506482073504366338
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6249299646841875550}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 2
m_Camera: {fileID: 20010936846075698}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_VertexColorAlwaysGammaSpace: 0
m_AdditionalShaderChannelsFlag: 25
m_UpdateRectTransformForStandalone: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!114 &2095997645222470298
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6249299646841875550}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 0
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 1
--- !u!114 &4216401758512156658
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6249299646841875550}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3}
m_Name:
m_EditorClassIdentifier:
m_IgnoreReversedGraphics: 1
m_BlockingObjects: 0
m_BlockingMask:
serializedVersion: 2
m_Bits: 4294967295
--- !u!114 &4674249550964265817
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6249299646841875550}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 94a65a525dc833d44b703c9497a38f71, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8311839842708757556
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -74937,6 +75053,140 @@ Transform:
- {fileID: 4836180625913568720}
m_Father: {fileID: 4657463438926638}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &8691313442411617469
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6483366733439243235}
- component: {fileID: 202643350866492317}
- component: {fileID: 4993443190278709601}
m_Layer: 5
m_Name: Text (TMP)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6483366733439243235
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8691313442411617469}
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: 5076219253652055847}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 0, y: 0.5}
m_AnchoredPosition: {x: 0, y: -7}
m_SizeDelta: {x: 360, y: 50}
m_Pivot: {x: 0, y: 0}
--- !u!222 &202643350866492317
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8691313442411617469}
m_CullTransparentMesh: 1
--- !u!114 &4993443190278709601
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8691313442411617469}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: 100 MB memory used
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 30
m_fontSizeBase: 30
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 1
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &8697257038037221240
GameObject:
m_ObjectHideFlags: 0
Expand Down
87 changes: 87 additions & 0 deletions Assets/Profiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Profiling.Memory;
using UnityEngine;

public class Profiler : MonoBehaviour
{
private TMPro.TextMeshProUGUI text;
private float timeSinceLastUpdate;
const int kFreeBlockPow2Buckets = 24;
// Start is called before the first frame update
private bool lowMemory;

private void LowMemory()
{
lowMemory = true;
}

void Start()
{
text = GetComponentInChildren<TMPro.TextMeshProUGUI>();
// Application.lowMemory += LowMemory;
}

// Update is called once per frame
void Update()
{
timeSinceLastUpdate += Time.deltaTime;
if (timeSinceLastUpdate >= 1.0f)
{
var totalAllocatedMemory = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / 1024 / 1024;
// var totalReservedMemory = UnityEngine.Profiling.Profiler.GetTotalReservedMemoryLong() / 1024 / 1024;
// var totalUnusedReservedMemory = UnityEngine.Profiling.Profiler.GetTotalUnusedReservedMemoryLong() / 1024 / 1024;
// var graphicsAllocatedMemory = UnityEngine.Profiling.Profiler.GetAllocatedMemoryForGraphicsDriver() / 1024 / 1024;
// var usedHeapSize = UnityEngine.Profiling.Profiler.usedHeapSizeLong / 1024 / 1024;

// var freeStats = new NativeArray<int>(kFreeBlockPow2Buckets, Allocator.Temp);
// var freeBlocks = UnityEngine.Profiling.Profiler.GetTotalFragmentationInfo(freeStats);

// if memory is low show text in red:
// text.color = lowMemory ? Color.red : Color.white;

// format text
text.text = $"{totalAllocatedMemory:D} MB memory used\n";
// $"{totalReservedMemory:F} MB total reserved mem \n" +
// $"{totalUnusedReservedMemory:F} MB total unused reserved mem \n" +
// $"{graphicsAllocatedMemory:F} MB graphics alloc mem \n" +
// $"{totalAllocatedMemory + graphicsAllocatedMemory:F} MB total alloc mem + graphics alloc mem \n";
// $"{usedHeapSize:F} MB \n/ " +
// $"{freeBlocks}";
// if (lowMemory)
// {
// text.text += "(low memory!!!)";
// }
// #if UNITY_ANDROID && !UNITY_EDITOR
// CheckAndroidMemory();
// #endif
timeSinceLastUpdate = 0;
}
}

void CheckAndroidMemory()
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (AndroidJavaObject activityManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "activity"))
{
AndroidJavaObject memoryInfo = new AndroidJavaObject("android.app.ActivityManager$MemoryInfo");
activityManager.Call("getMemoryInfo", memoryInfo);
long totalMem = memoryInfo.Get<long>("totalMem");
long availMem = memoryInfo.Get<long>("availMem");
long threshold = memoryInfo.Get<long>("threshold");
bool lowMemory = memoryInfo.Get<bool>("lowMemory");
text.text += // $"{totalMem / (1024 * 1024)} MB andr totl mem \n" +
$"{availMem / (1024 * 1024)} MB avail mem \n" +
$"{threshold / (1024 * 1024)} MB threshold \n";
// $"{lowMemory} low Mem\n";
}
}

private void OnDestroy()
{
Application.lowMemory -= LowMemory;
}
}
11 changes: 11 additions & 0 deletions Assets/Profiler.cs.meta

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

0 comments on commit e569c52

Please sign in to comment.