I am creating a Custom Editor Mode in Unreal. There is a ton of functions for drawing primitives with the Primitive Drawing Interface and the Primitive Drawing Utils, but every solution I found for drawing world-space text is linked to Play Mode, creating Objects with Text Components, or using HUD… I would like to find a solution that is self-contained and not some kind of hack that involves messing with the current Level/World. Any thoughts?
Thanks in advance!
Well, I could get it working by myself. Here is the follow-through:
After looking into how the GEngine->AddOnScreenDebugMessage does its magic I figured it out. The data is transferred into an intermediate struct that holds the required information (such as message-id, text, text color, etc). In another function, it uses the FSceneView and FViewport to get the DebugCanvas. This FCanvas object is some kind of parent to other Canvas Objects created on-demand. This child Canvas is used to display the messages recorded in these intermediate structs. Since I am not handling a list of objects (messages) to display, I used the following code in the Render function of my custom Editor Mode, it draws a text attached to a world-position:
It required a few includes to get access to the UDebugDrawService, and I am 100% sure it is safe to call this snippet from the Render function, BUT IT WORKS!
One might want to use another font, text color, etc. This is all achieved by injecting this information into the FCanvasTextItem. There are plenty of properties in this object which provide flexibility. Some adjustments would have to be done to support stereoscopic rendering. Though I don’t plan to support it for my Editor Mode, I think the following snippet might work in some specific functions of the Player Loop, pretty much the same way the GEngine->AddOnScreenDebugMessage works on non-production builds.
Actually, it appears that the _debugCanvasObject is only used for VR rendering. So all we need is to get the DebugCanvas from the Viewport and render the FCanvasItem objects with it. The DrawStatsHud is also not necessary, otherwise, those things would be drawn twice… There are plenty of different Canvas Items to pick from, which is neat. The following code displays an example of the minimal form: