- Home /
Is there a method that gets constantly updated in the editor?
Hey folks.
I finally found out how to use the 3rd and 4th MouseButtons, but now I need to constantly check for them.
Problem is I don't know of any method besides [ExecuteInEditMode], but actually I really don't want to use a GameObject and MonoBehaviour just for that ( just turned all of my Singletons to ScriptableObjects).
isn't there some magical Method or Class out there, that could help me out?
Thanks
Answer by Tobychappell · May 26, 2018 at 08:27 PM
UnityEditor.EditorApplication.update
https://docs.unity3d.com/ScriptReference/EditorApplication-update.html
[UnityEditor.InitializeOnLoad] // <<< IMPORTANT
public class TestClass
{
static TestClass()
{
UnityEditor.EditorApplication.update += MyMethod;
}
private static void MyMethod()
{
Debug.Log("MyMethod!");
}
}
You may want to wrap the whole script in a preprocessor
[hashsymbol]IF UNITY_EDITOR
...
[hashsymbol]ENDIF to avoid build errors
Uhm he specifically ask for an in-editor usage. So we talk about an editor class anyways. So the preprocessor tags are not required as long as the script is inside an editor folder.
I'm just going off what was in the question without assu$$anonymous$$g things:
@dCalle asked: "Is there a method that gets constantly updated in the editor?"
This is 'in-editor' usage as in "the" UnityEditor application.
dCalle said he didn't want to be using a GameObject or $$anonymous$$onoBehaviour.
An Editor's OnSceneGUI method is only called if it is set to be a custom editor of another $$anonymous$$onoBehaviour that is on a GameOebject that is in the current scene and has to be selected and there is mouse activity inside the SceneView.
An Editor class is usually the way to go, but without information on purpose i can only advise based on the requirements.
Nice, this looks good. Actually I went for EditorWindow, but this method looks way better. I check it out tomorrow. Thanks Buddy ;-) But one Question: When do you prefer InitializeOnLoad over DidReloadScripts? Ah and yeah, I forgot to tell you but It should've been an editor Script. But if I ever intent to use a combo, I know where to look ;-)
Im not sure what you mean by DidReloadScripts, unless you meant Editor Scripts?, This can be used(is normally used) with editor scripts. InitializeOnLoad will initialize classes when Unity loads, so this will call the static constructor of a class, Unity docs say it loads editor classes but this proves that it doesn't have to be an editor class. This can be when the UnityApplication opens and when Run is pressed, and maybe when the code is recompiled..
I found this interesting as it opens up possibilities to be able to get an update regardless of what I was doing in editor and don't need to rely on having a monobehavior to have an editor for, like as if it was a background progress in Unitys Application.
Why you might want this over an editor class would depend on if the functionality was involved with a particular system or component or if you wanted to be completely abstracted away from Unity components. ExecuteInEdit$$anonymous$$ode is another way but then you need a game object with that script in every scene, perfor$$anonymous$$g editor stuff in the game environment, which will end up in your game if you're not careful.
Alright thanks for the info. I thought of EditorScripts. And just thought I wasted the last couple of days circumventing the need for DidReloadScripts. But since InitilaizeOnLoad only calls the static constructor (which is easy-peasy) I'm kinda happy having found a (tiresome) solution to get all non-static, non-$$anonymous$$onoBehaviour/ScriptableObject (field)objects and call their method (provided by an interface) on recompile.
haha still didn't check out your solution above, but I'm probably gonna do that... today^^
Still a huge thanks for your support and nice talking to you ;-)
Answer by Lost_Syndicate · May 24, 2018 at 05:52 PM
use this:
[ExecuteInEditMode]
public class PrintAwake : MonoBehaviour
{
void Awake()
{
Debug.Log("Editor causes this Awake");
}
void Update()
{
Debug.Log("Editor causes this Update");
}
}
Found Here
Note you can remove MonoBehaviour
Nope, doesn't work without being a $$anonymous$$onoBehaviour...
Your answer
Follow this Question
Related Questions
How to use "SerializeObject" with an object which doesn't derive from Object? 1 Answer
Display Custom Inspectors for each class in a List<> 1 Answer
Release Note tracking for specific platforms such as WebGL 0 Answers
Unity 2020.2 crashes when loading my Unity 2020.1 project 0 Answers
Unity Custom Inspector - Resources.LoadAll() returns 0 element. 1 Answer