- Home /
Accessing "server build" setting through code
I'm making an online multiplayer game with dedicated server and I find myself building the application very often as well as switching client and server build. To avoid having to check and uncheck scenes manually every time I'm trying to automate the process through an editor script.
public static void BuildServer()
{
//Scriptable object containing the scenes
AutoBuildSettings settings = AssetDatabase.LoadAssetAtPath<AutoBuildSettings>($@"{BUILD_SETTINGS_PATH}Server");
//Set "server build" to true
BuildPipeline.BuildPlayer(settings.scenes, @"ServerBuild", BuildTarget.StandaloneWindows, BuildOptions.None);
}
I cannot find a way to set or even access the "server build" variable, does anyone know where it is if it is somewhere?
Answer by Captain_Pineapple · Oct 12, 2021 at 12:15 PM
If i am not mistaken unity still has not really adressed this issue as the "server-build" is not really what most people expect it to be.
as can be read here in the build settings docu server build does the following:
Enable this checkbox to build the Player for server use and with no visual elements (headless) without the need for any command line options. When you enable this option, Unity builds managed scripts with the UNITY_SERVER define, which means you can write server-specific code for your applications. [...]
As you can see this checkbox does not really do anything except for 2 things:
add the precompiler definition
UNITY_SERVER
add the build option
EnableHeadlessMode
so the latter can be set as value of the buildoption enum as argument for your build pipeline.
Let me know if that helped.
I'll have to manually check the box then, but at least I won't have to manage all the scenes and changing the path every time. Thanks!
you don't have to do that. You can use scripting to access and modify precompiler definitions and add the enableHeadlessmode flag by script as well.
The only critical issue is that setting percompiler definitions is "permanent" so you also have to manually remove the flag as well after the build has completed or failed.
From my own buildpipeline:
public static void addBuildScriptingDefines(string defineString)
{
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone) + ";" + defineString);
}
same can then be done to iterate the define string to remove a given value.
I found a simpler way around my problem. To build the server I build normally, and to build the client I use a script, that way I don't have to mess with the defines.
Answer by HarryBGamer2570 · 5 days ago
Debug.Log(EditorUserBuildSettings.enableHeadlessMode);
Is what I found, but I am not sure if you can rewrite it though.
It's really useful especially if you need to know if it is a server build or not DURING/BEFORE the build process
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
class PreBuild : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log(EditorUserBuildSettings.enableHeadlessMode);
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
My android app build has more bugs on it than the editor/deveploment version 0 Answers
Script works in Game View, but not in build 0 Answers
Script work in editor but not in build 0 Answers