Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic devicePixelRatio not update resolution #559

Open
1 of 2 tasks
kacperkurek opened this issue Jun 25, 2024 · 0 comments
Open
1 of 2 tasks

Dynamic devicePixelRatio not update resolution #559

kacperkurek opened this issue Jun 25, 2024 · 0 comments
Assignees
Labels

Comments

@kacperkurek
Copy link

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?

  • yes, I would like to contribute a fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants