jeffreylanters / react-unity-webgl

React Unity WebGL provides a modern solution for embedding Unity WebGL builds in your React Application while providing advanced APIs for two way communication and interaction between Unity and React.
https://react-unity-webgl.dev
Apache License 2.0
1.75k stars 162 forks source link

Dynamic devicePixelRatio not update resolution #559

Open kacperkurek opened 5 months ago

kacperkurek commented 5 months ago

Please avoid duplicates

Language and Compiler

TypeScript's built-in Compiler

What environment are you using?

Server Side Renderering

When does your problem occur?

When the Unity App is running

What does your problem relate to?

I don't know

React-Unity-WebGL Version

9.5.2

React Version

18.2.0

Unity Version

2021.3.33f1

What happened?

In game there is function that adjust devicePixelRatio to maintain 60fps, but canvas resolution is not updating after change of devicePixelRatio

Reproducible test case

const UnityGame = ({
  name,
  version,
  setProgress,
  setLoaded,
}: {
  name: string;
  version: string;
  setProgress: (progress: number) => void;
  setLoaded: (loaded: boolean) => void;
}) => {
  const buildUrl = useMemo(
    () =>
      `${process.env.NEXT_PUBLIC_BLOB_STORAGE_URL}/${name
        .toLowerCase()
        .replace(' ', '-')}`,
    [name]
  );
  const {
    unityProvider,
    isLoaded,
    loadingProgression,
    requestFullscreen,
    unload,
    UNSAFE__unityInstance,
    addEventListener,
    removeEventListener,
    sendMessage,
  } = useUnityContext({
    loaderUrl: `${buildUrl}/${version}.loader.js`,
    dataUrl: `${buildUrl}/${version}.data.br`,
    frameworkUrl: `${buildUrl}/${version}.framework.js.br`,
    codeUrl: `${buildUrl}/${version}.wasm.br`,
    companyName: 'Company',
    productName: name,
    productVersion: version,
    streamingAssetsUrl: '.',

    webglContextAttributes: {
      powerPreference: 2,
      premultipliedAlpha: true,
      preserveDrawingBuffer: true,
    },
  });

  useEffect(() => {
    setLoaded(isLoaded);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isLoaded]);

  const enterFullscreen = useCallback(() => {
    requestFullscreen(true);
  }, [requestFullscreen]);

  const [devicePixelRatio, setDevicePixelRatio] = useState(
    window.devicePixelRatio
  );

  const canvasRef = useRef<HTMLCanvasElement>(null);

  useEffect(() => {
    addEventListener('SetDPR', (dpr: any) => {
      setDevicePixelRatio(dpr);
    });
    return () => {
      removeEventListener('SetDPR', (dpr: any) => {
        setDevicePixelRatio(dpr);
      });
    };
  }, [addEventListener, removeEventListener, setDevicePixelRatio]);
  console.log('devicePixelRatio', devicePixelRatio);
  useEffect(() => {
    sendMessage(
      'DynamicDevicePixelRatioManager',
      'OnDPRUpdate',
      devicePixelRatio
    );
  }, [sendMessage, devicePixelRatio]);

  return (

        <div className="flex flex-1 items-center justify-center">
          <Unity
            ref={canvasRef}
            id="unity-canvas"
            tabIndex={1}
            unityProvider={unityProvider}
            className="max-h-full min-h-full min-w-full max-w-full outline-none"
            style={{
              visibility: isLoaded ? 'visible' : 'hidden',
            }}
            devicePixelRatio={devicePixelRatio}
          />
        </div>
  );
};

Would you be interested in contributing a fix?