- Home /
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
Answer by liortal · Apr 27, 2014 at 10:43 AM
I am not familiar with any Unity API for getting informed (e.g: event based) when adding/removing components from a game object.
What do you mean by "informed if any component has been changed" ? what would you consider as a change?
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).
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
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...
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
You can ask Unity to add this in future versions to their API in this site: http://feedback.unity3d.com/
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.
Your answer
Follow this Question
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