Search Unity

Feature Request Make UnityEditor.BuildPlayerOptions serializable

Discussion in 'Testing & Automation' started by Xarbrough, Aug 1, 2019.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    483
    A nice-to-have improvement would be to make the BuildPlayerOptions serializable. This would help with writing custom build process scripts that have settings serialized as custom assets.

    Currently, I have to implement classes with my custom version of the BuildPlayerOptions struct and then convert to the Unity version by copying each property, which feels like wasted code just because I want to store the values within a ScriptableObject. If the struct were serializable I could simply store it in any ScriptableObject or MonoBehaviour class.

    I know that marking the struct with the Serializable attribute may not be desired because then Unity would have to maintain binary compatibility and put additional effort into this, but maybe it would be possible to make this serializable on the native side like e.g. the Bounds struct, which could not be saved in MonoBehaviour scripts in the past but in some Unity version support was added to show the values in the inspector without using the Serializable attribute.

    Example of why it would be nice to directly serialize the options:

    Code (CSharp):
    1. public static class MyCustomBuildProcess
    2. {
    3.     [MenuItem("Builds/Run Build")]
    4.     public static void RunCustomBuild()
    5.     {
    6.         BuildPlayerOptions options = GetOptions();
    7.         BuildPipeline.BuildPlayer(options);
    8.     }
    9.  
    10.     private static BuildPlayerOptions GetOptions()
    11.     {
    12.         // Load configuration files (ScriptableObjects)
    13.         // with data to produce BuildPlayerOptions.
    14.         return myCustomBuildOptions;
    15.     }
    16.  
    17.     public class MyCustomBuildOptions : ScriptableObject
    18.     {
    19.         public string[] scenes;
    20.         public BuildTarget target;
    21.         // and so on...
    22.  
    23.         public BuildPlayerOptions AsUnityBuildPlayerOptions()
    24.         {
    25.             return new BuildPlayerOptions()
    26.             {
    27.                 scenes = this.scenes,
    28.                 target = this.target
    29.                 // an so on...
    30.             };
    31.         }
    32.     }
    33. }
     
    Last edited: Aug 1, 2019
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,222
    You can probably create something a bit more automatic instead of manually copying each property.
    My asset UCmd (see link in my signature) is soon going to be updated with more features to help with building a player.

    I can look into adding something like what you want :)

    Stay tuned for more updates on this.
     
    Last edited: Aug 8, 2019