ARC-8: devlog #4 - CHIP-8 inside Unity Inspector
Some years ago I coded a CHIP-8 emulator in C# just for fun, that emulator was hibernating in a private repository that I never released. Some days ago I started to working on it again with the idea to release it running on Blazor and as a Unity asset where any game developer could drag its prefabs as easter eggs directly to their games.
In this post, I will talk about how I put a CHIP-8 emulator to run inside the Unity Editor Inspector Window.
To help to understand everything in this post I recommend you read the previous ARC-8 devlog posts, in special the last one.
The source code is not yet published on GitHub, I will notify in this series of posts about ARC-8 devlog and on my Twitter too when this happens.
Introduction
I wanted to run the graphics of the emulator inside the Unity Editor Inspector Window. Maybe, you can ask “why?”, well there is an inside joke among Unity developers where we try to play not expected things inside the inspector window, as you can see in the links below:
- https://twitter.com/DJ_Link/status/1134489432130818048
- https://twitter.com/alexanderameye/status/1006916653777596416
- https://twitter.com/DevdogUnity/status/1007563448174219265
Chip8LoaderEditor.cs
This is a simplified version of the Editor script for Chip8Loader.
StartEmulator and StopEmulator methods
In the method StartEmulator
we start the emulation inside the Inspector Window.
After starting the emulator by calling the Run
method of Chip8Loader
passing our local EditorChip8Input
to override the emulator input to always use the keyboard.
We use the EditorCoroutineUtility.StartCoroutine
method from the Editor Coroutines package to start two coroutines for update and render emulator inside the editor.
The StopEmulator
stops the emulation inside the Inspector Window using the EditorCoroutineUtility.StopCoroutine
.
UpdateEmulator and RenderEmulator methods
In the method UpdateEmulator
we call LateUpdate from Chip8Loader
to run a emulation cycle.
The RenderEmulator
calls the Repaint from Editor to redraw the inspector using the OnInspectorGUI.
OnInspectorGUI method
When the emulation is not running this method just draws the Chip8Loader
inspector using the DrawDefaultInspector.
When we click on the button Test emulator
on the inspector, the emulation is activated and the emulation will be rendered on the inspector.
When we use the GUI.BeginClip
everything that we draw using the GL (Low-level graphics library) will be rendered inside the area passed as an argument to BeginClip. This is why we call Graphic.Render
between the BeginClip and EndClip.
In the last step, we read the input using input.ReadKeys
.
Next step
In the next ARC-8 devlog post I will probably talk about the release of the ARC-8 as an open-source project.
If you have any doubts about what I talk about above or any tip about the CHIP-8 emulator (or Unity) and you like to share it, please let me know in the comments section below.
Further reading
- Editor Scripting
- Custom Editors
- Editor
- Editor Coroutines package
- LateUpdate
- Repaint
- OnInspectorGUI
- DrawDefaultInspector
Icons made by Freepik, Vignesh Oviyan and Eucalyp from www.flaticon.com is licensed by Creative Commons BY 3.0