- Home /
[RESOLVED]How to get all available parameters from target Component
I'm making a editor to count value from another component, but I need to get reference or access to all parameter of that component. Actually this function from new UI system is exactly what I want. Any idea how to do this? Thanks :)
Answer by fffMalzbier · Apr 16, 2015 at 09:50 AM
Sadly the documentation is not very helpful but here is how it works.
First you have to include the event namespace
using UnityEngine.Events;
Then you can define your event:
public UnityEvent myevent;
That should bring up the ui seen in your image.
If you like to invoke your event you can do it like this:
if (myevent != null) {
myevent.Invoke();
}
http://docs.unity3d.com/ScriptReference/Events.UnityEvent.html
if you like to use events with parameter you have to define them via a custome class like this
[System.Serializable]
public class MyIntEvent : UnityEvent<int>
{
}
Then you can create a variable of that custom event type
public MyIntEvent m_MyEvent;
Answer by frarees · Apr 16, 2015 at 09:20 AM
If you want to access class members (i.e. fields, properties, methods...) you shall use reflection (System.Reflection namespace). Check MSDN docs.
Here's a simple example that would get all the public fields defined in a Test
class.
FieldInfo[] fields = typeof (Test).GetFields ();
This way is more customizable for Editor script, thank you so much! I never know this before.
Answer by Rosa-Gao · Apr 17, 2015 at 09:59 AM
Both @fffMalzbier and @frarees solution work out very well! UnityEvent way is much easier, But FieldInfo is more customizable when making Editor script. Very thanks both of you, I never know this before :)