0
\$\begingroup\$
private GameObject GetObjectFromFile(string path)
    {
        GameObject newobj = new GameObject();
        string[] text = new string[3];
        if (File.Exists(path))
        {
            text = File.ReadAllLines(path);
            foreach (string s in text)
            {

                    text = s.Split(':');
                    break;

            }
            var parent = text[0];

            var localPosition = StringToVector3(text[1]);
            var localRotation =StringToVector3(text[2]);
            var localScale = StringToVector3(text[3]);

            newobj.transform.localPosition = localPosition;
            newobj.transform.localRotation = localRotation;
            newobj.transform.localScale = localScale;
        }

        return newobj;
    }

    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;
    }

It's converting fine the localPosition and localScale but the localRotation is a quaternion and not vector3.

This is the text file format I'm reading from:

Main Camera (UnityEngine.Transform):(297.7, 5.1, 228.5):(0.0, 0.0, 0.0, 1.0):(1.0, 1.0, 1.0)

The part (0.0, 0.0, 0.0, 1.0) is the rotation.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Have you tried defining a StringToQuaternion method analogous to your StringToVector3?

public static Quaternion StringToQuaternion(string sQuaternion)
{
    // Remove the parentheses
    if (sQuaternion.StartsWith("(") && sQuaternion.EndsWith(")"))
    {
        sQuaternion = sQuaternion.Substring(1, sQuaternion.Length - 2);
    }

    // split the items
    string[] sArray = sQuaternion.Split(',');

    // store as a Vector3
    Quaternion result = new Quaternion(
        float.Parse(sArray[0]),
        float.Parse(sArray[1]),
        float.Parse(sArray[2]),
        float.Parse(sArray[3]));

    return result;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .