Search Unity

[suggestion] Accessible Compilation Day

Discussion in 'Scripting' started by TX128, Apr 14, 2019.

  1. TX128

    TX128

    Joined:
    Aug 22, 2018
    Posts:
    25
    Hello everyone,


    I want to make a proposal for something that is quite simple to implement to have and that would heavily simplify a lot of tasks for us internally. Our app has save files and those can run on different versions of the same application. The problem is that in order to differentiate the versions of the save files we need to manually remember to update the version number of our application. If instead we could retrieve the day (and if possible, even the time) when the code was compiled that would be pretty neat, because failing to update the version number of our application is impossible to fix down the line without telling the clients to remove all the previous copies from their computers.


    The idea would be that on the editor this method/property would return the time when the play button was pressed or the last time since compilation (in case the player has recompiled while playing).
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    GroZZleR likes this.
  4. GroZZleR and Kurt-Dekker like this.
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I use the BuildPlayer approach, via a command line build script, and it's simple enough to add arbitrary content to the build. In my game I generate a build into text file that I include in the build, to display the Git version of the build I'm creating.

    In case it's of any help, it looks something like this. (Note that the actual Git file I'm including was itself generated via a git post-commit hook. This code just finds it and includes it in my Unity build.)

    Code (CSharp):
    1. public class CommandLineBuildUtil
    2. {
    3.     static void BuildProduction()
    4.     {
    5.         /* Set up the scenes and build path stuff... */
    6.    
    7.         EditorUserBuildSettings.SetPlatformSettings("Standalone", "CopyPDBFiles", "true");
    8.         BuildPipeline.BuildPlayer(scenes, fullExePath, BuildTarget.StandaloneWindows64, BuildOptions.Development | BuildOptions.AllowDebugging);
    9.  
    10.  
    11.  
    12.         // Create a file with the git info and include it in the build
    13.         var lastCommitFileName = "last-git-commit-version.txt";
    14.         var fullCommitFilePath = Path.Combine(gitRepoBase, lastCommitFileName);
    15.  
    16.         string lastGitCommitHash = "";
    17.  
    18.         if (File.Exists(fullCommitFilePath))
    19.         {
    20.             lastGitCommitHash = File.ReadAllText(fullCommitFilePath);
    21.         }
    22.  
    23.         var buildInfo = $@"Build Date: {DateTime.Now.ToShortDateString()}
    24. Rev: {lastGitCommitHash}";
    25.  
    26.         var fullBuildInfoPath = Path.Combine(buildOutputBasePath, applicationDataDirName, BuildConstants.BuildInfoFileName);
    27.         File.WriteAllText(fullBuildInfoPath, buildInfo);
    28.  
    29.     }
    30. }
    And the Window batch file that executes this:

    Code (CSharp):
    1. "C:\Program Files\Unity\Hub\Editor\2018.3.11f1\Editor\Unity.exe" -quit -batchmode -stackTraceLogType Full -projectPath "C:\Users\Dan\Documents\GitHub\Gravia" -executeMethod CommandLineBuildUtil.BuildProduction
    (Some day maybe I'll learn how to not need to manually choose the specific Unity exe in my script...)
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Eh, or not... not all automation is worth pursuing! :)
     
  7. Fishing_Cactus

    Fishing_Cactus

    Joined:
    Nov 8, 2014
    Posts:
    45
    We use this in our build scripts to select the correct unity version based on the saved ProjectVersion.txt file:


    Code (csharp):
    1.  
    2. FOR /F "tokens=2 delims= " %%A IN (%PROJECT_PATH%\ProjectSettings\ProjectVersion.txt) DO (
    3.     ECHO Picking Unity installation to use...
    4.     ECHO Checking existance of folder C:\Program Files\Unity\Hub\Editor\%%A\*
    5.     IF EXIST "C:\Program Files\Unity\Hub\Editor\%%A\*" (
    6.         SET UNITY="C:\Program Files\Unity\Hub\Editor\"%%A"\Editor\Unity.exe"
    7.        ECHO Correct Unity version found.
    8.    ) ELSE (
    9.        ECHO Couldn't find Unity version %%A
    10.    )
    11. )
    12.  
    You'll have to replace %PROJECT_PATH% with your actual project path.

    If this Unity version is not found, we use a fallback Unity that we know is installed.
     
    dgoyette likes this.
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I like it. I'll give it a shot. I know it might seems silly to care about this, but it's not about saving 15 seconds edit my build script every few months. It's about avoiding the trouble that can arise if I forget to update the build script, and get a bunch of weird build failures that waste my time until I realize I forgot to edit the build script. :) Thanks.