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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How can a variable on an object in a scene be set by a build script?

Discussion in 'Unity Build Automation' started by coastalbytes, Apr 21, 2023.

  1. coastalbytes

    coastalbytes

    Joined:
    May 31, 2021
    Posts:
    7
    We have a feature settings script that contains feature flags that can be turned on or off and affect how things behave at runtime. For example, an isBeta flag. If that is turned on, it displays to the user on the UI that it is a beta release. When doing a manual build via the editor, that flag is toggled on the object in the scene, then the build generated.

    For running a build via command line, it is done with a static function that runs BuildPlayer, and the following command (from the docs):
    "C:\Program Files\Unity\Editor\Unity.exe" -quit -batchmode -projectPath "C:\Users\UserName\Documents\MyProject" -executeMethod MyEditorScript.PerformBuild

    Is there a way that static function MyEditorScript.PerformBuild can change a variable in scene? So that we could, say, specify a beta or non-beta build via command line.

    Open to suggestions for alternative ways to implement that type of feature flag. Something like a config file that is read at runtime, like stored in json or something readable is not a great solution as users could easily change it.
     
  2. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    680
    It very much sounds like you need to define some custom conditional defines (compiler directives)...

    https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    Your flags\settings can then be wrapped around the defines, for example...

    Code (CSharp):
    1. #if DEV_BUILD
    2.   float characterSpeed = 2f
    3.   boolean invincible = true;
    4. #elif #PROD_BUILD
    5.   float characterSpeed = 1f
    6.   boolean invincible = false;
    7. #endif
    Hope this helps.
     
    ollieblanks and coastalbytes like this.
  3. coastalbytes

    coastalbytes

    Joined:
    May 31, 2021
    Posts:
    7
    PeachyPixels likes this.