How can i add a button to the inspector ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NodesGenerator : MonoBehaviour {

    public GameObject NodePrefab;
    public int NodesNumberX = 16;
    int NodesNumberY;
    
    void Generate()
    {
        for (int x = 0; x < NodesNumberX; x++)
        {
            for (int y = 0; y < NodesNumberY; y++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.localScale = new Vector3(4, 1, 4);
                cube.transform.position = new Vector3(x * 5, 0, y * 5);
            }
        }
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Generate Nodes"))
        {
            NodesNumberY = NodesNumberX;
            Generate();
        }
    }
}

When i attach the script to a gameobject i want to see in the script in the inspector:

The OnGui add the button to the game screen when running the game.
But i want to add the button to the Inspector for example like the button Add Component. This kind of button.

you can use a bool field and read it’s value on OnValidate, execute the action, and set the bool back to false

You need to create a custom inspector for your component.

building custom inspector
adding buttons to the custom inspector

GitHub - datsfain/EditorCools: Unity Editor Tools this repo contains multiple editor tools, one of them is buttons for Inspectors.

[EditorCools.Button]
private void DoSth() => Debug.Log("Button Clicked!");