Unity Editor script in DLL

I’m am trying to create a dll (or any script) that doesn’t depend on the project’s compilation.

We have a file that has some constants which need to be auto-generated by our build script. The file is in gitignore, so when you pull the project, the menu that the build script adds (with the option to generate the constants file) doesn’t exist, because you are missing the file :slight_smile:

So the project doesn’t compile, because it requires a file to be generated, but the option that generates the file, doesn’t exist because the project doesn’t compile :slight_smile:

Is there any way to add a menu item that doesn’t depend on the project compilation ?


Yes, here are some steps to do it with Visual Studio.

// Create a new C#/Windows/Class Library project. Set the Framework to 3.5 
// (I don't remember which versions are supported but 3.5 should do fine).
// In solution explorer, add a reference to UnityEditor.dll and UnityEngine.dll
// by right clicking "References/Add Reference..."
// ... Which are found here: C:\Program Files\Unity\Editor\Data\Managed

using UnityEditor;
using UnityEngine;
using System.IO;

public static class GenerateConstants
    [MenuItem("Assets/Generate Constants")]
    public static void GenerateConstantsFile()
        string contents = "Some text etc";
        string filename = "autogenerated example.txt";

        string path = Path.Combine(Application.dataPath, filename);
        File.WriteAllText(path, contents);
        Debug.Log("Generated file at: " + path);

// Compile the dll file, and place it under Assets/Editor

You could do the same with MonoDevelop and it’s pretty similar, although I don’t feel like doing a step by step for it. Basically you just create a dll project, reference UnityEngine.dll and UnityEditor.dll. Code away. Compile. Place dll in Assets/Editor.