Hi everyone.
I like to add an gameobject to a EditorWindow like this.
public GameObject obj;
void OnGUI() {
obj = GameObject(10, 10, 300, 30, obj);
if (obj != null) {
var admittedClass = obj.GetComponent<...???...>();
// pseudo code -----------
var classReferencesList = admittedClass.ClassFields;
admittedClass.classReferencesList[0] would access myChoosenFloat;
}
}
GameObject GameObject(float x, float y, float w, float h, GameObject obj) {
return (GameObject)EditorGUI.ObjectField(new Rect(x, y, w, h), "", obj, typeof(GameObject));
}
Component ComponentObject(float x, float y, float w, float h, Component obj) {
return (Component)EditorGUI.ObjectField(new Rect(x, y, w, h), "", obj, typeof(Component));
}
After the a GameObject is committed to the slot, I like to know what classes and fields are used on the GameObject, and access them in the EditorWindow Script.
Would that possible in any way?
I ask because I saw an EditorScript that have a similar functionality, but was not able fiddle out how this is done.
Thank you
System.Reflection
Use reflection by calling ‘GetType’ on the GameObject, then called GetMembers on the type. This will give you all members as an array of MemberInfo.
1 Like
Great… thank you!
_go = GameObject(10, 10, 300, 20, _go);
if (_go != null) {
foreach (var script in _go.GetComponents<MonoBehaviour>()) {
foreach (var field in script.GetType().GetFields()) {
Debug.Log("field.ReflectedType CLASS : " + field.ReflectedType);
Debug.Log(System.String.Format("{0} = {1} = {2}", field.Name, field.MemberType, field.GetType()));
Debug.Log("field.Attributes = " + field.Attributes);
Debug.Log("field.GetValue(field) = " + field.GetValue(field));
Debug.Log("field.GetValue(field).GetType() = " + field.GetValue(field).GetType());
}
}
}
I was able to get the MemberInfo. But I was not able to compare the System.TypeCode of the field in foreach loop. I tried several Methods.
field.MemberType.GetTypeCode(); results Int32 at anytime. I didn’t find any other way to compare the TypeCode without using a string and this seems to be ugly.
- Is there any other solution to get GetType() without casting into string and produce GC?
- Further is it possible to store the reference to the selected variable while closing and reopen the editor? (OnEnable(), OnDestroy())
field.GetValue(field).GetType() is referenced to AnyVar.MyChoosenFloat.
Thanks a lot !
var fieldStruc = field.GetValue(field).GetType();
if (fieldStruc.Name.ToLower == "single") { }
//
1) if (field.???? == System.TypeCode.Single)
any ideas? Thank you.