Sometimes, the AOT compiler cannot figure out all thee code it needs to generate for an iOS project. When you call this code, the game crashes.
There are a few different causes.
- The one is generic methods that call other generic methods, where one of the parameters is a value type. Grids implement points use structs, so many of the generic methods have value-typed type parameters. Most of these work fine, but some of them call other generic methods, and they sometimes don’t get generated, so that your came crashes when the original method is called.
- Some collections (List, HashSet, etc.) generate equality comparers automatically. This works fine as long as the items in the collection are not value types.
- Some LINQ expressions crash, presumably when they call the underlying collection’s methods that use the automatically generated equality comparers.
To make Grids compile with iOS, follow these steps:
- Use PointList, instead of List, for lists of points.
- For LINQ exressions that crash, firs convert the intermediate results to a PointList, using the ToPointList method.
- For HashSets, and Dictionaries, where the keys are points, specify a comparer to use in the constructor of these classes. You can use the PointComparer that come with grids for this purpose.
- For other methods that crash, you can add compiler hints. We have added some hints as a template for basic grid construction, but you may need to add one or two more depending on what you are using. The scenario is typically you call a method A, which in turns calls B. The game crashes, giving you an error saying that B cannot be JITed. To have B generated, you have to call it somewhere explicitly with the right type, B(). You can call all your hints from the same place. Anywhere is fine, although the Start or Awake method in a central class (such as your GameManager) is a good place. Make sure that they don’t get stripped by the compiler!
If you use a grid with your own cell type, you may need to add hits for the grid’s construction. To add compiler hints from the template, follow these steps:
- Find the method in Templates/CompilerHints.cs that corresponds to the grid you are using. For example, if you use a RectGrid, then find the method __CompilerHint__Rect().
- Copy this method to a place where you can easily call it (typically in the file where you instantiate your grid).
- Change the type __CellType to whatever cells you are using in your grid. You need a version of the hint method for each type of cell you are using.
- You need to call this method in a way that makes sure that the compiler does not take the call out as an optimization step. It doe snot really matter where you call it, as long as it is definitely called. A good place to put it is just before you construct the grid. The method always return true, so you can call it like this: