I'm creating a manager object that controls a bunch of other objects at once.
I want to be able to restrict / expose parameters in the target object to the manager via a custom attribute. I have the following working:
[AttributeUsage(AttributeTargets.Field)]
public class ManagableAttribute : Attribute {}
public class TargetObject : MonoBehaviour
{
// public ObjectScript obj;
[Managable]public float var1;
public float var2;
}
Two variables in the target object, only one of which has the [Managable]
attribute.
Based on some other posts I found, in the editor script I have it searching the fields in the target script and identifying the ones with the [Managable]
attribute and printing them to the terminal:
[CustomEditor(typeof(TargetObject))]
[CanEditMultipleObjects]
public class ObjectManagerEditor : Editor
{
public override void OnInspectorGUI()
{
TargetObject targetObject = (TargetObject)target;
//// Manually creating the variable floatfield i need
//o.var1 = EditorGUILayout.FloatField("var1", o.var1);
Type targetType = targetObject.GetType();
FieldInfo[] objectFields = targetType.GetFields(BindingFlags.Instance | BindingFlags.Public);
foreach (var field in objectFields)
{
ManagableAttribute thisAttribute = (ManagableAttribute)Attribute.GetCustomAttribute(field, typeof(ManagableAttribute));
if (thisAttribute != null)
{
// Debug.Log(thisAttribute);
var f = field;
Debug.Log(field.Name);
}
}
}
}
Can I have it create an EditorGUILayout.FloatField
(or whatever type) in the loop for each variable it finds with the [Managable]
attribute?