public void ShowValidationMessages() { IValidated target = (IValidated)this.target; // Enable rich text in help boxes. Store original so we can revert since this might be a "hack". var styleRichText = GUI.skin.GetStyle("HelpBox").richText; GUI.skin.GetStyle("HelpBox").richText = true; // This is a static list so we need to clear it before use. Not sure if this will ever be a threaded // operation which would be an issue. foreach (var messages in ValidatedHelper.messages) { messages.Clear(); } // OceanRenderer isn't a hard requirement for validation to work. Null needs to be handled in each // component. target.Validate(FindObjectOfType <OceanRenderer>(), ValidatedHelper.HelpBox); // We only want space before and after the list of help boxes. We don't want space between. var needsSpaceAbove = true; var needsSpaceBelow = false; // We loop through in reverse order so errors appears at the top. for (var messageTypeIndex = 0; messageTypeIndex < ValidatedHelper.messages.Length; messageTypeIndex++) { var messages = ValidatedHelper.messages[messageTypeIndex]; if (messages.Count > 0) { if (needsSpaceAbove) { // Double space looks good at top. EditorGUILayout.Space(); EditorGUILayout.Space(); needsSpaceAbove = false; } needsSpaceBelow = true; // Map Validated.MessageType to HelpBox.MessageType. var messageType = (MessageType)ValidatedHelper.messages.Length - messageTypeIndex; if (_groupMessages) { // We join the messages together to reduce vertical space since HelpBox has padding, borders etc. var joinedMessage = messages[0]; // Format as list if we have more than one message. if (messages.Count > 1) { joinedMessage = $"- {joinedMessage}"; } for (var messageIndex = 1; messageIndex < messages.Count; messageIndex++) { joinedMessage += $"\n- {messages[messageIndex]}"; } EditorGUILayout.HelpBox(joinedMessage, messageType); } else { foreach (var message in messages) { EditorGUILayout.HelpBox(message, messageType); } } } } if (needsSpaceBelow) { EditorGUILayout.Space(); } // Revert skin since it persists. GUI.skin.GetStyle("HelpBox").richText = styleRichText; }
public void ShowValidationMessages() { IValidated target = (IValidated)this.target; // Enable rich text in help boxes. Store original so we can revert since this might be a "hack". var styleRichText = GUI.skin.GetStyle("HelpBox").richText; GUI.skin.GetStyle("HelpBox").richText = true; // This is a static list so we need to clear it before use. Not sure if this will ever be a threaded // operation which would be an issue. foreach (var messages in ValidatedHelper.messages) { messages.Clear(); } // OceanRenderer isn't a hard requirement for validation to work. Null needs to be handled in each // component. target.Validate(FindObjectOfType <OceanRenderer>(), ValidatedHelper.HelpBox); // We only want space before and after the list of help boxes. We don't want space between. var needsSpaceAbove = true; var needsSpaceBelow = false; // We loop through in reverse order so errors appears at the top. for (var messageTypeIndex = 0; messageTypeIndex < ValidatedHelper.messages.Length; messageTypeIndex++) { var messages = ValidatedHelper.messages[messageTypeIndex]; if (messages.Count > 0) { if (needsSpaceAbove) { // Double space looks good at top. EditorGUILayout.Space(); EditorGUILayout.Space(); needsSpaceAbove = false; } needsSpaceBelow = true; // Map Validated.MessageType to HelpBox.MessageType. var messageType = (MessageType)ValidatedHelper.messages.Length - messageTypeIndex; if (_groupMessages) { // We join the messages together to reduce vertical space since HelpBox has padding, borders etc. var joinedMessage = messages[0]._message; // Format as list if we have more than one message. if (messages.Count > 1) { joinedMessage = $"- {joinedMessage}"; } for (var messageIndex = 1; messageIndex < messages.Count; messageIndex++) { joinedMessage += $"\n- {messages[messageIndex]}"; } EditorGUILayout.HelpBox(joinedMessage, messageType); } else { foreach (var message in messages) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.HelpBox(message._message, messageType); // Jump to object button. if (message._object != null) { // Selection.activeObject can be message._object.gameObject instead of the component // itself. We soft cast to MonoBehaviour to get the gameObject for comparison. // Alternatively, we could always pass gameObject instead of "this". var casted = message._object as MonoBehaviour; if (Selection.activeObject != message._object && (casted == null || casted.gameObject != Selection.activeObject)) { if (s_jumpButtonContent == null) { s_jumpButtonContent = new GUIContent(EditorGUIUtility.FindTexture("scenepicking_pickable_hover@2x"), "Jump to object to resolve issue"); } if (GUILayout.Button(s_jumpButtonContent, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true))) { Selection.activeObject = message._object; } } } // Fix the issue button. if (message._action != null) { if (s_fixButtonContent == null) { s_fixButtonContent = new GUIContent(EditorGUIUtility.FindTexture("SceneViewTools@2x"), "Fix the issue"); } if (GUILayout.Button(s_fixButtonContent, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true))) { var serializedObject = new SerializedObject(message._object); message._action.Invoke(serializedObject); if (serializedObject.ApplyModifiedProperties()) { // SerializedObject does this for us, but gives the history item a nicer label. Undo.RecordObject(message._object, $"Fix for {message._object.name}"); } } } EditorGUILayout.EndHorizontal(); } } } } if (needsSpaceBelow) { EditorGUILayout.Space(); } // Revert skin since it persists. GUI.skin.GetStyle("HelpBox").richText = styleRichText; }