Search Unity

CurrentCulture changes back when modifying C# source files at runtime

Discussion in 'Editor & General Support' started by Midiphony, Jun 13, 2019.

  1. Midiphony

    Midiphony

    Joined:
    Jan 25, 2019
    Posts:
    14
    Hi everyone !

    This one is going to be pretty hard to grasp, but you could help me very much to better understand how the engine works (it might also be your case). ;)

    In my project, I am parsing OpenStreetMap data in real time, in order to display 3D buildings in Unity. Since I am French, the default CultureInfo chosen by .NET is fr-FR, the culture of my system. However, the OpenStreetMap data are written in an english format (dot instead of comma in numbers, date format...).

    My current solution has been to change the CurrentCulture of the System at the start of the program :
    Code (CSharp):
    1. System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
    2.  
    3.         System.Globalization.CultureInfo.CurrentCulture = culture;
    4.         System.Globalization.CultureInfo.CurrentUICulture = culture;
    5.  
    6.         System.Globalization.CultureInfo.DefaultThreadCurrentCulture = culture;
    7.         System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = culture;
    This works quite well and removes every FormatException that I used to have.

    However, the CurrentCulture gets back to "fr-FR" (my system culture) when I am modifying any of my C# source files while the game is running in the editor. :eek:

    I'm not sure to understand how Unity and CLR works with each other. It seems that Unity recompiles my code, executes again the CLR and that's when the CurrentCulture seems to go back to default.

    Am I understanding the issue correctly ? :confused:
    What can I do to prevent this from happening ? (I was thinking of checking the CurrentCulture each time I am parsing data but this seems so non-performant)


    Thank you in advance, I can give more details or try to explain myself better:)
     
  2. Midiphony

    Midiphony

    Joined:
    Jan 25, 2019
    Posts:
    14
    Okay, I still don't understand what Unity is doing when modifying a script at runtime (does it relaunch the CLR ? Is that why the CurrentCulture changes ?)...

    But I might have a consistent fix for my problem : always use the InvariantCulture when parsing any of the data to import. It looks like this :
    Code (CSharp):
    1. float.Parse(stringNumberToParse, System.Globalization.CultureInfo.InvariantCulture)
    It should be more efficient than checking and changing the CurrentCulture at each parsing operations...