[SOLVED] using "using" reserved word as a function, inside another function (OnSceneGUI)

Hello,

I’m following a tutorial on editor scripting (Editor Scripting - Unity Learn) and there’s a snippet of code that I find difficult to understand:

On line 9, the code uses the reserved keyword “using” but I don’t understand the purpose of using it here or even why is it being used similar as a function declaration ?

Any help will be deeply appreciated.

using UnityEditor;

[CustomEditor(typeof(Launcher))]
public class LauncherEditor : Editor
{
    void OnSceneGUI()
    {
        // HERE IS MY QUESTION: Why using "using" like this:
        using (new EditorGUI.DisabledGroupScope(!Application.isPlaying))
        {
            if (GUILayout.Button("Fire"))
            {
                // Do something
            }
        }
    }
}

It’s related to C# disposable pattern. It basically allows to use an object that will be destroyed when gone out of scope, or in this case, disable everything inside the block and stop disabling when exited.

Take a look at this using statement - ensure the correct use of disposable objects - C# reference | Microsoft Learn

Thank you so much for the explanation JoNax97 ! I now understand it. :slight_smile:

1 Like