dimanche 21 mars 2021

What type does nameof operator take in c#

I'm writing some editor UI code in Unity and am calling nameof() frequently to get string representations of relative property paths from the object I am editing.

my code currently looks like this

EditorGUI.PropertyField( // draw UI
     new Rect(rect.x+ currentOffset, rect.y, 110, EditorGUIUtility.singleLineHeight), // positioning
     element.FindPropertyRelative(nameof(LogicalFuzzyRule.predicate1.input)), // **get the property**
     GUIContent.none // nothing else
     );  


EditorGUI.PropertyField(
     new Rect(rect.x+ currentOffset + 110, rect.y, 60, EditorGUIUtility.singleLineHeight),
     element.FindPropertyRelative(nameof(LogicalFuzzyRule.predicate1.isOrIsNot)),
     GUIContent.none
     ); 

EditorGUI.PropertyField(
    new Rect(rect.x+ currentOffset + 170, rect.y, 40, EditorGUIUtility.singleLineHeight),
    element.FindPropertyRelative(nameof(LogicalFuzzyRule.predicate1.state)),
    GUIContent.none
    );

I would really like to be able to refactor this to pass in the start of the path like

var predicate = LogicalFuzzyRule.predicate1;

EditorGUI.PropertyField( // draw UI
     new Rect(rect.x+ currentOffset, rect.y, 110, EditorGUIUtility.singleLineHeight), // positioning
     element.FindPropertyRelative(nameof(predicate.input)), // **get the property**
     GUIContent.none // nothing else
     );  

and pass in the value of predicate via a function. Is this possible at all? does it even make sense to break up this call?

The code I would like to write does not compile of course, what type would the rhs of the 'predicate' assignment need to be in order for this to work?





Aucun commentaire:

Enregistrer un commentaire