Dynamic Device Pixel Ratio
In the following example, we'll render our Unity Application using a device pixel ratio which matches the device pixel ratio of the browser. We'll create an event listener using a media matcher which will update the device pixel ratio of the Unity Application when the device pixel ratio changes.
App.jsx
import React, { useState, useEffect } from "react";
import { Unity, useUnityContext } from "react-unity-webgl";
function App() {
const { unityProvider, isLoaded, loadingProgression } = useUnityContext({
loaderUrl: "build/myunityapp.loader.js",
dataUrl: "build/myunityapp.data",
frameworkUrl: "build/myunityapp.framework.js",
codeUrl: "build/myunityapp.wasm",
});
// We'll use a state to store the device pixel ratio.
const [devicePixelRatio, setDevicePixelRatio] = useState(
window.devicePixelRatio
);
useEffect(
function () {
// A function which will update the device pixel ratio of the Unity
// Application to match the device pixel ratio of the browser.
const updateDevicePixelRatio = function () {
setDevicePixelRatio(window.devicePixelRatio);
};
// A media matcher which watches for changes in the device pixel ratio.
const mediaMatcher = window.matchMedia(
`screen and (resolution: ${devicePixelRatio}dppx)`
);
// Adding an event listener to the media matcher which will update the
// device pixel ratio of the Unity Application when the device pixel
// ratio changes.
mediaMatcher.addEventListener("change", updateDevicePixelRatio);
return function () {
// Removing the event listener when the component unmounts.
mediaMatcher.removeEventListener("change", updateDevicePixelRatio);
};
},
[devicePixelRatio]
);
return (
<Unity
unityProvider={unityProvider}
style={{ width: 800, height: 600 }}
devicePixelRatio={devicePixelRatio}
/>
);
}