maximized window from code

With a toggle how could I put my 3d game in full screen and in a maximized window from code?

To achieve this, you can use the Screen.fullScreen property to determine if the game is currently running in fullscreen mode. Here’s an example script that demonstrates how to switch between fullscreen and windowed modes:

using UnityEngine;

public class FullscreenToggle : MonoBehaviour
{
    private bool _isFullscreen;

    private void Awake()
    {
        _isFullscreen = Screen.fullScreen;
    }

    public void ToggleFullscreen()
    {
        _isFullscreen = !_isFullscreen;
        SetFullscreen(_isFullscreen);
    }

    private void SetFullscreen(bool value)
    {
        Screen.fullScreen = value;
#if UNITY_EDITOR
        EditorWindow.GetWindowWithFocus().maximized = false;
#endif
    }
}

Explanation:
In this script, we declare a boolean variable _isFullscreen to keep track of whether the game is currently running in fullscreen mode. In the Awake() method, we initialize this variable based on the initial fullscreen setting.

The ToggleFullscreen() method toggles the fullscreen status by negating the current value of _isFullscreen. The SetFullscreen() method then sets the Screen.fullScreen property to the new value.

Finally, we add #if UNITY_EDITOR checks around the line where we disable editor windows maximization because it doesn’t exist in builds.

Usage:
Attach this script to any gameobject in your scene and call the ToggleFullscreen() method whenever you want to switch between fullscreen and windowed modes. For example, you can bind a key to this method in the Input Manager settings.