Setting up the code
This is an explanation to get started with RimGui.
class ManualEntry : MonoBehaviour
{
[SerializeField] ManualSO manualSO;
[SerializeField] Camera unityCamera;
GuiHelper guiHelper;
Gui gui;
Context ctx;
public void Start()
{
guiHelper = new(manualSO.GuiHelperSO, unityCamera);
gui = guiHelper.Gui;
ctx = guiHelper.Gui.Context;
}
public void Update()
{
// Process Gui for this frame.
// Must be executed once at the beginning of the frame.
guiHelper.ProcessFrame();
// It is possible to execute Gui.Text() and other Gui functions freely between ProcessFrame() and CompleteFrame().
if (gui.BeginStandardWindow("Window"))
{
gui.Text("A");
gui.EndStandardWindow();
}
// Must be executed once at the end of the frame.
guiHelper.CompleteFrame();
}
void OnDestroy()
{
// Make sure to call Dispose().
guiHelper.Dispose();
}
}
1. Initialization in Start()
A GuiHelper
instance is created to set up the necessary resources for GUI rendering. GuiHelper
provides a Gui
object, which manages the Context
.
guiHelper = new(manualSO.GuiHelperSO, unityCamera);
gui = guiHelper.Gui;
ctx = guiHelper.Gui.Context;
2. GUI Processing in Update()
Starting Frame Processing
At the beginning of each frame, guiHelper.ProcessFrame()
is called. This resets the GUI state and prepares it for new frame rendering.
guiHelper.ProcessFrame();
Rendering the GUI
A standard window is created with Gui.BeginStandardWindow("Window")
, and Gui.Text("A")
displays text within the window.
if (gui.BeginStandardWindow("Window"))
{
gui.Text("A");
gui.EndStandardWindow();
}
Ending Frame Processing
At the end of the frame, guiHelper.CompleteFrame()
is called to finalize the rendering and reflect it on the screen.
guiHelper.CompleteFrame();
3. Releasing Resources in OnDestroy()
Calling guiHelper.Dispose()
ensures that GUI resources are properly released. Failure to do so may lead to memory leaks.
void OnDestroy()
{
guiHelper.Dispose();
}