public override void OnGUI(Rect _Position, SerializedProperty _Item, GUIContent _Label) { Rect rect = new Rect(_Position); rect.height = MuffinDevGUI.LINE_HEIGHT; MuffinDevGUI.ComputeLabelledFieldRects(rect, out Rect labelRect, out Rect fieldRect); // Key field SetKey(_Item, EditorGUI.TextField(labelRect, GetKey(_Item))); // Type label EditorGUI.LabelField(fieldRect, $"({ValueType.Name})"); BlackboardAssetDemo.PlayerData data = GetValue(_Item); EditorGUI.indentLevel++; { // Name field rect.y += rect.height + MuffinDevGUI.VERTICAL_MARGIN; rect = EditorGUI.IndentedRect(rect); data.name = EditorGUI.TextField(rect, "Name", data.name); // Score field rect.y += rect.height + MuffinDevGUI.VERTICAL_MARGIN; data.score = EditorGUI.IntField(rect, "Score", data.score); } EditorGUI.indentLevel--; // Save changes SetValue(_Item, data); }
public override void OnGUI(Rect _Position, SerializedProperty _Item, GUIContent _Label) { MuffinDevGUI.ComputeLabelledFieldRects(_Position, out Rect labelRect, out Rect fieldRect); // Key field SetKey(_Item, EditorGUI.TextField(labelRect, GetKey(_Item))); // Value field Vector2 currentValue = GetValue(_Item); Vector2 newValue = EditorGUI.Vector2Field(fieldRect, string.Empty, currentValue); if (currentValue != newValue) { SetValue(_Item, newValue); } }
/// <summary> /// Draws the Blackboard property GUI. /// </summary> public override void OnGUI(Rect _Position, SerializedProperty _Property, GUIContent _Label) { // Avoid strange behaviours when multi-selecting elements if (_Property.hasMultipleDifferentValues) { EditorGUI.PropertyField(_Position, _Property); return; } SerializedProperty serializedDataList = _Property.FindPropertyRelative(SERIALIZED_DATA_LIST_PROP); // Draw box and header Rect rect = new Rect(_Position); GUI.Box(_Position, "", MuffinDevGUI.ReorderableListBoxStyle); rect.height = MuffinDevGUI.LINE_HEIGHT; GUI.Box(rect, _Property.displayName, MuffinDevGUI.ReorderableListHeaderStyle); Rect buttonRect = new Rect(rect); buttonRect.width = Mathf.Clamp(rect.width * 40 / 100, MIN_ADD_ENTRY_BUTTON_WIDTH, MAX_ADD_ENTRY_BUTTON_WIDTH); buttonRect.x = rect.x + rect.width - MuffinDevGUI.HORIZONTAL_MARGIN - buttonRect.width; buttonRect.height -= MuffinDevGUI.VERTICAL_MARGIN; buttonRect.y += MuffinDevGUI.VERTICAL_MARGIN; DrawAddEntryButton(buttonRect, serializedDataList); // Setup layout rect.height = MuffinDevGUI.LINE_HEIGHT; rect.y += MuffinDevGUI.LINE_HEIGHT + MuffinDevGUI.VERTICAL_MARGIN * 2; rect.width -= MuffinDevGUI.HORIZONTAL_MARGIN * 2; rect.x += MuffinDevGUI.HORIZONTAL_MARGIN; // For each Blackboard entry for (int i = 0; i < serializedDataList.arraySize; i++) { Rect itemRect = new Rect(rect); itemRect.width = itemRect.height; // Draw "remove entry" button if (GUI.Button(itemRect, EditorIcons.IconContent(EEditorIcon.Close, "Deletes this entry from the Blackboard"), MuffinDevGUI.PropertyFieldButtonStyle)) { serializedDataList.DeleteArrayElementAtIndex(i); return; } itemRect.x += itemRect.width + MuffinDevGUI.HORIZONTAL_MARGIN; itemRect.width = rect.width - itemRect.width - MuffinDevGUI.HORIZONTAL_MARGIN; SerializedProperty item = serializedDataList.GetArrayElementAtIndex(i); Type dataType = Type.GetType(item.FindPropertyRelative(DATA_TYPE_NAME_PROP).stringValue); // Display a warning field if the entry's type can't be found if (dataType == null) { EditorGUI.HelpBox(itemRect, "Invalid Type", MessageType.Warning); rect.y += itemRect.height + MuffinDevGUI.VERTICAL_MARGIN; continue; } // If an editor exists for the entry's type, draw its GUI if (VALUE_EDITORS.TryGetValue(dataType, out IBlackboardValueEditor editor)) { itemRect.height = editor.GetPropertyHeight(item, new GUIContent(item.FindPropertyRelative("m_Key").stringValue)); editor.OnGUI(itemRect, item, new GUIContent(item.FindPropertyRelative("m_Key").stringValue)); } // Else, draw the key field and display property type as a label else { SerializedProperty keyProperty = item.FindPropertyRelative(KEY_PROP); MuffinDevGUI.ComputeLabelledFieldRects(itemRect, out Rect labelRect, out Rect fieldRect); keyProperty.stringValue = EditorGUI.TextField(labelRect, keyProperty.stringValue); EditorGUI.LabelField(fieldRect, $"No editor for type {dataType.Name}", new GUIStyle(EditorStyles.helpBox).WordWrap(false)); } rect.y += itemRect.height + MuffinDevGUI.VERTICAL_MARGIN; rect.height = MuffinDevGUI.LINE_HEIGHT; } // Draw "is empty" label if the list is empty if (serializedDataList.arraySize == 0) { EditorGUI.LabelField(rect, "This Blackboard is empty..."); } }