• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
16
Question by Joachim Ante · Dec 22, 2010 at 01:38 PM · editorcallback

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Feb 26, 2018 at 03:48 PM 0
Share

So, what do you mean by a "frame in edit mode"?

2 Replies

· Add your reply
  • Sort: 
avatar image
23

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");
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TowerOfBricks · Dec 22, 2010 at 03:43 PM 1
Share

Or simply put the EditorApplication.update code in void OnEnable () {

avatar image TowerOfBricks · Dec 22, 2010 at 03:44 PM 0
Share

So you can skip the InitilizeOnLoad attribute.\n I assume you are using an editor script of some kind.

avatar image Joachim Ante · Dec 30, 2010 at 12:19 PM 2
Share

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.

avatar image yoyo · Jan 31, 2011 at 09:26 PM 0
Share

And it only works if the object with the OnEnable method is in an Editor script, since EditorApplication is an Editor class.

avatar image Orion 1 · Mar 21, 2011 at 10:49 AM 4
Share

Hooray! Why is this attribute not documented? I spent half the day searching for something like this!

Show more comments
avatar image
0

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.");
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Editor Shutdown Callback exists? 1 Answer

before and after apply prefab changes callback 0 Answers

How to show an icon in Hierarchy view 5 Answers

How to handle a "remove component" from a prefab - Editor callbacks 1 Answer

Event or Callback for when Files are moved. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges