Posts

Showing posts from December, 2016

Preventing Unity3D IL2CPP from stripping your code

I was trying to get a list of a type's constructors at runtime using reflection, so that I could create an instance of the class using dependency injection. All worked just fine until we tried to build the app for iOS. At first we were using Mono as the scripting back-end, but it seems that new versions of iOS pop up a dialog telling the user the app is 32 bit and may run slowly (i.e. "Your app is crap"). When switching the backend scripting to IL2CPP (in File->Builder->Player Settings) the app suddenly wasn't working. It turns out that SomeType.GetConstructors().Count was returning zero, which was a problem because obviously I wanted to invoke those constructors with dependencies. The problem was that because these constructors weren't being calling explicitly from anywhere in my app IL2CPP decided I didn't need them, and stripped them out. The solution is to create a file in your Assets folder called link.xml and fill it in like so.... <lin

Forcing a device-orientation per scene in Unity3D

Unity3D has a Screen class with an orientation property that allows you to force orientation in code, which lets you have different scenes with different orientations (useful in mini-games). this works fine for Android but crashes on iOS. The problem is the file UnityViewControllerBaseiOS.mm that gets generated during the build for iOS has an assert in it which inadvertently prevents this property from being used. It is possible to create a post-build class that runs after the iOS build files have been generated that can alter the generated code before you compile it in XCode. Just create a C# script named iOSScreenOrientationFix.cs and paste in the following code - adapted from  this Unity3D forum post . using UnityEngine; using UnityEditor; using UnityEditor.Callbacks; using System.IO; namespace Holovis { public class iOSScreenOrientationFix : MonoBehaviour { #if UNITY_CLOUD_BUILD // This method is added in the Advanced Features Settings on UCB // PostBuildProc