[6.4 Linux 2D]-Clear Areas Outside The Rendering When Resizing The Game's Windows-Currently See Flashing Artifacts?

Hi,

Completely new to Unity Engine version 6.4 on CachyOS Linux KDE Plasma.
Our game runs in a resizable window (640x360) keeping aspect ratio.
When we maximize the game’s window and then restore, there are graphic artifacts flashing?
Talking about the area that is not drawn to.
How to clear the areas outside the rendering in a resizable window C# code?
Let us know, thank you!

SS

Screenshot below shows the artifact:

We solved it with below C# line before when the Camera is resized:

GL.Clear(true, true, Color.black);

But read somewhere that the above is depreciated?

Is there a newer way to clear the entire screen in Unity v6.4?

Check the camera settings. For a 2D game like this it should have Background Type set to Solid Color (or Skybox), and the chosen color must be fully opaque (max alpha value).

If that’s not it, check if this occurs on other OS. CachyOS is not officially supported by Unity, only Ubuntu is in versions 22 and 24.

Hi,

NOTE: Checked the camera setting, all is correct.

It is solved with the line we show above…
But we read somewhere that the “GL.Clear()” call is depreciated.
Is there a newer replacement for “GL_Clear()” ?

We run Linux on our development computers.
We also have an M2 Mac Mini for testing, will boot that up later and will test.
We have Windows 11 Pro in a VirtualBox VM under Linux, but the performance will always be terrible in that.

Thanks for your help!

SS

According to AI the recommendation is command buffers:

using UnityEngine;
using UnityEngine.Rendering;

public class ResizeHandler : MonoBehaviour
{
    private CommandBuffer clearBuffer;

    void Start()
    {
        clearBuffer = new CommandBuffer();
        clearBuffer.name = "ResizeClear";
        // Clear params: (clearDepth, clearColor, backgroundColor)
        clearBuffer.ClearRenderTarget(true, true, Color.black);
    }

    void OnPreRender()
    {
        // optional: Check if window resized this frame
        // and only if so execute the clear immediately before cameras draw
        // to avoid any performance impact during normal operations
        Graphics.ExecuteCommandBuffer(clearBuffer);
    }
}

Hi,

Implemented your source into our project.
It builds and runs with no errors or warning in console, but still same flashing artifacts
when maximizing the game window and then restoring the game window?

C# source code on our side is below, are we doing something wrong?

SS

using UnityEngine;
using UnityEngine.Rendering;

public class ForceAspectRatio : MonoBehaviour
{
    public float targetAspectRatio = 16f / 9f;
    Camera camera;

    private CommandBuffer clearBuffer;

    void Start()
    {
        //Screen.SetResolution(640, 360, FullScreenMode.Windowed);
        camera = GetComponent<Camera>();
        SetCameraAspectRatio();

        clearBuffer = new CommandBuffer();
        clearBuffer.name = "ResizeClear";
        // Clear params: (clearDepth, clearColor, backgroundColor)
        clearBuffer.ClearRenderTarget(true, true, Color.black);
    }

    void Update()
    {
        SetCameraAspectRatio();
    }

    void OnPreRender()
    {
        // optional: Check if window resized this frame
        // and only if so execute the clear immediately before cameras draw
        // to avoid any performance impact during normal operations
        Graphics.ExecuteCommandBuffer(clearBuffer);
    }

    void SetCameraAspectRatio()
    {
        float windowAspect = (float)Screen.width / (float)Screen.height;
        float scaleHeight = windowAspect / targetAspectRatio;

        Rect rect = camera.rect;

        if (scaleHeight < 1.0f)
        {
            // Pillarbox (black bars on sides)
            rect.width = 1.0f;
            rect.height = scaleHeight;
            rect.x = 0;
            rect.y = (1.0f - scaleHeight) / 2.0f;
        }
        else
        {
            // Letterbox (black bars top/bottom)
            float scaleWidth = 1.0f / scaleHeight;
            rect.width = scaleWidth;
            rect.height = 1.0f;
            rect.x = (1.0f - scaleWidth) / 2.0f;
            rect.y = 0;
        }

        //GL.Clear(true, true, Color.black);

        camera.rect = rect;
    }
}