- Home /
How can you access scripts in a folder called "Editor" from elsewhere?
For example, access this:
class InsideEditorFolder {
public static void DoItToIt () {}
}
with this:
public class OutsideEditorFolder : MonoBehaviour {
[ContextMenu("Context Menu Item")] void ContextMenuItem () {
InsideEditorFolder.DoItToIt();
}
}
Here is a real-world example project (probably won't work right unless you have the Unity 3 beta). I assume this is an issue of namespaces, but I don't really know anything about that subject yet.
Answer by Lucas Meijer 1 · Aug 29, 2010 at 04:02 PM
As of Unity3 you can actually do this. However, the editorscript will not be available when you publish to your target platform, so it is up to you to properly use #if UNITY_EDITOR #endif blocks to make sure that code only gets included when you run the game in the editor.
I'm pretty sure that in order for it to work, you need to make sure the editorscript is in an earlier "compilation phase" than the runtime script. Putting your editorscript in Plugins/Editor/MyEditorScript.cs should do the trick. that said, you can also do whatever you want to do in your editorscript in your runtime script. You can just access UnityEditor.* and do whatever you feel like, again with the same caveat that it will not work when you publish, so you need to use the ifdefs to make sure the code is not included in builds.
That's good information, but not helping me. The entire point of the question is to organize things how I want. Your solution just involves making the organization even worse, and in fact, nonsensical, as it's not a plugin. Not to say it's something wrong with your methods, it's just sad that some weird workaround has to be undertaken.
You don't have to put it in the Plugins folder, you can also just have it in the same folder as your gamescript, as long as you properly use #if UNITY_EDITOR around the parts that talk to editor api
I know that it can be in the same folder; that's what I did in the example to which I linked. But I don't want it there. I want it to go a folder called Editor. Some things, like custom Inspectors, HAVE to go into a folder called Editor. So considering those folders are already created, I should be able to throw whatever I want in there, as they have such a useful name.
Answer by TowerOfBricks · Aug 29, 2010 at 02:56 PM
Hi Jessy
Well, you can't.
The scripts inside the "Editor" folder are supposed to be used only in the editor (like custom editors, custom windows, etc etc). Due to the compilation order (the editor folder scripts are compiled last), you can't access those scripts from outside the editor folder.
It might be possible that you could pass a delegate to the script outside the editor folder first, and then make that script call it when needed.
It might also be possible that you could just define the ContextMenu in the editor script, or that you could call:
[MyOtherScript.ContextMeny ("Random")]
But I haven't tried that though.
Obviously, a context menu is only going to be used in the Editor, so there needs to be a way to connect context menus with other scripts. I see no logic in allowing contextual menus to be present on $$anonymous$$onoBehaviors, and they should be done through some other means (not via a custom editor, because there should be an option to keep the default editor and just alter the contextual menu.)
Your answer
Follow this Question
Related Questions
Is there a way to see namespaces in Editor ? (complete class name) 0 Answers
Detecting scene save - replacement for AssetModificationProcessor.OnWillSaveAssets 1 Answer
A weird issue in Unity related to scripts and Editor. 1 Answer
Visual Studio Setup 2 Answers
How to detect if unity build scene for target platform or editor player 2 Answers