unity find all variable of a gameobject code example
Example: unity find all variables in a class
public class PropertyReader {
public struct Variable {
public string name;
public Type type;
}
private Variable[] _fields_cache;
private Variable[] _props_cache;
public Variable[] getFields() {
if (_fields_cache == null)
_fields_cache = getFields (this.GetType ());
return _fields_cache;
}
public Variable[] getProperties() {
if (_props_cache == null)
_props_cache = getProperties (this.GetType ());
return _props_cache;
}
public object getValue(string name) {
return this.GetType().GetProperty(name).GetValue(this,null);
}
public void setValue(string name, object value) {
this.GetType().GetProperty(name).SetValue(this,value,null);
}
public static Variable[] getFields(Type type) {
var fieldValues = type.GetFields ();
var result = new Variable[fieldValues.Length];
for (int i = 0; i < fieldValues.Length; i++) {
result[i].name = fieldValues[i].Name;
result[i].type = fieldValues[i].GetType();
}
return result;
}
public static Variable[] getProperties(Type type) {
var propertyValues = type.GetProperties ();
var result = new Variable[propertyValues.Length];
for (int i = 0; i < propertyValues.Length; i++) {
result[i].name = propertyValues[i].Name;
result[i].type = propertyValues[i].GetType();
}
return result;
}
}