Skip to content

Instantly share code, notes, and snippets.

@michaelbartnett
Last active August 29, 2015 14:05
Show Gist options
  • Save michaelbartnett/02ebfce83ae93cbf741a to your computer and use it in GitHub Desktop.
Save michaelbartnett/02ebfce83ae93cbf741a to your computer and use it in GitHub Desktop.
mono sadness
//
// EppyEditor.PropertyField is supposed to be a replacement for functions like
// EditorGUILayout.FloatField and EditorGUILayout.TextField except it handles
// multi-object editing correctly without the programmer having to think about
// it, and chooses the correct GUI function based on the type paramter 'T'.
//
// This is different from EditorGUILayout.PropertyField in that (A) you get the
// result back, (B) it doesn't print a label automatically (doable already with
// propertyfield, but not if you want it to return an exact type) and (C) it allows
// you to pass in different function refs to tweak how it works.
//
// Aside from a SerializedProperty/SerializedObject+string, it needs three
// things to work: getter function, setter function, and drawer function.
//
// I guess it's sort of re-implementing PropertyDrawers. I don't care. This is
// for use in custom inspectors.
//
// If any of the function ref params are null, defaults will be chosen. So I
// made all the function ref arguments optional:
//
public static T PropertyField<T>(
SerializedProperty serializedProperty,
Func<SerializedProperty, T> getter=null,
Action<SerializedProperty, T> setter=null,
Func<T, T> drawer=null)
//
// I want to change the drawer function slightly, and include my own prefix
// label, because I can.
//
// So we can use named arguments to write nice uncluttered code, right?
// Nope. Unhandled Exception: Mono.CSharp.InternalErrorException: Internal error
//
EppyEditor.PropertyField<string>(
vehicleTypeProp,
drawer: s => {
// var defaultDrawer = EppyEditor.GetDefaultFieldDrawer<string>();
EditorGUILayout.LabelField("A LABEL HOW NEATO (" + s + "): ");
// return defaultDrawer(s);
return s;
});
//
// Okay, use named arguments, but actually include all args in the right order?
// Still does not work. Still InternalErrorException.
//
EppyEditor.PropertyField<string>(
vehicleTypeProp,
getter: null,
setter: null,
drawer: s => {
// var defaultDrawer = EppyEditor.GetDefaultFieldDrawer<string>();
EditorGUILayout.LabelField("A LABEL HOW NEATO (" + s + "): ");
// return defaultDrawer(s);
return s;
});
//
// This is the only way.
// I give up. You win.
// I'll make an IShittyPropertyManipulationInterface.
//
EppyEditor.PropertyField<string>(
vehicleTypeProp,
null,
null,
s => {
// var defaultDrawer = EppyEditor.GetDefaultFieldDrawer<string>();
EditorGUILayout.LabelField("A LABEL HOW NEATO (" + s + "): ");
// return defaultDrawer(s);
return s;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment