Change the color of a Cube in Unity not in one, but in two different ways!
(Should be spelled Cubemeleon!)
The server, written in Go, is on the server directory. This is the easiest way
to run it:
go run main.goSome remarks:
- You'll need a reasonably recent Go version installed.
- It always binds to port 8888.
- There is a single endpoint,
/colorwhich you can use to get or set the desired color.GET /colorwill return a string representation of the RGB color components, with each component between 0 and 1. The format looks like this:0.30,0.20,0.83. (And now that I think about it, two decimals is quite less precise than what we get with a byte; this should have had more decimals!) Example:curl http://localhost:8888/colorPUT /colorsets the color. It expects the desired color in the body, as string similar to the one returned byGET. Example:curl -X POST -d "0.1,0.2,0.7" http://localhost:8888/color
- It's not doing anything slow, so I'd expect it to scale reasonably well.
- The architecture is simplistic; it should scale fine enough to a few more endpoints. After, at the very least, I'd put more thought in a proper error handling and logging strategy.
- Security is also completely neglected.
- We should add some kind of authentication/authorization to prevent anyone to access it.
- The details of course depend on how the service would be used. For example, maybe password-less access to the color is fine, but if this was some sort of a per-player secret color that is relevant for the game, we would need to protect it.
- Setting the color sounds more like a thing that should be always protected (like, we can't allow anyone to easily change some setting that affects the game).
- If we are protecting only
POSTs, BasicAuth possibly could be enough. If we need per-player authentication, BasicAuth or Bearer would probably be the obvious choices (again, depending on the overall architecture, beyond just this "color server").
SampleSceneis where the Cubemeleon lives.- There's a custom Editor Tool that allows to change the Cubemeleon color. It is
accessible from the menu
Tools / Cubemeleon. - When the scene runs, the Cubemeleon script will perform a
GET /colorrequest to server, build aColorfrom the result, and set the Cubemeleon color to it.- What I did is arguably too simplistic for a real use case. The code has a comment about this, including a possible alternative.