How to create c# files as Editor Extension

I want to add a custom Editor “right-click” menu item. Just like the current right-click “Create C# Script”, only in my case I want to prompt the user for a name and then create several c# scripts saved into various folders.

I found the docs help for add the right-click menu item, but could not the help I need for creating the c# files. Anyone have suggestions to get me started?

Hi there,

It’s been a while since you posted this, so probably you’ve found a solution, but for future reference you
could go about it this way:

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


public class CreateScrpt {
  [MenuItem("Assets/Create Custom C# Class", false, -1)]
  static void Create() {
    // This returns the GUIDs of the selected folders/files
    var selectionGUIDs = Selection.assetGUIDs;
    // Nothing selected
    if(selectionGUIDs.Length == 0)
      return;

    var path = AssetDatabase.GUIDToAssetPath(selectionGUIDs[0]);
    Debug.Log(path);

    var content = @"
      using System;
   
      public class CustomClass {

      }

      ";

    var fullPath = path + "/CustomClass.cs";

    File.WriteAllText(fullPath, content);
    // Need to import the newly created file, otherwise it won't appear in the
    // editor.
    AssetDatabase.ImportAsset(fullPath);
  }
}

Looks great, but my created C# scripts is IL Code after creation. Cant edit, and do stuff.

Please don’t necro post, a 7 year old post is unlikely to help you. Create a new thread and explain in detail what you are trying to achieve and have done so far.

Thread locked

1 Like