public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var eventNameRef = property.FindPropertyRelative("Value"); var asStringRef = property.FindPropertyRelative("AsString"); LastEventString.AsString = asStringRef.boolValue; LastEventString.Value = eventNameRef.stringValue; LastEventString = Draw(position, LastEventString, label); eventNameRef.stringValue = LastEventString.Value; asStringRef.boolValue = LastEventString.AsString; property.serializedObject.ApplyModifiedProperties(); }
static public EventString Draw(Rect position, object eventStringObj, GUIContent content) { EventString eventString = eventStringObj as EventString; // Where the Left and Right UI area separate var labelDivider = position.x + InspectorValues.LabelWidth; // Scale dynamically when wide enuogh labelDivider += Mathf.Max(0, (position.width - InspectorValues.MinRectWidth) / InspectorValues.WidthScaler); // The Left Label Area var labelRect = position; labelRect.xMax = labelDivider; EditorGUI.SelectableLabel(labelRect, content.text); // The right UI Area var propBoxRect = position; propBoxRect.xMin = labelDivider; var boxStyle = new GUIStyle(EditorStyles.helpBox); // Use that to draw a box GUI.BeginGroup(propBoxRect, boxStyle); GUI.EndGroup(); // Add margin for the box var propRect = propBoxRect; propRect.xMin += PropertyBoxMargins; propRect.xMax -= PropertyBoxMargins; propRect.yMin += PropertyBoxMargins; propRect.yMax += PropertyBoxMargins; propRect.height = InspectorValues.RowHeight; // The left Properties var leftPropRect = propRect; leftPropRect.width = ToggleWidth; // The right Properties var rightPropRect = propRect; rightPropRect.xMin = leftPropRect.xMax; // First Property Row { var ToggleLabel = eventString.AsString ? "String" : "Type"; eventString.AsString = EditorGUI.ToggleLeft(leftPropRect, ToggleLabel, eventString.AsString, LabelStyle); EditorGUI.BeginDisabledGroup(!eventString.AsString); eventString.Value = EditorGUI.TextField(rightPropRect, eventString.Value); EditorGUI.EndDisabledGroup(); // Early exit if configured to not show dropdowns when in string mode if (CompactWhenString && eventString.AsString) { return(eventString); } } // Second and Third Property row are disabled when string mode is active EditorGUI.BeginDisabledGroup(eventString.AsString); { var category = eventString.GetEventCategory(); // Second Property Row { leftPropRect.y += InspectorValues.RowHeight + PropertyBoxMargins; rightPropRect.y += InspectorValues.RowHeight + PropertyBoxMargins; EditorGUI.LabelField(leftPropRect, "Category", LabelStyle); // Check if the currently stored category is in the list of categories (since we need the index for the dropdown array) int categoryIndex = IndexOf(EventCategories, category); // When not found add default and select it if (categoryIndex < 0) { categoryIndex = EditorGUI.Popup(rightPropRect, 0, ConcatArrays(new string[] { "-" }, EventCategories)) - 1; } else { categoryIndex = EditorGUI.Popup(rightPropRect, categoryIndex, EventCategories); } // User selected a valid category if (categoryIndex >= 0) { // And a category that is different than before if (!EventCategories[categoryIndex].Equals(category)) { category = EventCategories[categoryIndex]; eventString.Value = EventCategory.ConstructEventString(category, ""); } } } // Third Property Row { leftPropRect.y += InspectorValues.RowHeight; rightPropRect.y += InspectorValues.RowHeight; EditorGUI.BeginDisabledGroup(category == null); EditorGUI.LabelField(leftPropRect, "Event", LabelStyle); // No valid category selected yet if (category == null) { EditorGUI.Popup(rightPropRect, 0, new string[] { "-" }); } // We have a valid category else { // Get the events for this category, prefixing an empty element to represent no choice var eventNames = EventCategory.GetEventNamesInCategory(category); // Check if the currently stored eventString is in the list of eventNames int eventIndex = IndexOf(EventCategory.GetEventStringsInCategory(category), eventString.Value); // When not found add default and adjust index if (eventIndex < 0) { eventIndex = EditorGUI.Popup(rightPropRect, 0, ConcatArrays(new string[] { "-" }, eventNames)) - 1; } else { eventIndex = EditorGUI.Popup(rightPropRect, eventIndex, eventNames); } // User selected a valid eventName so store it if (eventIndex >= 0) { eventString.Value = EventCategory.GetEventNameInCategory(category, eventNames[eventIndex]); } } EditorGUI.EndDisabledGroup(); } } EditorGUI.EndDisabledGroup(); return(eventString); }