Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Leepo · Feb 15, 2012 at 07:58 PM · editoreditor-scriptingcomponent

Event for loading component in the editor

Is there an event/callback for gameobjects/components that are loaded in a scene? All of this applies to editor time only, NOT during playmode or runtime.

I want to monitor a specific component type ("PhotonView") and need to check this type whenever it is loaded in a scene for the first time:

  • when a scene is loaded

  • whenever it is created in the scene (i.e. by duplicating a object with a PhotonView attached or by simply adding the component)

Is there some sort of "OnLoaded"/"OnCreated" method that I can use for my components? I believe that if there is a solution, it is probably not in the public API, but I'm fine with a non documented solution.

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 iwaldrop · Oct 29, 2014 at 07:50 AM 0
Share

I realize this is an old question, but there's not that any that I know of. What could be done, for future reference, is to setup a static class, decorated with the [InitializeOnLoad] Attribute, to monitor for instances of that Type in the Hierarchy. The simplest, though most CPU intensive, method would be polling the Hierarchy using GameObject.FindObjectsOfType. Any count greater than 0 would indicate that the type was found. I say simplest, though I don't know of any other effective alternatives. $$anonymous$$inda sucks to poll every frame, so you'd probably want to throttle it using an EditorCoroutine.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by IgorAherne · Aug 25, 2017 at 04:50 AM

executeInEditMode can sometimes be undesirable.

In that case you will need to open a pandora box and look into:

ISerializationCallbackReceiver (1-2 functions called when object is created or loaded/saved)

and into an approach discussed here (perhaps with some core singleton that already existed prior to your object birth, and will process its request next frame after your object is created/deserialized):

https://forum.unity3d.com/threads/update-class-in-editor-but-not-when-playing.484647/

Also, I would argue that it would be acceptable to declare an actual constructor in a Monobehavior, register for an update from EditorApplication and process query NEXT time update is ran for you.

 like this:
 #if UNITY_EDITOR
 //DONT DO ANYTHING IN CONSTRUCTOR EXCEPT CALLBACK SIGN-UP
 public MyMonobehaviorClass(){
 //since constructor is called multiple times, important to unsubscribe then re-subscribe, to avoid duplicates:
       EditorApplication.update -= myOneTimeUpdate;
       EditorApplication.update += myOneTimeUpdate;
 }
 
 void myOneTimeUpdate(){
      //we were bourn! PRocess, then unsubscribe
      EditorApplication.update -= myOneTimeUpdate;
 }
 #endif

Comment
Add comment · Show 1 · 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 zereda-games · Feb 11, 2019 at 07:05 PM 0
Share

Interesting little toolet you found there! the Link. I'm going to have to play with that. Upvoted for giving me new data.

avatar image
0

Answer by ifdef_ben · Nov 29, 2016 at 09:24 PM

I'd use the [ExecuteInEditMode] attribute on the monobehavior and then put my code in Awake(). Now this would still be called during playmode so I'd check that Application.isPlayingis false before. Something like this.

 [ExecuteInEditMode]
 public class Foo : MonoBehavior{  
     void Awake()
     {
         if(!Application.isPlaying)
             bar();
     }
 }

Comment
Add comment · Show 1 · 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 zereda-games · Feb 11, 2019 at 07:07 PM 0
Share

So [ExecuteInEdit$$anonymous$$ode] is placed before the class only? may be why i was getting error LOL. Don't fully understand it's usage personally.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Running a script when Unity starts. 3 Answers

How can i get the center of the entire editor window 0 Answers

get folder from selection array 4 Answers

Editor Scripting - Override Scaling 1 Answer


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