Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Parsing floats mac-PC

Discussion in 'Scripting' started by Raef, Mar 30, 2009.

  1. Raef

    Raef

    Joined:
    Mar 30, 2009
    Posts:
    27
    hello unity community!

    I've got a small problem. I've created a small web application where users can edit a room and save all the locations of the objects in the room. When you load the application it will load that ASP and parse all the objects and instantiate them in the room.

    now this was developped on pc, but due to circumstances I had to continue the development on mac and when I tried to run it, I noticed that the locations of my paintings didn't parse well.

    I think this is due to the fact that I use ParseFloat(string) to turn my splitted string into floats for my postion/quaternion. but I get really large numbers instead.

    here's a sample of what I parse
    Code (csharp):
    1. Future Flow ;2008;Jerre;3,135276;1,407117;-1,048797;-1,514731E-07;0;0;1$
    with first the name, the year, the creator, the position x,y,z and quaternion x,y,z,w all seperated by ; and closed with a $

    could it be the fact that my transform.position.x saves a comma float, while mac wants a .?

    thanks in advance, first problem I've encountered with Unity, but that doesn't change my positive opinion about it, as a Technical Artist, I just love it :p
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
  3. Raef

    Raef

    Joined:
    Mar 30, 2009
    Posts:
    27
    thx for the reply, is there also a way to do this in javascript?
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    JavaScript in unity uses the .net API just like C# does. The syntax might need some changing here and there, but the classes, methods etc. are all the same.
     
  5. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Just for reference:

    In Javascript do the following:

    1) Add the internationalization import:
    Code (csharp):
    1. import System.Globalization;
    2) Add these helpers to your code:
    Code (csharp):
    1.  
    2. function ConvertVector3(str1 : String, str2 : String, str3 : String) {
    3.     return Vector3(ConvertStringFloat(str1), ConvertStringFloat(str2), ConvertStringFloat(str3));
    4. }
    5.  
    6. function ConvertQuaternion(str1 : String, str2 : String, str3 : String, str4 : String) {
    7.     return Quaternion(ConvertStringFloat(str1), ConvertStringFloat(str2), ConvertStringFloat(str3), ConvertStringFloat(str4));
    8. }
    9.  
    10. function ConvertStringFloat(str : String) {
    11.     return float.Parse(str, CultureInfo.InvariantCulture);
    12. }
    3) Convert a string with a Vector3 position like this:
    Code (csharp):
    1. var posBox : Vector3 = ConvertVector3(boxArray[0], boxArray[1], boxArray[2]);