Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity 2021.2 get current NamedBuildTarget

Discussion in 'Scripting' started by Infenix, Jan 25, 2022.

  1. Infenix

    Infenix

    Joined:
    May 5, 2019
    Posts:
    7
    I recently started to work with Unity 2021.2 and I found out BuildTargetGroup was being replaced by NamedBuildTarget. Previously, the currently targeted BuildTargetGroup could be retrieved from EditorUserBuildSettings.activeBuildTarget. In Unity 2021.2, I can't figure out what is supposed to replace this function.

    How do you retrieve the currently targeted NamedBuildTarget ?
     
  2. JoelAtMoodkie

    JoelAtMoodkie

    Joined:
    Oct 18, 2009
    Posts:
    895
    I've been trying to find the solution to this but to no avail, seems like a major oversight from Unity.

    I put together the following code to get all NamedBuildTargets (excluding 'Unknown') as in my case having all targets will suffice (I'm setting scripting define symbols which aren't platform specific).

    Code (CSharp):
    1. static List<NamedBuildTarget> GetAllNamedBuildTargets()
    2. {
    3.     var staticFields = typeof(NamedBuildTarget).GetFields(BindingFlags.Public | BindingFlags.Static);
    4.     var buildTargets = new List<NamedBuildTarget>();
    5.  
    6.     foreach (var staticField in staticFields)
    7.     {
    8.         // We exclude 'Unknown' because this can throw errors when used with certain methods.
    9.         if (staticField.Name == "Unknown")
    10.             continue;
    11.  
    12.         if (staticField.FieldType == typeof(NamedBuildTarget))
    13.             buildTargets.Add((NamedBuildTarget)staticField.GetValue(null));
    14.     }
    15.  
    16.     return buildTargets;
    17. }
     
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    I don't see the problem. In Unity 2021.2 and 2021.3 LTS you can still call
    EditorUserBuildSettings.selectedBuildTargetGroup
    and
    EditorUserBuildSettings.activeBuildTarget


    They haven't been removed nor have they been marked obsolete so how is this "replaced"?
    I haven't seen
    NamedBuildTarget
    before. Where is it used?

    Also looking into the
    NamedBuildTarget
    struct there is a public static method
    FromBuildTargetGroup
    that converts a
    BuildTargetGroup
    to a
    NamedBuildTargetGroup
     
  4. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    It's used in PlayerSettings.GetScriptingDefineSymbolsForGroup which is said to be removed in the future in the documentation. And PlayerSettings.SetScriptingDefineSymbols accepts a NamedBuildTarget.

    It does seem like there are a few oversights in the API because, well it leads to really verbose conversion code like this:

    Code (CSharp):
    1. private static void EditSymbols(Func<string, string, string> action, string value)
    2. {
    3.     var target = EditorUserBuildSettings.activeBuildTarget;
    4.     var group = BuildPipeline.GetBuildTargetGroup(target);
    5.     var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(group);
    6.     string symbols = PlayerSettings.GetScriptingDefineSymbols(namedBuildTarget);
    7.     symbols = action(symbols, value);
    8.     PlayerSettings.SetScriptingDefineSymbols(namedBuildTarget, symbols);
    9. }
    It seems there should be a more official or better way to do these things, but they are internal:

    upload_2022-5-6_18-5-13.png
     
  5. iMer

    iMer

    Joined:
    May 21, 2013
    Posts:
    29
    Just came across this exact issue as well since our old CI build script was unable to set scripting defines for the new dedicated server "sub target"
    Apparently you're meant to use the new API with the named build target and just use NamedBuildTarget.Server and THAT can add scripting defines for the server build

    simply. genious.
     
  6. RuanCardoso

    RuanCardoso

    Joined:
    Jul 9, 2017
    Posts:
    7
    Code (CSharp):
    1. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
    2. BuildTargetGroup targetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
    3. var namedBuildTarget = UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(targetGroup);
     
  7. PyrateAkananto

    PyrateAkananto

    Joined:
    May 8, 2018
    Posts:
    8
    @Xarbrough Thank you very much for the hint on that internal function. Its original source code seems to be:
    https://github.com/Unity-Technologi...r/Mono/BuildPipeline/NamedBuildTarget.cs#L122

    To fix my current issues I implemented a function very similar to that internal function and put it into my own sources.

    Nevertheless I hope Unity will clean up this mess with old enums and new enums and deprecated functions and broken conversion functions.

    There already exists an official issue regarding this problem and setting Scripting Defines Symbols. It is in the "fixed" state which I find hard to believe:
    https://issuetracker.unity3d.com/is...lation-when-dedicated-server-platform-is-used

    @RuanCardoso The code you posted uses the conversion function UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup which unfortunately does ignore the StandaloneBuildSubtarget (SUB!) and therefore is incomplete.
     
  8. IndieMarc

    IndieMarc

    Joined:
    Jan 16, 2017
    Posts:
    181
    No, this doesn`t work, it returns "Standalone" when my target platform is set to Dedicated Server.

    There is no way to know if the current platform is Server or if it is Standalone? Seems like a major design flaw in the new unity versions.

    Right now when you try to automatically assign scripting define symbols when people are importing a unity asset. It won`t work for the Server build. Since activeBuildTarget returns Standalone when it should return DedicatedServer.
     
  9. vakuor

    vakuor

    Joined:
    Jul 9, 2018
    Posts:
    5
    Simple workaround I made:

    Code (CSharp):
    1.             public static NamedBuildTarget CurrentNamedBuildTarget
    2.             {
    3.                 get
    4.                 {
    5. #if UNITY_SERVER
    6.                     return NamedBuildTarget.Server;
    7. #else
    8.                     BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
    9.                     BuildTargetGroup targetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
    10.                     NamedBuildTarget namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(targetGroup);
    11.                     return namedBuildTarget;
    12. #endif
    13.                 }
    14.             }
     
    Alverik likes this.