I’m using the great ArrayPrefs2 script to save arrays to PlayerPrefs, and I’ve recently been getting Out of Memory exceptions only on iOS only (editor works fine, saving completes).
The exception occurs at this line:
var bytes = new byte[(4*array.Count)*vectorNumber + 1];
Anyone have any idea why this would happen? The arrays I’m saving are only about 20 items, so not a huge number. Unity 4, iOS6.01, XCode 4.5.2.
That would have been the smart thing to do! In the editor array.Count is 3, on iOS it’s over 200 million. So that’s the problem.
Here’s code from ArrayPrefs2 with Debug.Log added by me.
public static bool SetIntArray (String key, int[] intArray)
{
Debug.Log("A: "+intArray.Length);
return SetValue (key, intArray, ArrayType.Int32, 1, ConvertFromInt);
}
private static bool SetValue<T> (String key, T array, ArrayType arrayType, int vectorNumber, Action<T, byte[],int> convert) where T : IList
{
if (array.Count == 0)
{
Debug.LogError ("The " + arrayType.ToString() + " array cannot have 0 entries when setting " + key);
return false;
}
Debug.Log("B: "+array.Count);
var bytes = new byte[(4*array.Count)*vectorNumber + 1];
bytes[0] = System.Convert.ToByte (arrayType); // Identifier
Initialize();
for (var i = 0; i < array.Count; i++) {
convert (array, bytes, i);
}
return SaveBytes (key, bytes);
}
In the editor the outputs for A and B lines are the both 3, on my iPad2 A is 3 but the B value is 200,000,000+. Any idea why using the IList interface would cause this, only on iOS?