Search Unity

Question #if doesnt nest in visual studio

Discussion in 'Code Editors & IDEs' started by sildeflask, Feb 13, 2024.

  1. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    329
    This is mostly a cosmetic thing but its bothering me

    Code (CSharp):
    1.  
    2.  
    3. #if UNITY_ANDROID
    4.         if (Application.platform == RuntimePlatform.Android)
    5.         {
    6.             Screen.sleepTimeout = SleepTimeout.NeverSleep;
    7.  
    8.         }
    9. #endif
    10.  
    11.  
    #if UNITY_ANDROID and #endif are not automatically lined up (nested) as a normal if statement would.

    I assume there is some setting in visual studio that makes #if go maximum to the left, even if its nested in other methods.

    Anyone knows how to fix this? thanks
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    477
    I don't think it is possible and there is a reason. This is a valid code:

    Code (CSharp):
    1. #if UNITY_ANDROID
    2.         private void Test_Android()
    3.         {
    4.             Debug.Log("Android");
    5. #elif UNITY_WINDOWS
    6.         private void Test_Windows()
    7.         {
    8.             Debug.Log("Window");
    9. #elif UNITY_EDITOR
    10.         private void Test_Editor()
    11.         {
    12.             Debug.Log("Editor");
    13. #endif
    14.             // More code
    15.         }
    There is no meaning of indentation of preprocessor expressions.
     
  3. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    329
    why would this be so bad? :

    Code (CSharp):
    1.             #if UNITY_ANDROID
    2.             private void Test_Android()
    3.             {
    4.                 Debug.Log("Android");
    5.             #elif UNITY_WINDOWS
    6.             private void Test_Windows()
    7.             {
    8.                 Debug.Log("Window");
    9.             #elif UNITY_EDITOR
    10.             private void Test_Editor()
    11.             {
    12.                 Debug.Log("Editor");
    13.            #endif
    14.                 // More code
    15.             }
    16.  
    i can recreate it with spaces but whenever i copy paste it reformats back to the left, but if its not possible its not possible, thanks for the reply
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    477
    In my own opinion: you're mixing runtime code with compiler preprocessor and in reality indentation needs to have meaning. In case of preprocessors, there is no any meaning.

    IDK the official stance on it though, you should ask/research Microsoft.