Skip to content

StinkySteak/unity-build-environment-config

Repository files navigation

Build Environment Config

a Unity package to manage multiple environments with ease

How to?

  1. Create a class inherits from BaseBuildEnvironment with a [CreateAssetMenu] attribute
    [CreateAssetMenu(fileName = "Platform Environment", menuName = "Data/Platform Environment/Data")]
    public class BuildEnvironment : BaseBuildEnvironment
    {

    }
  1. 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;
    }
  1. 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 
}
  1. 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
{

}
  1. 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
};
  1. 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);
}
  1. 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/";
  1. Lastly, add the window activation method
[MenuItem(MENU_ITEM_PATH)]
public static void SetGameBuild()
    => GetWindow<MainBuildEnvironmentConfigWindow>("Game Build");
  1. 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 Data Creation

  2. Now also create the data by going to Data > Platform Environment > Data

  3. Now add the data to the Platform Environment array Window

  4. To test it out, head over to Tools > Set Release Target (Make sure your PlatformDataContainer is in the correct path!)

Complete Class

 [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);
    }
}

About

A Unity Plugin to manage multiple environments with ease

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages