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

PreExport asset changes not included in build

Discussion in 'Unity Build Automation' started by AlexNanomonx, Apr 6, 2017.

  1. AlexNanomonx

    AlexNanomonx

    Joined:
    Jan 4, 2017
    Posts:
    41
    I'm trying to display the Git commit hash in my development builds so we can accurately report which version bugs are detected on. I've been using the PreExport functionality to get the commit hash and save it in a serialised class .asset file.

    Code (CSharp):
    1. public static class CloudBuildHelper
    2.     {
    3.         private const string PROJECT_SETTINGS_INSTALLER_LOCATION = @"Assets\Resources\Installers\ProjectSettingsInstaller.asset";
    4.      
    5.         /// <summary>
    6.         /// This is a helper function which can be run in the preexport step of the Cloud build. It takes the manifest provided by
    7.         /// the Cloud Build which contains the commit hash and gives it to the project settings to be displayed in every scene
    8.         /// </summary>
    9.         /// <param name="manifest"></param>
    10.         public static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest)
    11.         {
    12.             Debug.Log("Running PreExport Script");
    13.  
    14.             try
    15.             {
    16.                 string commit = null;
    17.                 if (!manifest.TryGetValue<string>("scmCommitId", out commit))
    18.                 {
    19.                     Debug.LogError("PreExport - Unable to get build version number.");
    20.                 }
    21.  
    22.                 Debug.Log("PreExport - Commit Hash: " + commit);
    23.  
    24.                 AssetDatabase.StartAssetEditing();
    25.  
    26.                 var installer =
    27.                     AssetDatabase.LoadAssetAtPath<ProjectSettingsInstaller>(PROJECT_SETTINGS_INSTALLER_LOCATION);
    28.  
    29.                 if (installer != null)
    30.                 {
    31.                     installer.ProjectSettings.CommitHash = commit;
    32.                     Debug.LogError("PreExport - Set commit in ProjectSettingsInstaller.asset at " +
    33.                                    PROJECT_SETTINGS_INSTALLER_LOCATION
    34.                                    + " to " + commit);
    35.                 }
    36.                 else
    37.                 {
    38.                     Debug.LogError("PreExport - Can't find ProjectSettingsInstaller.asset at " +
    39.                                    PROJECT_SETTINGS_INSTALLER_LOCATION);
    40.                 }
    41.  
    42.             }
    43.             catch (Exception e)
    44.             {
    45.                 Debug.LogError("PreExport - Unable to set build version number. Got error " + e + ", " + e.Message);
    46.             }
    47.             finally
    48.             {
    49.                 Debug.Log("PreExport - finishing editing assets");
    50.                 //I'm not sure which of the next three function I *NEED* to call, but it seems to work when I call all of them
    51.                 AssetDatabase.StopAssetEditing();
    52.                 AssetDatabase.SaveAssets();
    53.                 AssetDatabase.Refresh();
    54.             }
    55.         }
    56.     }

    As far as I can tell that code is executing correctly. It hit all the right logs and none of the error or warnings. However, when I run the built project, the CommitHash variable is empty. I'm pretty sure my asset is being written to correctly because if I remove the CloudBuild specific code (it's a bit annoying that it doesn't compile locally btw) and run it in the editor on my local machine, the ProjectSettingsInstaller.asset file is changed in the way I'd expect. Yet I don't see those changes in the build output.

    Unity version: 5.5.3f1
    Source control: git
    Build target: Windows x64
    Build log:
    Code (Boo):
    1.  
    2. 24884: [Unity] Running PreExport Script
    3. 24885: [Unity] UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    4. 24886: [Unity] UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    5. 24887: [Unity] UnityEngine.Logger:Log(LogType, Object)
    6. 24888: [Unity] UnityEngine.Debug:Log(Object)
    7. 24889: [Unity] CloudBuild.CloudBuildHelper:PreExport(BuildManifestObject) (at Assets/Editor/CloudBuild/PreExport.cs:20)
    8. 24890: [Unity] System.Reflection.MonoMethod:InternalInvoke(Object, Object[], Exception&)
    9. 24891: [Unity] UnityEditor.CloudBuild.UnityReflector:CallStaticEditorMethod(String, Boolean, Object[])
    10. 24892: [Unity] UnityEditor.CloudBuild.Builder:Build()
    11. 24893: [Unity] (Filename: Assets/Editor/CloudBuild/PreExport.cs Line: 20)
    12. 24894: [Unity] PreExport - Commit Hash: 77694085d19737ef6e19defca35cceec4b95e1d0
    13. 24895: [Unity] UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    14. 24896: [Unity] UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    15. 24897: [Unity] UnityEngine.Logger:Log(LogType, Object)
    16. 24898: [Unity] UnityEngine.Debug:Log(Object)
    17. 24899: [Unity] CloudBuild.CloudBuildHelper:PreExport(BuildManifestObject) (at Assets/Editor/CloudBuild/PreExport.cs:30)
    18. 24900: [Unity] System.Reflection.MonoMethod:InternalInvoke(Object, Object[], Exception&)
    19. 24901: [Unity] UnityEditor.CloudBuild.UnityReflector:CallStaticEditorMethod(String, Boolean, Object[])
    20. 24902: [Unity] UnityEditor.CloudBuild.Builder:Build()
    21. 24903: [Unity] (Filename: Assets/Editor/CloudBuild/PreExport.cs Line: 30)
    22. 24904: [Unity] PreExport - Set commit in ProjectSettingsInstaller.asset at Assets\Resources\Installers\ProjectSettingsInstaller.asset to 77694085d19737ef6e19defca35cceec4b95e1d0
    23. 24905: [Unity] PreExport - finishing editing assets
    24. 24906: [Unity] UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    25. 24907: [Unity] UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    26. 24908: [Unity] UnityEngine.Logger:Log(LogType, Object)
    27. 24909: [Unity] UnityEngine.Debug:Log(Object)
    28. 24910: [Unity] CloudBuild.CloudBuildHelper:PreExport(BuildManifestObject) (at Assets/Editor/CloudBuild/PreExport.cs:57)
    29. 24911: [Unity] System.Reflection.MonoMethod:InternalInvoke(Object, Object[], Exception&)
    30. 24912: [Unity] UnityEditor.CloudBuild.UnityReflector:CallStaticEditorMethod(String, Boolean, Object[])
    31. 24913: [Unity] UnityEditor.CloudBuild.Builder:Build()
    32. 24914: [Unity] (Filename: Assets/Editor/CloudBuild/PreExport.cs Line: 57)