Skip to content

Commit 7b85706

Browse files
committed
✨ Add the WebGLMemoryStats plugin
1 parent 8ac3054 commit 7b85706

9 files changed

Lines changed: 182 additions & 0 deletions

File tree

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/[Ll]ibrary/
2+
/[Tt]emp/
3+
/[Oo]bj/
4+
/[Bb]uild/
5+
/[Bb]uilds/
6+
/[Pp]roject[Ss]ettings/
7+
/Assets/AssetStoreTools*
8+
9+
# Visual Studio 2015 cache directory
10+
/.vs/
11+
12+
# Autogenerated VS/MD/Consulo solution and project files
13+
ExportedObj/
14+
.consulo/
15+
*.csproj
16+
*.unityproj
17+
*.sln
18+
*.suo
19+
*.tmp
20+
*.user
21+
*.userprefs
22+
*.pidb
23+
*.booproj
24+
*.svd
25+
*.pdb
26+
27+
# Unity3D generated meta files
28+
*.pidb.meta
29+
30+
# Unity3D Generated File On Crash Reports
31+
sysinfo.txt
32+
33+
build.log
34+
*.unitypackage

Assets/Plugins.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/WebGLMemoryStats.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using UnityEngine;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Kongregate {
5+
public class WebGLMemoryStats : MonoBehaviour {
6+
[Tooltip("Interval (in seconds) between log entries")]
7+
public uint LogIntervalSeconds = 15;
8+
9+
public static uint GetUsedMemorySize() {
10+
return GetTotalStackSize() + GetStaticMemorySize() + GetDynamicMemorySize();
11+
}
12+
13+
#if UNITY_WEBGL && !UNITY_EDITOR
14+
public static uint GetFreeMemorySize() {
15+
return GetTotalMemorySize() - GetUsedMemorySize();
16+
}
17+
18+
void Start() {
19+
InvokeRepeating("Log", 0, LogIntervalSeconds);
20+
}
21+
22+
private void Log() {
23+
var total = GetTotalMemorySize() / 1024 / 1024;
24+
var used = GetUsedMemorySize() / 1024 / 1024;
25+
var free = GetFreeMemorySize() / 1024 / 1024;
26+
Debug.Log(string.Format("WebGL Memory - Total: {0}MB, Used: {1}MB, Free: {2}MB", total, used, free));
27+
}
28+
29+
[DllImport("__Internal")]
30+
public static extern uint GetTotalMemorySize();
31+
32+
[DllImport("__Internal")]
33+
public static extern uint GetTotalStackSize();
34+
35+
[DllImport("__Internal")]
36+
public static extern uint GetStaticMemorySize();
37+
38+
[DllImport("__Internal")]
39+
public static extern uint GetDynamicMemorySize();
40+
#else
41+
public static uint GetFreeMemorySize() { return 0; }
42+
public static uint GetTotalMemorySize() { return 0; }
43+
public static uint GetTotalStackSize() { return 0; }
44+
public static uint GetStaticMemorySize() { return 0; }
45+
public static uint GetDynamicMemorySize() { return 0; }
46+
#endif
47+
}
48+
}

Assets/Plugins/WebGLMemoryStats/WebGLMemoryStats.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var MemoryStatsPlugin = {
2+
3+
GetTotalMemorySize: function() {
4+
return TOTAL_MEMORY; // WebGLMemorySize in bytes
5+
},
6+
7+
GetTotalStackSize: function() {
8+
return TOTAL_STACK;
9+
},
10+
11+
GetStaticMemorySize: function() {
12+
return STATICTOP - STATIC_BASE;
13+
},
14+
15+
GetDynamicMemorySize: function() {
16+
if (typeof DYNAMICTOP !== "undefined") {
17+
return DYNAMICTOP - DYNAMIC_BASE;
18+
} else {
19+
// Unity 5.6+
20+
return HEAP32[DYNAMICTOP_PTR >> 2] - DYNAMIC_BASE;
21+
}
22+
}
23+
};
24+
25+
mergeInto(LibraryManager.library, MemoryStatsPlugin);

Assets/Plugins/WebGLMemoryStats/WebGLMemoryStats.jslib.meta

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# Unity-WebGL-Utilities
22
Some helpful utilities for Unity WebGL games
3+
4+
## WebGLMemoryStats
5+
6+
This is a simple behavior that you can add to a persistent game object. It will periodically log WebGL memory statistics to the browser console to help you tune your WebGL memory size:
7+
8+
![](http://kong.dreamhosters.com/grabs/Default_unity_-_mtx-unity_-_WebGL__Personal___OpenGL_4_1__1E96A487.png)
9+
10+
![](http://kong.dreamhosters.com/grabs/Play_webgl-test__a_free_online_game_on_Kongregate_1E96A97A.png)

build.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
if [ -z "$UNITY_DIR" ]; then
4+
UNITY=/Applications/Unity/Unity.app/Contents/MacOS/Unity
5+
else
6+
UNITY="$UNITY_DIR/Unity.app/Contents/MacOS/Unity"
7+
fi
8+
9+
PROJECT_DIR=`pwd`
10+
echo "Using Unity Executable: $UNITY"
11+
12+
"$UNITY" -batchmode -projectPath "$PROJECT_DIR" -exportPackage Assets/Plugins \
13+
"$PROJECT_DIR/KongregateWebGLUtilities.unitypackage" -quit \
14+
-logFile "$PROJECT_DIR/build.log"

0 commit comments

Comments
 (0)