I’ve noticed an issue when retrieving string constants via reflection. If you use certain characters the returned string becomes cut off. Here’s a short, self-contained, correct example:
public class BugExample : UnityEngine.MonoBehaviour {
private const string example0 = "0123";
private const string example1 = "½123";
private const string example2 = "§123";
private const string example3 = "£123";
private const string example4 = "££123";
void Start() {
// IL2CPP bug example with reflection.
// Target Platform: Windows
// Architecture: x86_64
// Compression Method: Default
// No ticked checkboxes in the build settings (except to include my scene)
// My OS: Windows 10 (64-bit)
// Scripting Compatibility Level: .NET 4.x
// C++ Compiler Configuration: Release
// Use incremental GC: Yes
// Allow 'unsafe' Code: No
// Default, untouched, optimization (Managed Stripping Level is Low)
// Unity engine version: 2019.3.15f1
UnityEngine.Debug.Log(example0); //expected "0123", got it
UnityEngine.Debug.Log(example1); //expected "½123", got it
UnityEngine.Debug.Log(example2); //expected "§123", got it
UnityEngine.Debug.Log(example3); //expected "£123", got it
UnityEngine.Debug.Log(example4); //expected "££123", got it
System.Reflection.FieldInfo[] constants = GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
UnityEngine.Debug.Log((string)constants[0].GetValue(null)); //expected "0123", got it
UnityEngine.Debug.Log((string)constants[1].GetValue(null)); //expected "½123" but got "½12"
UnityEngine.Debug.Log((string)constants[2].GetValue(null)); //expected "§123" but got "§12"
UnityEngine.Debug.Log((string)constants[3].GetValue(null)); //expected "£123" but got "£12"
UnityEngine.Debug.Log((string)constants[4].GetValue(null)); //expected "££123" but got "££1"
// --> The bug is that strings are cut short on more special characters (presumably "non-ASCII") when retrieved through reflection.
// Works fine when playing in the Unity Editor or when exporting with Mono as the script backend.
}
}