Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can I switch to windowed mode from WebGL?

Discussion in 'WebGL' started by Gigacee, Sep 9, 2019.

  1. Gigacee

    Gigacee

    Joined:
    Mar 4, 2015
    Posts:
    52
    I want to switch between full screen mode and windowed mode from WebGL.
    WebGL のゲーム中から、フルスクリーン/ウィンドウモードを切り替えたいと思っています。

    I could switch to full screen mode by writing:
    以下のように書くことで、フルスクリーンに切り替えることはできました。

    Code (JavaScript):
    1.  
    2. // jslib
    3. ChangeScreenMode: function () {
    4.     unityInstance.SetFullscreen(1);
    5. }
    6.  
    Code (CSharp):
    1.  
    2. // cs
    3. #if !UNITY_EDITOR && UNITY_WEBGL
    4.     [DllImport("__Internal")]
    5.     private static extern void ChangeScreenMode();
    6. #endif
    7.  
    8. #if !UNITY_EDITOR && UNITY_WEBGL
    9.     ChangeScreenMode();
    10. #endif
    11.  
    However, I don't know how to switch to windowed mode. What should I do?
    しかし、ウィンドウモードに切り替える方法が判りません。どうしたら良いでしょうか?
     
  2. kou-yeung

    kou-yeung

    Joined:
    Sep 5, 2016
    Posts:
    30
    you can check any element is fullscreen use
    ( 以下のコードでフルスクリーンしているエレメントを取得できます。なければ nullを返されます)
    Code (JavaScript):
    1. document.fullscreenElement
    https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/fullscreenElement

    and change your code to
    Code (CSharp):
    1. var fullscreen = (document.fullscreenElement === null)?1:0;
    2. unityInstance.SetFullscreen(fullscreen);
    i think this can switch full screen mode and windowed mode
    (あなたのサンプルを以下のように変更すれば切り替えができるかもしれません。時間取れないので試してませんが・・・)
     
    Last edited: Sep 19, 2019
  3. Gigacee

    Gigacee

    Joined:
    Mar 4, 2015
    Posts:
    52
    ありがとうございます!
    unityInstance.SetFullscreen(0);
    で、ウィンドウモードに切り替えられるのですね。
    以下のようにして、やりたいことを実現できました。

    Code (CSharp):
    1. #if !UNITY_EDITOR && UNITY_WEBGL
    2. [DllImport("__Internal")]
    3. private static extern void ChangeScreenMode(float num);
    4. #endif
    5.  
    6. void Start()
    7. {
    8.     fullscreenButton.OnClickAsObservable().Subscribe(_ =>
    9.     {
    10. #if !UNITY_EDITOR && UNITY_WEBGL
    11.         ChangeScreenMode(1f);
    12. #endif
    13.     });
    14.  
    15.     windowedButton.OnClickAsObservable().Subscribe(_ =>
    16.     {
    17. #if !UNITY_EDITOR && UNITY_WEBGL
    18.         ChangeScreenMode(0f);
    19. #endif
    20.     });
    21. }
    Code (JavaScript):
    1. ChangeScreenMode: function (num) {
    2.     unityInstance.SetFullscreen(num);
    3. }
     
    kou-yeung likes this.