Quantcast
Channel: Answers by "robinking"
Viewing all articles
Browse latest Browse all 24

Answer by robinking

$
0
0
After some diagnostics, I've discovered the exact cause of the problem. A 3D array of Vector2s goes awry when built for iPhone: each Vector2 seems to use 2 array spaces - the index position for Vector2.x and the one after for Vector2.y! It can be grasped more easily using this script - attach it to an Empty in an otherwise empty scene. Then run it in the editor and build for the device. I've attached the two outputs as screen grabs below. The script stores a sequence of Vector2s in a 3d array, retrieves each just after storing (works fine), then retrieves them all after all have been stored (the Y of each has been overwritten by the X of the subsequent one on the device). It prints the Vector2s three different ways, to test if it is a formatting problem (it isn't). I don't know if this occurs for 2d arrays as well, or for Vector3s as well - but it's worth looking into. I think this is worth reporting as a bug. I fixed it for my game by splitting the 3d array of Vector2s into 2 x 3d arrays of floats (one for x, one for y). I tried a 4d array at first, but then discovered another issue, namely that 4d arrays work fine in the editor but cause fatal error on the device! using UnityEngine; using System.Collections; using System.Collections.Generic; public class Vec2Problem : MonoBehaviour { public List msgs; // Use this for initialization void Start () { MakeV2s (); } private void MakeV2s() { int width = 2, height = 2, depth = 2; Vector2[,,] vecs = new Vector2[width,height,depth]; float a = 0; float b = 0; msgs.Add ( "STORE VECTOR2s IN 3D BUILT-IN ARRAY:" ); for ( int x = 0; x < width; x++ ) { for ( int y = 0; y < height; y++ ) { for ( int z = 0; z < depth; z++ ) { Vector2 v = new Vector2(a,b); string s1 = string.Format ("RAW Vector2: ",x,y,z); s1+= v.ToString () + " / " + v.ToString ("F3") + " / (" + v.x.ToString ("F3") + ", " + v.y.ToString ("F3") + ")"; msgs.Add (s1); vecs[x,y,z] = v; string s2 = string.Format (" vecs[{0},{1},{2}]: ",x,y,z); s2+= vecs[x,y,z].ToString () + " / " + vecs[x,y,z].ToString ("F3") + " / (" + vecs[x,y,z].x.ToString ("F3") + ", " + vecs[x,y,z].y.ToString ("F3") + ")"; msgs.Add (s2); Vector2 v2 = vecs[x,y,z]; string s3 = string.Format (" Vector2: ",x,y,z); s3+= v2.ToString () + " / " + v2.ToString ("F3") + " / (" + v2.x.ToString ("F3") + ", " + v2.y.ToString ("F3") + ")"; msgs.Add (s3); // msgs.Add (""); a += 0.279f; b += 0.131f; } } } msgs.Add ("RETRIEVAL:"); for ( int x = 0; x < width; x++ ) { for ( int y = 0; y < height; y++ ) { for ( int z = 0; z < depth; z++ ) { string s4 = string.Format ("vecs[{0},{1},{2}]: ",x,y,z); s4+= vecs[x,y,z].ToString () + " / " + vecs[x,y,z].ToString ("F3") + " / (" + vecs[x,y,z].x.ToString ("F3") + ", " + vecs[x,y,z].y.ToString ("F3") + ")"; msgs.Add (s4); Vector2 v3 = vecs[x,y,z]; string s5 = string.Format (" Vector2: ",x,y,z); s5+= v3.ToString () + " / " + v3.ToString ("F3") + " / (" + v3.x.ToString ("F3") + ", " + v3.y.ToString ("F3") + ")"; msgs.Add (s5); // msgs.Add (""); } } } } void OnGUI() { string msgBlock = ""; if (msgs==null) return; foreach( string s in msgs ) { msgBlock += s + "\n"; } if (msgBlock != "") GUI.Label( new Rect( 2, 2, Screen.width, Screen.height ), msgBlock ); } } ![alt text][1] [1]: /storage/temp/9622-3darrayofvector2sproblem.jpg

Viewing all articles
Browse latest Browse all 24

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>