Is there a way to preload a unity game using nextjs? #504
Replies: 2 comments 1 reply
-
I'm working on implementing this in my NextJS 14 (app router) project. Did you ever get it working? In theory having the game component in the layout should preserve it across navigations:
|
Beta Was this translation helpful? Give feedback.
-
Hi! 👋 I faced a very similar situation while integrating a Unity WebGL game into my Next.js project — where I also wanted to preload the game and avoid reloading it when navigating between pages. ✅ Here's a better approach than using display: none:
Render Unity once and keep it hidden or off-screen using CSS transform/positioning Control visibility with state, but don’t unmount the component
Create a layout like app/unity-layout.tsx (if using App Router) Keep inside that layout so it persists between child routes Use CSS like opacity: 0; pointer-events: none instead of display: none
Use shallow routing or local state to simulate /welcome, /tutorial, etc. Load Unity once, and show/hide pages using internal state ✅ You can also check out this live demo I built: It’s a physics-based Unity WebGL browser game with Next.js — and I use a persistent preload strategy to avoid reloading the game. Let me know if you'd like me to share some working code or a GitHub template! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Im trying to build a nextjs project with an unity game, there are some pages (/welcome page , /tutorial page and /information page) that the user will go before going to the game, so I want to preload the game component and have it ready when the user goes to the game page (/game).
I tried to add the game with a display none to the nextjs layout but every time I navigate to another page the game gets reloaded.
I was think is to have a SPA where I will show all the pages on the same url, but I just want to double check there is a another way to achieve this.
Beta Was this translation helpful? Give feedback.
All reactions