Skip to content

Commit 294c8db

Browse files
feat: implement cache control for Unity assets and improve FPS counter rendering
1 parent 7d8fa88 commit 294c8db

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/game.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { useCallback, useEffect, useMemo, useState } from "react";
2-
import { Unity, useUnityContext, useUnityMetricsInfo } from "react-unity-webgl";
2+
import {
3+
Unity,
4+
useUnityContext,
5+
useUnityMetricsInfo,
6+
UnityCacheControlMode,
7+
} from "react-unity-webgl";
38
import "./game.css";
49

510
export function Game() {
611
const [score, setScore] = useState(0);
712
const [isStarted, setIsStarted] = useState(false);
813

14+
const cacheControl = useCallback((url: string): UnityCacheControlMode => {
15+
if (url.endsWith(".wasm") || url.endsWith(".data")) {
16+
return "immutable";
17+
}
18+
return "no-store";
19+
}, []);
20+
921
const {
1022
loadingProgression,
1123
unityProvider,
@@ -22,6 +34,7 @@ export function Game() {
2234
companyName: "Jeffrey Lanters",
2335
productName: "Crate Clicker",
2436
productVersion: "1.0.0",
37+
cacheControl: cacheControl,
2538
});
2639

2740
const { fps } = useUnityMetricsInfo(getMetricsInfo, {
@@ -72,7 +85,9 @@ export function Game() {
7285
/>
7386
</div>
7487
<div className="score-display">{score}</div>
75-
{!isNaN(fps) && <div className="fps-counter">{Math.round(fps)} FPS</div>}
88+
{fps && !isNaN(fps) && (
89+
<div className="fps-counter">{Math.round(fps)} FPS</div>
90+
)}
7691
<Unity
7792
unityProvider={unityProvider}
7893
devicePixelRatio={window.devicePixelRatio}

0 commit comments

Comments
 (0)