converting string to vector in Unity

3.2k Views Asked by At

I am trying to convert an array of strings in unityscript with values holding values like:

"Vector3(5, 3, 8)"

into an array of vectors, but Unity will not take these strings as is. Anyone have any ideas?

4

There are 4 best solutions below

0
On BEST ANSWER

I figure someone else might find this useful later so:

if(PlayerPrefs.GetFloat("nodeNum") > 0) {
    //Assemble axis value arrays

    //X
    var xString = PlayerPrefs.GetString("xVals");
    var xValues = xString.Split(","[0]);
    //Y
    var yString = PlayerPrefs.GetString("yVals");
    var yValues = yString.Split(","[0]);
    //Z
    var zString = PlayerPrefs.GetString("zVals");
    var zValues = zString.Split(","[0]);

    var countNode = 0;
    var goal = PlayerPrefs.GetFloat("nodeNum");
    var nodeVecs = new Array(Vector3.zero);

    while (countNode != goal) {
        var curVec = Vector3(float.Parse(xValues[countNode]), float.Parse(yValues[countNode]), float.Parse(zValues[countNode]));
        nodeVecs.Push(curVec);
        countNode += 1;
    }
    var convNodeVecs : Vector3[] = nodeVecs.ToBuiltin(Vector3) as Vector3[];

    for(var nodeVec : Vector3 in convNodeVecs) {
        Instantiate(nodeObj, nodeVec, Quaternion.identity);
    }
}
1
On

you cant convert all vector elements together in c# you can do it like bellow:

its psuedoCode:

position.x=convert.ToFloat("3");
position.y=....

i think there is no api to make this for you: "Vector3(5, 3, 8)"

0
On

You didn't give us any sample code, so I'll assume the array is nicely organized, all elements right next to each other.

While I don't think there's any way that you can do this fully using UnityScript, there's ways you can process the literal string with other languages, that is, copying from your Unity script and into another program that will do the change for you.

Here's a small sample of what I would do.

Usage: Create a file array.txt with all the Vector3(x,x,x) strings, separated by line breaks and create another file array.php

Array.txt (sample)

Vector3(5, 1, 3)
Vector3(3, 3, 1)
Vector3(2, 2, 7)
Vector3(6, 6, 4)
Vector3(8, 8, 8)
Vector3(9, 3, 2)
Vector3(1, 2, 1)
Vector3(4, 3, 6)
Vector3(5, 3, 8)

Array.php

<?

$file = file('array.txt');

$text = array();

$text[] = "var vectors = new Array;";

foreach ( $file as $i )
{
    $text[] = "vectors.Push(".trim($i).");";
}

echo implode("<br>", $text);

Then just run it under a PHP sandbox or a web server and copy and paste the new array into your script.

0
On

use this method to convert a single String value into a vector3 value

public static Vector3 StringToVector3(string sVector)
     {
         // Remove the parentheses
         if (sVector.StartsWith ("(") && sVector.EndsWith (")")) {
             sVector = sVector.Substring(1, sVector.Length-2);
         }
 
         // split the items
         string[] sArray = sVector.Split(',');
 
         // store as a Vector3
         Vector3 result = new Vector3(
             float.Parse(sArray[0]),
             float.Parse(sArray[1]),
             float.Parse(sArray[2]));
 
         return result;
     }