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
}



How to use Urdu/Arabic language in Unity 3D using Unicode Standard?

As we know that computers just deal with numbers therefore they store characters and alphabets by assigning a number for each one.The Unicode Standard provides a unique number for every character no matter what language.Here is the unicode representation of few urdu characters.


See links for complete set of Unicodes of Urdu and Arabic language.
1- http://tabish.freeshell.org/u-font/chart.html
2- https://unicode-table.com/en/#arabic
3- https://en.wikipedia.org/wiki/Arabic_script_in_Unicode

Now let see how to use these unicodes in Unity 3D. To create Urdu word "Tamater"(Tomato in english) by using unicodes of Urdu alphabets Tey,Meem,Alif and Rey follow below steps

1-  Create Text GameObject by GameObject > UI > Text. Use font of your choice I'm using
     "ADOBEARABIC- REGULAR".
2-  Create C# script "UrduWord" and attach with Text GameObject.
3-  Paste the code given below.
   
    Text wordTxt;    
    void Start ()
    {
        wordTxt = GetComponent<Text>();
        MyWord();
    }

    public void MyWord ()
    {
       char tey = '\uFB68';   //tey unicode
       char meem = '\uFEE3';  //meem unicode
       char alif = '\uFE8E';  //alif unicode 
       char tey2 = '\uFB68'
       char rey = '\uFEAE';
    
        wordTxt.text = rey.ToString() + tey2.ToString() + 
                       alif.ToString () + meem.ToString() +
                       tey.ToString();
   
            


4- Hit the play button and you will see output like this



As it is very difficult to add Unicodes every time you want to create new word therefore I've created a text file containing Unicodes of all Urdu alphabets in sequence. Read this file sequentially and create a Dictionary for reusability.
https://www.mediafire.com/file/jkof6mjjxbtk50y/UrduAlphabets.txt

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 ...