Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
3
Question by Paulius-Liekis · Dec 23, 2010 at 03:20 PM · editormonobehaviourcallback

How do I detect that MonoBehaviour is modified in the Editor?

I have a MonoBehaviour which builds a procedural mesh. I want to make a preview of it in the Editor, so I need a couple of callbacks:

  • when it's dropped into Editor, so I can build the mesh for the first time
  • when some field is modified, so I can rebuild the mesh

Could you tell me if these callbacks are available and how they are called? Or tell me if I'm supposed to do this in some other way.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by SteveFSP · Dec 23, 2010 at 04:23 PM

when it's dropped into Editor, so I can build the mesh for the first time

The callback you probably want is OnEnable and make sure the MonoBehavior is set to execute in the editor.

when some field is modified, so I can rebuild the mesh

There are no callbacks that track field changes. GUI.changed may be of use since it will trigger on changes to the inspector. But you may need to implement a custom inspector then use it in the OnInspectorGUI callback. (I haven't tried to use GUI.changed for a MonoBehaviour in the editor without a custom inspector.)

void OnInspectorGUI() {

 // Implement field GUI controls here.

 // Check to see if any of the field GUI controls resulted in a change.
 if (GUI.changed)
 {
     // Call your function to rebuild of mesh.
     // E.g.: RebuildMesh();
     // Also, tell the Unity editor that the object has changed and
     // needs to be saved when the scene is saved.
     EditorUtility.SetDirty(target);
 }

}

See also: Extending the Editor -> Making a Custom Editor.

Comment
Add comment · Show 3 · 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 Statement · Dec 23, 2010 at 06:51 PM 0
Share

While I can't find any other way of noticing when an object is instantiated, I still find having a custom script on the host object a bit awkward. Is there no other way to know if an object is added (from the editor scripts)?

avatar image Paulius-Liekis · Dec 23, 2010 at 09:13 PM 0
Share

Could you tell me which method on my $$anonymous$$onoBehaviour will be called after this "EditorUtility.SetDirty(target);"?

avatar image SteveFSP · Dec 24, 2010 at 04:48 AM 0
Share

Paulius: I've updated the example to be more plain. Calling SetDirty is for the benefit of the Unity Editor only. It lets the editor know the target has changed and needs to be saved.

avatar image
4

Answer by inewland · Mar 14, 2015 at 12:47 AM

Another options is to use the OnValidate() method. It will fire if any value in the inspector has changed. It doesn't tell you what has changed but with a few variables you could get the info you need.

This doesn't require [ExecuteInEditMode]!

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Editor script - any callback when target is first instantiated? 1 Answer

before and after apply prefab changes callback 0 Answers

Unity Editor crashing when editing scripts referenced from GameObjects 1 Answer

Save scene event when the user save the scene in the editor 1 Answer

The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges