Entry 28 - Lua: MoonSharp & UI - July 26, 2015

MoonSharp
Last weekend I decided to switch from UniLua to MoonSharp for a ton of reasons.

First of all the code of UniLua was really hard to understand for me because most of the comments (and even some Lua error messages) were just strange Chinese (?) characters. Also UniLua is incomplete and I didn't know how to achieve specific things with it because there is no real documentation either and just very basic examples.

I don't want to use external libraries so I was looking for another Lua alternative which is 100% C# and the most promising candidate I found is MoonSharp.

MoonSharp is very well documented and the code is very clean, structured and easy to understand. I can now do a lot of fancy stuff and it's fun to use. It also seems to be fast enough for my needs. Stranded III is now already fully using it. UniLua has been completely replaced by it.

Sorting Layers
Unity's 2D system offers so called sorting layers to have more control over the rendering order of 2D sprites. This is nice but the the code API for this whole system seems to be very limited. Which is bad because I wanted to expose that functionality to Lua scripters.

The layers are defined in the Unity editor and you can only assign a name and change the order of the layers. They also have an ID internally but you can't even see that ID in the editor. Unfortunately the internal IDs also are not just a simple numbering but they seem to be quite random and there's no API command to get all the sorting layers or their IDs.

No problem though! You can set the sorting layer of a sprite using the integer ID OR the string name. Hooray!

Wait! Strings? That must be slower than using the IDs. And of course it is. I tested it:

I assigned a sorting layer to a sprite 10,000,000 (ten million) times using both methods and measured how long it took.

1

spriteRenderer.sortingLayerName = "Default";

This took 1.9 secs.

1

spriteRenderer.sortingLayerID = 0;

This took 0.25 secs

So using the string method is over 7 times slower. This seems to be a huge difference but it actually doesn't matter at all. The stuff will never be called that often. It would be impossible to notice any difference in performance.

I dislike the idea of working with strings anyway. It seems to be wrong. Especially if you could use faster integer IDs.

I didn't find a good way to get the IDs through code because of missing Unity code APIs (there is a solution for the editor but not for standalone runtime) so I decided to use a little hack to get them: I'm using the string assignment to assign the layer to a sprite and then I use the ID property to get the ID (luckily it's updated as well when the layer is changed). I repeat this for all my defined sorting layers and get a list of all sorting layer IDs this way.

I then simply create a Lua table containing this mapping so that the values can be used easily by scripters. Internally it will use the faster IDs but scripters don't have to deal with them.

UI
I also changed the UI look a bit. It actually looks much more like Stranded II now!

The main menu has leafs now as I mentioned earlier in one of the older blog entries. What you can't see on the screenshot: They are shaking a bit to make them look more natural and alive.

Settings with the Stranded II paper-style background and wooden boards around it:

And my self-written Lua UI even supports tooltips with images now. Even though that red cross doesn't make much sense here. Later it might show a sample comparison image with a specific feature enabled and disabled so you can see the difference.