How to call startcoroutine from editor script?

I’m using the method GenerateObjects to call it from the editor script when pressing the button.

it’s working fine when it’s in runtime mode. the game start and generating the objects then when finish when I click the button it’s generating new objects.

but now I want that if it’s not in runtime but if I’m in the editor without running the game when I’m pressing the button that it will also generate the objects depending on the given seconds.

mono script

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

public class InstantiateOnTerrain : MonoBehaviour
{
    private float terrainWidth;
    private float terrainLength;

    private float xTerrainPos;
    private float zTerrainPos;

    public Terrain terrain;
    public GameObject prefab;
    public int numberOfObjects;
    public float instantiateTime;
    public float yOffset;


    void Start()
    {
        //Get terrain size
        terrainWidth = terrain.terrainData.size.x;
        terrainLength = terrain.terrainData.size.z;

        //Get terrain position
        xTerrainPos = terrain.transform.position.x;
        zTerrainPos = terrain.transform.position.z;

        StartCoroutine(generateObjectsOnTerrain());
    }

    public void GenerateObjects()
    {
        StartCoroutine(generateObjectsOnTerrain());
    }

    private IEnumerator generateObjectsOnTerrain()
    {
        for (int i = 0; i < numberOfObjects; i++)
        {
            float objOffsetX = prefab.transform.localScale.x / 2;
            float objOffsetZ = prefab.transform.localScale.z / 2;

            float randX = UnityEngine.Random.Range(xTerrainPos + objOffsetX, xTerrainPos + terrainWidth - objOffsetX);
            float randZ = UnityEngine.Random.Range(zTerrainPos + objOffsetZ, zTerrainPos + terrainLength - objOffsetZ);
            float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

            //Apply Offset if needed
            yVal = yVal + yOffset;

            //Generate the Prefab on the generated position
            GameObject objInstance = (GameObject)Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);

            if (instantiateTime > 0)
            {
                yield return new WaitForSeconds(instantiateTime);
            }
        }
    }
}

editor script

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.TerrainTools;
using UnityEngine;

[CustomEditor(typeof(InstantiateOnTerrain))]
public class InstantiateOnTerrainEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        InstantiateOnTerrain onTerrain = (InstantiateOnTerrain)target;
        if(GUILayout.Button("Instiante Objects"))
        {
            onTerrain.GenerateObjects();
        }
    }
}

There’s a small Unity package called Editor Coroutines: About Editor Coroutines | Editor Coroutines | 1.0.0

Download that, read the docs, you’ll work it out.