Saturday, March 31, 2018

Changing Build Setting Dynamically/Programmatically in Unity 3D

Suppose we are publishing app on three platforms Google Play, iTunes Connect and Amazon. For every single change we have to make a separate build with different Player Settings(e.g. Bundle Id, App Name and Icon etc.) for each platform. It is very tedious and error prone to edit Player Settings manually each time. So we can do it dynamically by writing an Editor script.

Check how custom window looks like.As in my case there are two builds for each platform Simple and Christmas therefore I've added Christmas App toggle.


To create this custom window create a C# class "MyWindow" and inherit it from EditorWindow and paste the code given below.

   
    bool iosBuild = true;
    bool googleBuild;
    bool amazonBuild;
    bool christmasApp;
  
    // Add menu item named "My Window" to the Window menu
    [MenuItem("Window/Custom Player Settings")]
    public static void ShowWindow()
    {
        //Show existing window instance. If one doesn't exist, make one.
        EditorWindow.GetWindow(typeof(MyWindow));
    }

    
    void OnGUI()
    {
        iosBuild = EditorGUILayout.Toggle("iOS Build", iosBuild && 
                                          !googleBuild && !amazonBuild);
        googleBuild = EditorGUILayout.Toggle("Google Build", googleBuild && 
                                             !iosBuild && !amazonBuild);
        amazonBuild = EditorGUILayout.Toggle("Amazon Build", amazonBuild && 
                                             !iosBuild && !googleBuild);
        christmasApp = EditorGUILayout.ToggleLeft("Christmas App", christmasApp);

        if (GUILayout.Button("Change Build Setting"))
        {
            if (iosBuild)
                IOSSetting(christmasApp);
            if (googleBuild)
                GoogleSetting(christmasApp);
            if (amazonBuild)
                AmazonSetting(christmasApp);
        }
    }


Here ShowWindow function is creating window and OnGUI populating it.The MenuItem attribute turns any static function into a menu command and help in adding menu items to main menu and inspector. 

There are three boolean variables one for each platform. iosBuild toggle is set on/true when other toggles are off/false and vice versa. When there is click event on "Change Build Setting" it calls corresponding setting function by checking boolean variables. 

Add following functions in your class.



void IOSSetting(bool isChristmasVersion)
{
    if (isChristmasVersion)
    {
        PlayerSettings.applicationIdentifier = "com.mycompany.myapp.christmas";
        PlayerSettings.productName = "MyApp Christmas";
        PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Unknown, 
                       new Texture2D[] { Resources.Load("icon2") as Texture2D });

    }
    else
    {
        PlayerSettings.applicationIdentifier = "com.mycompany.myapp";
        PlayerSettings.productName = "MyApp";
        PlayerSettings.SetIconsForTargetGroup(BuildTargetGroup.Unknown,
                       new Texture2D[] { Resources.Load("icon1") as Texture2D });
    }
}
void GoogleSetting(bool isChristmasVersion)
{
    // add your code
}
void AmazonSetting(bool isChristmasVersion)
{
    // add your code
}



1 comment:

  1. JackpotCity Casino NJ Review - JT Hub
    JackpotCity 창원 출장마사지 Casino NJ 계룡 출장안마 bonus code: JTG2021 offers 100% up to $1,000 bonus 남양주 출장샵 + 125 Free Spins up to 시흥 출장안마 $1000. Valid for New Jersey players at JackpotCity.com.Minimum Deposit: $20Deposit Methods: Interac + moreDeposit Methods: Interac + more 양산 출장안마

    ReplyDelete

Changing Build Setting Dynamically/Programmatically in Unity 3D

Suppose we are publishing app on three platforms Google Play, iTunes Connect and Amazon. For every single change we have to make a separate ...