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
1
Question by chillersanim · Apr 27, 2014 at 10:23 AM · c#editorruntimecomponent

Get if any component in gameObject has changed

Hello

I'm working on a managing component.
This component need to be informed if any component in the gameObject has been changed or a component has been added or deleted.

Is there a way to determine this, without using Update() and checking every frame?
Is there an event or a something similar which I could use that informs my component of such a change?

I would prefer a solution that works in the editor as well as in a runtime.
If this is not possible, then I would need a solution that at least works in the editor.

Greetings
Chillersanim

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by liortal · Apr 27, 2014 at 10:43 AM

  1. I am not familiar with any Unity API for getting informed (e.g: event based) when adding/removing components from a game object.

  2. What do you mean by "informed if any component has been changed" ? what would you consider as a change?

  3. For components added or removed, you can "simulate" this by wrapping Unity's API with a new wrapper API of your own that will expose an event in case of a Component added or removed. This can look similar to this (just an example):

public class ComponentExtensions {

     public event Action<GameObject> ComponentAdded;
     public event Action<GameObject> ComponentRemoved;
     
     public T AddComponent<T>(GameObject go) where T: Component
     {
         T result = go.AddComponent<T>();
        
         // Raise event
         ComponentAdded(go);
     } 
 }

All your code should use this API for adding/removing components from game objects. One downside to this technique though is that this is not possible for external code (since it won't call your wrapper API).

Comment
Add comment · Show 8 · 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 chillersanim · Apr 27, 2014 at 10:53 AM 0
Share

By "informed if any component has been changed" do I mean, that when the user or a script changes any value, I want to be informed, like the OnValidate() method but for other components and during runtime also.

Your code would be an idea, but I need it to work always.
It's because I want to write a dynamic loading system, and needs to know if the object needs to be reserialized and updated in the database or not.

Greetings
Chillersanim

avatar image liortal · Apr 27, 2014 at 11:50 AM 0
Share

A more (bizzare) option is to have some "code-weaving" that will post process your code and inject some custom code to all call sites of "AddComponent" or OnValidate. Not sure if something like that was ever performed...

avatar image chillersanim · Apr 27, 2014 at 12:09 PM 0
Share

I think, code weaving is to complicated and will create non readable code.
For people who are interested in code weaving:
http://stackoverflow.com/questions/189359/what-is-il-weaving

I will try another approach. Thank you for your help.

Greetings
Chillersanim

avatar image liortal · Apr 27, 2014 at 12:12 PM 0
Share

You can ask Unity to add this in future versions to their API in this site: http://feedback.unity3d.com/

avatar image chillersanim · Apr 27, 2014 at 12:22 PM 0
Share

Yes, that would be possible. But I am trying to create a workaround at the moment and if that works, then I will simply use the workaround.

Show more comments

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

22 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Generate Script in Editor Script and GetType() returns null. 3 Answers

Create a Component Script for Editor Only 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Distribute terrain in zones 3 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