- Home /
How do i get a callback every frame in edit mode
I dont want to use an Editor Window or ScriptableObject or MonoBehaviour. I just want to get a callback called every frame without having the user having to click a menu item or something like that.
Answer by Joachim Ante · Dec 22, 2010 at 01:39 PM
You can use the InitializeOnLoad attribute combined with static constructors.
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
class MyClass
{
static MyClass ()
{
EditorApplication.update += Update;
}
static void Update ()
{
Debug.Log("Updating");
}
}
Or simply put the EditorApplication.update code in void OnEnable () {
So you can skip the InitilizeOnLoad attribute.\n I assume you are using an editor script of some kind.
That only works if you have a ScriptableObject or $$anonymous$$onoBehaviour that has the OnEnable function. Thats not always the case, for this purpose we have the InitializeOnLoad attribute.
And it only works if the object with the OnEnable method is in an Editor script, since EditorApplication is an Editor class.
Hooray! Why is this attribute not documented? I spent half the day searching for something like this!
Answer by Topillogical · Feb 25, 2018 at 11:46 PM
@Razgrizzz You can call any methods you want in your Update() method, but you will need static references to them.
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
class MyClass
{
public static List<MyObject> list;
static MyClass ()
{
EditorApplication.update += Update;
list = new List<MyObject>();
}
public MyClass(){ // regular constructor where we add this to static list
MyClass.list.Add(this);
}
static void Update ()
{
Debug.Log("Updating");
foreach(MyClass mc in list){
mc.AnyPublicMethod();
}
}
public void AnyPublicMethod(){
print("Dude.");
}
}
Your answer
Follow this Question
Related Questions
DrawGizmo attribute: GizmoType arg with incompatible flag values 0 Answers
Editor script - any callback when target is first instantiated? 1 Answer
OnValidate() not called when stored ScriptableObject is called 0 Answers
How do I detect that MonoBehaviour is modified in the Editor? 2 Answers
When I Multi-select gameobjects and press 'delete' 'OnDestroy' func is not called? 0 Answers