// Handles gui layout public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Return error if (property.serializedObject == null) { string missingText = GetLocalizedText(property, LocalizedMissingKey); WitEditorUI.LayoutErrorLabel(missingText); return; } // Show foldout if desired string titleText = GetLocalizedText(property, LocalizedTitleKey); if (FoldoutEnabled) { property.isExpanded = WitEditorUI.LayoutFoldout(new GUIContent(titleText), property.isExpanded); if (!property.isExpanded) { return; } } // Show title only else { WitEditorUI.LayoutLabel(titleText); } // Indent GUILayout.BeginVertical(); EditorGUI.indentLevel++; // Pre fields OnGUIPreFields(position, property, label); // Iterate all subfields WitPropertyEditType editType = EditType; const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; Type fieldType = fieldInfo.FieldType; if (fieldType.IsArray) { fieldType = fieldType.GetElementType(); } FieldInfo[] subfields = fieldType.GetFields(flags); for (int s = 0; s < subfields.Length; s++) { FieldInfo subfield = subfields[s]; if (ShouldLayoutField(property, subfield)) { LayoutField(s, property, subfield, editType); } } // Post fields OnGUIPostFields(position, property, label); // Undent EditorGUI.indentLevel--; GUILayout.EndVertical(); }
// Draw a specific property protected virtual void LayoutField(int index, SerializedProperty property, FieldInfo subfield, WitPropertyEditType editType) { // Begin layout GUILayout.BeginHorizontal(); // Get label content string labelText = GetLocalizedText(property, subfield.Name); GUIContent labelContent = new GUIContent(labelText); // Determine if can edit bool canEdit = editType == WitPropertyEditType.FreeEdit || (editType == WitPropertyEditType.LockEdit && editIndex == index); bool couldEdit = GUI.enabled; GUI.enabled = canEdit; // Cannot edit, just show field SerializedProperty subfieldProperty = property.FindPropertyRelative(subfield.Name); if (!canEdit && subfieldProperty.type == "string") { // Get value text string valText = subfieldProperty.stringValue; if (string.IsNullOrEmpty(valText)) { valText = GetDefaultFieldValue(property, subfield); } // Layout key WitEditorUI.LayoutKeyLabel(labelText, valText); } // Can edit, allow edit else { GUILayout.BeginVertical(); LayoutPropertyField(subfield, subfieldProperty, labelContent, canEdit); GUILayout.EndVertical(); } // Reset GUI.enabled = couldEdit; // Lock Settings if (editType == WitPropertyEditType.LockEdit) { // Is Editing if (editIndex == index) { // Clear Edit if (WitEditorUI.LayoutIconButton(WitStyles.ResetIcon)) { editIndex = -1; string clearVal = ""; if (subfieldProperty.type != "string") { clearVal = GetDefaultFieldValue(property, subfield); } SetFieldStringValue(subfieldProperty, clearVal); GUI.FocusControl(null); } // Accept Edit if (WitEditorUI.LayoutIconButton(WitStyles.AcceptIcon)) { editIndex = -1; GUI.FocusControl(null); } } // Not Editing else { // Begin Editing if (WitEditorUI.LayoutIconButton(WitStyles.EditIcon)) { editIndex = index; GUI.FocusControl(null); } } } // End layout GUILayout.EndHorizontal(); }