Hi, I have a dilemma that I never knew how to solve:
I want to access the corresponding Editor-Script from my Runtime-Script (target).
This is how it looks like now:
public class MyClass : MonoBehaviour, IFeatureInterface {
public void DoStuff() {
[ ... ]
HelperClass.DoInelligentStuff();
[ ... ]
}
}
public static class HelperClass {
public static void DoInelligentStuff() {
if (Application.isPlaying) {
UnityEngine.Function();
}
#if UNITY_EDITOR
else {
UnityEditor.Function();
}
#endif
}
}
As you can see, I have to use a different functionality inside the editor, which utilizes functions from the UnityEditor namespace in the real code. This is because I can’t use the runtime code inside the DoStuff()
when in edit mode. The editor functionality is programmed by me and makes the runtime code work for us inside the editor.
In an extra Editor-Window I just search for all instances in the scene with the IFeatureInterface
and then call DoStuf()
on them.
But now, since the project grew and I’m trying to implement Assembly Definitions, I will now have to separate the code, there is no #if UNITY_EDITOR
hack possible anymore. But all possibilities that come to mind won’t work one way or the other.
I thought about adding an Editor-Script to MyClass, which implements an editor version of DoStuff() which then does the editor functionality instead of the current helper class doing it. But I can’t catch the function call in the editor script, it doesn’t know when DoStuff() is called inside the runtime script.
Another option would be to search all IFeatureInterface
an then don’t get MyClass
, but the corresponding MyClassEditor
script. I know I can get the runtime class with the “target” variable, but the other way around is not possible as far as I know. However this leads to a ton of duplicated code, that needs to be implemented on any class that implements the IFeatureInterface
…
It would still be great if the helper class would be able to solve this, because I would have to write a ton of duplicate code, but the two option above seem like the only possibilities, even if they suck in this case…
How would one overcome this dilemma? I really need to get this fixed, hope you have some cool suggestions on how to do this