a Unity package to manage multiple environments with ease
- Create a class inherits from
BaseBuildEnvironment
with a[CreateAssetMenu]
attribute
[CreateAssetMenu(fileName = "Platform Environment", menuName = "Data/Platform Environment/Data")]
public class BuildEnvironment : BaseBuildEnvironment
{
}
- Define the properties to be included in the environment. e.g analytics, development build, in game cheat with the property getter too
[CreateAssetMenu(fileName = "Platform Environment", menuName = "Data/Platform Environment/Data")]
public class BuildEnvironment : BaseBuildEnvironment
{
[SerializeField] private bool _developmentBuild;
[SerializeField] private bool _enableAnalytics;
public bool DevelopmentBuild => _developmentBuild;
public bool EnableAnalytics => _enableAnalytics;
}
- Go create the container for our environments, so we can register the available environments for our project.
[CreateAssetMenu(fileName = "BuildEnvironmentContainer", menuName = "Data/Platform Environment/Container")]
public class BuildEnvironmentContainer : BaseBuildEnvironmentDataContainer
{
// Leave it empty
// If you have a [Button] custom editor e.g Odin Inspector or Naughty Attributes
// You can override OpenMenu() and add the [Button] attribute for shortcuts opening the editor menu later on
}
- Now Let's Create the a editor window that we can use to toggle the environment. Create a class derives from
BaseBuildEnvironmentConfigWindow
public class MainBuildEnvironmentConfigWindow : BaseBuildEnvironmentConfigWindow
{
}
- In this class, we have to override several things such as the symbol definition list & adding the list manually
private const string DEVELOPMENT = "DEVELOPMENT";
private const string ANALYTICS = "ANALYTICS";
public override string[] SYMBOLS => new string[] {
DEVELOPMENT,
ANALYTICS
};
- Now Override the Update List, this is where you want to add the active symbols
protected override void UpdateList(BaseBuildEnvironment activeEnvironment, List<string> symbolList)
{
var env = activeEnvironment as BuildEnvironment;
if (env.DevelopmentBuild)
symbolList.Add(DEVELOPMENT);
if (env.EnableAnalytics)
symbolList.Add(ANALYTICS);
}
- Let's also set where the editor should scan the asset path, you can customize the path by your liking
protected override string ENVIRONMENT_DATA_CONTAINER_PATH => "Assets/Data/Environment/";
- Lastly, add the window activation method
[MenuItem(MENU_ITEM_PATH)]
public static void SetGameBuild()
=> GetWindow<MainBuildEnvironmentConfigWindow>("Game Build");
-
All the class has been setup, now lets try it up by going to create our environment data containe by right clicking on Project Folder, Data > Platform Environment > Container
-
Now also create the data by going to Data > Platform Environment > Data
-
To test it out, head over to Tools > Set Release Target (Make sure your
PlatformDataContainer
is in the correct path!)
[CreateAssetMenu(fileName = "Platform Environment", menuName = "Data/Platform Environment/Data")]
public class BuildEnvironment : BaseBuildEnvironment
{
[SerializeField] private bool _developmentBuild;
[SerializeField] private bool _enableAnalytics;
public bool DevelopmentBuild => _developmentBuild;
public bool EnableAnalytics => _enableAnalytics;
}
public class MainBuildEnvironmentConfigWindow : BaseBuildEnvironmentConfigWindow
{
private const string DEVELOPMENT_SCRIPT_SYMBOL = "DEVELOPMENT";
private const string FTUE_SCRIPT_SYMBOL = "FTUE";
public override string[] SYMBOLS => new string[] {
DEVELOPMENT_SCRIPT_SYMBOL,
FTUE_SCRIPT_SYMBOL,
};
protected override string ENVIRONMENT_DATA_CONTAINER_PATH => "Assets/Data/Environment/";
[MenuItem(MENU_ITEM_PATH)]
public static void SetGameBuild()
=> GetWindow<MainBuildEnvironmentConfigWindow>("Game Build");
protected override void UpdateList(BaseBuildEnvironment activeEnvironment, List<string> symbolList)
{
var env = activeEnvironment as BuildEnvironment;
if (env.DevelopmentBuild)
symbolList.Add(DEVELOPMENT_SCRIPT_SYMBOL);
if (env.EnableAnalytics)
symbolList.Add(INGAME_CHEAT_SCRIPT_SYMBOL);
}
}