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

Thursday, August 3, 2017

How to share Screenshot/Image using Facebook Unity SDK

We can login to facebook using following two functions provided by Facebook Unity SDK
1- FB.LogInWithReadPermissions
2- FB.LogInWithPublishPermissions

These two function use two type of permissions read and publish from more than 30 permissions provided by Facebook.If we want to share a link and description we can do simply by using FB.ShareLink which open a share dialogue in our app.This dialogue have everything if I'm not wrong except the option to upload stuff from local storage.So what should we do to share screenshot or image from device.It's very simple.we can do this in two steps


  1. First login using FB.LogInWithPublishPermissions by adding "publish_actions" permission in  parameters.
  2. Use Facebook Graph API to upload image
NOTE: If the image you want to share is imported in Unity Editor then make sure it has read enabled in import setting

Her is code snippet to post Screenshot/Image on Facebook
  1. void Start()
  2. {
  3.      FB.Init(OnInit);
  4. }

  5. private void OnInit()
  6. {
  7.      LoginToFB();
  8. }

  9. public void LoginToFB()
  10. {
  11.       FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, LoginResult);
  12. }

  13. private void LoginResult(IResult result)
  14. {
  15.     //TODO:do what you want
  16. }

  17. // Call this function on the sharing event
  18. public void ShareScreenShot(Texture2D screenShot)
  19. {
  20.       byte[] encodedScreenShot = screenShot.EncodeToPNG();
  21.      
  22.       var wwwForm = new WWWForm();
  23.       wwwForm.AddBinaryData("image", encodedScreenShot, "ScreenShot.png");
  24.       wwwForm.AddField("message", "Write description here");
  25.       //calling graph api 
  26.       FB.API("me/photos", HttpMethod.POST, ShareScreenShotCallback, wwwForm);
  27. }
  28. void ShareScreenShotCallback(IResult result)
  29. {
  30.     //TODO:do what you want
  31. }

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