Search Unity

Change PlayerSettings Static & Dynamic batching properties programmatically

Discussion in 'Scripting' started by stephero, Mar 8, 2019.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,

    I would like to change the following properties (Static Batching and Dynamic Batching) from the player's settings from script:
    Screenshot_1.png

    But I cannot find them in the editor's PlayerSettings scripting API: https://docs.unity3d.com/ScriptReference/PlayerSettings.html
    Most of the other properties are here (colorSpace, gpuSkinning...), but not these two!

    What am I missing?

    Bonus question: do you know if it's possible to retrieve these properties (read only) at runtime (so not through the UnityEditor API)?

    Thanks
     
  2. einWikinger

    einWikinger

    Joined:
    Jul 30, 2013
    Posts:
    97
    There is no public API for setting batching options, but you can use reflection (on your own risk) to access the internal API used by Unity:

    Code (CSharp):
    1. public static void SetBatchingForPlatform(BuildTarget platform, int staticBatching, int dynamicBatching)
    2. {
    3.     var method = typeof(PlayerSettings).GetMethod("SetBatchingForPlatform", BindingFlags.Static | BindingFlags.Default | BindingFlags.NonPublic);
    4.    
    5.     if (method == null)
    6.     {
    7.         throw new NotSupportedException("Setting batching per platform is not supported");
    8.     }
    9.  
    10.     object[] args = new object[]
    11.     {
    12.         platform,
    13.         staticBatching,
    14.         dynamicBatching
    15.     };
    16.  
    17.     method.Invoke(null, args);
    18. }