Unity: How to automatically add Admob App ID

Admob is one of the most used Ad network used by developers all over the world. In recent sdk update Admob introduced a change which requires developers to add Admob App Id in iOS info.plist and Android’s AndroidManifest.xml file.

In iOS Admob requires you to add GADApplicationIdentifier as key with a value of Admob App Id.

This change can be done programatically in Unity by adding the parameter in PostProcessBuild.

Unity changes for iOS Admob

Create a new script file and save it inside Assets -> Editor folder.

Update the file to add key and value in info.plist after the build is completed programatically.

using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public class PListProcessor : MonoBehaviour
{
#if UNITY_IOS
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget,
                                          string path)
    {
        // Replace with your iOS AdMob app ID. Your AdMob app ID will look
        // similar to this sample ID: ca-app-pub-3940256099942544~1458002511
        string appId = "ca-app-pub-3940256099942544~1458002511"; // Replace with your Admob APP id

        string plistPath = Path.Combine(path, "Info.plist");
        PlistDocument plist = new PlistDocument();

        plist.ReadFromFile(plistPath);
        plist.root.SetString("GADApplicationIdentifier", appId);
        File.WriteAllText(plistPath, plist.WriteToString());
    }
#endif
}

 

 

 

Tags:,

Leave a Reply