x


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.

more ▼

asked Aug 29 '10 at 02:36 PM

Jessy gravatar image

Jessy
15.7k 72 95 198

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Aug 29 '10 at 04:02 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

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.

Aug 30 '10 at 10:36 PM Jessy

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

Aug 31 '10 at 01:42 PM Lucas Meijer 1 ♦♦

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.

Sep 01 '10 at 12:13 AM Jessy
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 29 '10 at 02:56 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

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 MonoBehaviors, 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.)

Aug 29 '10 at 03:29 PM Jessy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x351
x67

asked: Aug 29 '10 at 02:36 PM

Seen: 2487 times

Last Updated: Aug 29 '10 at 02:36 PM