Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Last active December 3, 2015 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashwinning/e89ba60b61f8adcc0f2f to your computer and use it in GitHub Desktop.
Save Ashwinning/e89ba60b61f8adcc0f2f to your computer and use it in GitHub Desktop.
Convert an array of floats to an array of Vector3. (Also serialize a vector 3 array to string, just for fun)

##Converting an array of floats to string

using System.Collections.Generic;

//...

	Vector3[] ConvertToVector3Array (float[] floats)
	{
		List<Vector3> vector3List = new List<Vector3>();
		for ( int i = 0; i < floats.Length; i += 3)
		{
			vector3List.Add( new Vector3(floats[i], floats[i+1], floats[i+2]) );
		}
		return vector3List.ToArray();
	}

###To test the output

using System.Text;

//..

		void Start ()
	{
		float[] testArray = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
		Vector3[] v3Arr = ConvertToVector3Array (testArray);
		Debug.Log (SerializeVector3Array(v3Arr));
	}

	public static string SerializeVector3Array(Vector3[] aVectors)
	{
		StringBuilder sb = new StringBuilder();
		foreach (Vector3 v in aVectors)
		{
			sb.Append("{" + v.x).Append(",").Append(v.y).Append(",").Append(v.z).Append("}, ");
		}
		if (sb.Length > 0) // remove last "|"
			sb.Remove(sb.Length - 1, 1);
		return sb.ToString();
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment