- Home /
Unity Editor script in DLL
Hi, 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 :)
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 :)
Is there any way to add a menu item that doesn't depend on the project compilation ?
Thanks
Answer by Statement · Oct 08, 2015 at 07:55 PM
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);
AssetDatabase.Refresh();
}
}
// 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.
I tried that before posting here. I had no results. It compiled, with no errors, but the menu didn't show up. I checked all the references, there where no external references (project related), everything needed was in the dll, but nothing showed up in the menu. If i ran the code without a dll, the menu worked, if the project compiled. I thought i did something wrong. I'll check the dll again, maybe i missed something. Thanks for the confirmation :)
It worked. Thanks :) For others that are looking, it did not work to compile via "mcs" i had to create a mono develop project with .NET target 2.0 and add the assembiles for UnityEditor and UnityEngine. After that, you add your code, build and copy the dll and mdb into Assets/Editor.
Doesn't $$anonymous$$onoDevelop invoke mcs? I am not a $$anonymous$$onoDevelop/mcs expert at all but I thought $$anonymous$$D used mcs as its toolchain. Perhaps you can check the command line arguments in build log and compare it to those you used if you have a preference to use mcs for any reason.