Unity Lasso Selection - Part 2: Point-in-polygon algorithm explained

~8 minutes read | 1500 words


In the last post, we talked about how to make thing selectable, and a few ways to connect stuff together. This post will explain the interesting part - the lasso selection algorithm. You can find the complete code here: https://github.com/LeLocTai/unity-objects-selections.

At its core, lasso selection is the problem of testing whether a point in a polygon.

The first thing I thought of when starting this project, is that it would be difficult to do actual point in polygon test - especially for weirdly shaped ones that have crossing edge like those in the gif above. I was going to try to tessellate the polygon into triangles to reduce the problem to point in triangles, which a lot easier.

I discovered that was pretty stupid, after a quick internet search.

Unity Lasso Selection - Part 1: Building the foundation

~16 minutes read | 3400 words


This first post go through the process of designing and building all the infrastructure needed for a lasso selection system in Unity Engine, including some tips for managing dependency between Unity scripts.

While this seem mundane, a good design is extremely important for any project. A well designed system lets you extend them without tearing thing apart. It’s easy to test, so you’re confident that your new feature doesn’t break any things. It’s portable, so we can pull it from one project and plug it into another, without having to carry over a bunch of unrelated stuffs, or having to do unnecessary setup work.

While this post can’t cover everything you need to know to design a good system, nor is it always practical to choose good design over development speed; I’m a firm believer in the one-step-at-a-time approach to learning. I hope to convince you that dependency injection is easy, and Singleton is almost never worth it.

If you’re only interested in the lasso selection algorithm, it’ll be in the next part. If you just want something you can use, the Github link is below.

Note that this series is only mean to cover the interesting parts of the project, not a step by step tutorial; so if you just use the code here, it probably will not run. The full codebase is available at https://github.com/LeLocTai/unity-objects-selections.