Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Stop build if not properly connected to Unity Services in the Editor

Discussion in 'Authentication' started by FabioFP, Aug 2, 2022.

  1. FabioFP

    FabioFP

    Joined:
    Jan 25, 2022
    Posts:
    4
    Hi, is there a way to stop a build, perhaps on a Pre Process script, in case the Editor is not properly connected to Unity Services?
    The warning shown in the Build Settings window is a bit too subtle and might be easy to miss. It seems to happen often if you have a poor/unstable internet connection.

    I recently investigated an issue that originated from this (a build gets stuck at startup due to Unity auth errors, apparently because the build was compiled while the Editor was not properly connected to Unity Services).
     

    Attached Files:

  2. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    90
    Hello!
    We opted for a warning to ensure we don't break builds that are in the process of integrating our services but may have not had time to set it fully.

    You can however easily create a preprocess script on your side that will do this!

    Here's an example that should do the trick:

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEditor.Build;
    4. using UnityEditor.Build.Reporting;
    5.  
    6. namespace TestApp.Editor
    7. {
    8.     public class CloudSettingsBuildPreprocess : IPreprocessBuildWithReport
    9.     {
    10.         const string k_UserNameAnonymous = "anonymous";
    11.  
    12.         public int callbackOrder => int.MinValue;
    13.  
    14.         public void OnPreprocessBuild(BuildReport report)
    15.         {
    16.             if (!IsLoggedIn() || !IsProjectBound())
    17.             {
    18.                 throw new BuildFailedException("Cloud settings validation failed! Ensure your project is set and that you are logged in.");
    19.             }
    20.         }
    21.  
    22.         static bool IsLoggedIn()
    23.         {
    24.             return !string.IsNullOrEmpty(CloudProjectSettings.userId) &&
    25.                 !CloudProjectSettings.userName.Equals(k_UserNameAnonymous, StringComparison.InvariantCultureIgnoreCase);
    26.         }
    27.  
    28.         static bool IsProjectBound()
    29.         {
    30.             return !(string.IsNullOrEmpty(CloudProjectSettings.organizationId) ||
    31.                 string.IsNullOrEmpty(CloudProjectSettings.organizationName) ||
    32.                 string.IsNullOrEmpty(CloudProjectSettings.projectId) ||
    33.                 string.IsNullOrEmpty(CloudProjectSettings.projectName));
    34.         }
    35.     }
    36. }
    37.  
     
  3. FabioFP

    FabioFP

    Joined:
    Jan 25, 2022
    Posts:
    4
    Hi @erickb_unity, thanks for your reply, much appreciated!
    I’ll give it a shot tomorrow morning.
     
  4. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    90
    I forgot to mention, but a good way to test this is to sign out of the Unity Hub and then try making a build