157 lines
5.5 KiB
C#
157 lines
5.5 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using System.Xml;
|
||
|
using System.Xml.Linq;
|
||
|
using Google.MiniJSON;
|
||
|
using UnityEditor;
|
||
|
using UnityEditor.Android;
|
||
|
using UnityEditor.Build;
|
||
|
using UnityEngine;
|
||
|
|
||
|
#if UNITY_ANDROID
|
||
|
|
||
|
namespace WZ
|
||
|
{
|
||
|
public class Android : IPostGenerateGradleAndroidProject
|
||
|
{
|
||
|
|
||
|
public int callbackOrder => int.MaxValue;
|
||
|
|
||
|
public void OnPostGenerateGradleAndroidProject(string path)
|
||
|
{
|
||
|
#if UNITY_2019_3_OR_NEWER
|
||
|
AddAdmobAppId(path);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
private void AddAdmobAppId(string path)
|
||
|
{
|
||
|
var appManifestPath = Path.Combine(path, "src/main/AndroidManifest.xml");
|
||
|
|
||
|
var manifestFile = new XmlDocument();
|
||
|
manifestFile.Load(appManifestPath);
|
||
|
const string gmsAdsApplicationID = "com.google.android.gms.ads.APPLICATION_ID";
|
||
|
var admobAppId = GetMetaDataValue(manifestFile, gmsAdsApplicationID);
|
||
|
|
||
|
var projectAdmobAppId = GetAdmobAppId();
|
||
|
LoggerUtils.Debug("Get admob id:"+projectAdmobAppId);
|
||
|
|
||
|
if (string.IsNullOrEmpty(admobAppId))
|
||
|
{
|
||
|
AddMetaDataValue(manifestFile, gmsAdsApplicationID, projectAdmobAppId);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
UpdateMetaDataValue(manifestFile, gmsAdsApplicationID, projectAdmobAppId);
|
||
|
}
|
||
|
|
||
|
var androidManifestFileArray = EditorFileUtils.GetAllFileWhereSuffix(path, "AndroidManifest.xml");
|
||
|
foreach (var tempPath in androidManifestFileArray)
|
||
|
{
|
||
|
var newText = File.ReadAllText(tempPath).Replace("INSERT_YOUR_ADMOB_APP_ID_HERE", projectAdmobAppId);
|
||
|
File.WriteAllText(tempPath, newText);
|
||
|
}
|
||
|
|
||
|
const string OPTIMIZE_INITIALIZATION = "com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION";
|
||
|
|
||
|
var optimizeInitialization = GetMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION);
|
||
|
if (string.IsNullOrEmpty(optimizeInitialization))
|
||
|
{
|
||
|
AddMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION, "true");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
UpdateMetaDataValue(manifestFile, OPTIMIZE_INITIALIZATION, "true");
|
||
|
}
|
||
|
|
||
|
const string OPTIMIZE_AD_LOADING = "com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING";
|
||
|
var optimizeAdLoading = GetMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING);
|
||
|
if (string.IsNullOrEmpty(optimizeAdLoading))
|
||
|
{
|
||
|
AddMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING, "true");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
UpdateMetaDataValue(manifestFile, OPTIMIZE_AD_LOADING, "true");
|
||
|
}
|
||
|
|
||
|
manifestFile.Save(appManifestPath);
|
||
|
|
||
|
|
||
|
if (string.IsNullOrEmpty(projectAdmobAppId))
|
||
|
{
|
||
|
// 显示错误提示框
|
||
|
EditorUtility.DisplayDialog("Error", "Google advertising channel was used, but admobId was not configured in the configuration file.", "Ok");
|
||
|
throw new Exception("Google advertising channel was used, but admobId was not configured in the configuration file.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private bool UpdateMetaDataValue(XmlDocument manifest, string name, string value)
|
||
|
{
|
||
|
var xpath = $"/manifest/application/meta-data[@android:name='{name}']";
|
||
|
var node = manifest.DocumentElement?.SelectSingleNode(xpath, GetNamespaceManager(manifest));
|
||
|
if (!(node is XmlElement element)) return false;
|
||
|
var attributeNode = element.GetAttributeNode("android:value");
|
||
|
if (attributeNode != null)
|
||
|
{
|
||
|
attributeNode.Value = value;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
private bool AddMetaDataValue(XmlDocument manifest, string name, string value)
|
||
|
{
|
||
|
var metaNode = manifest.CreateElement("meta-data");
|
||
|
|
||
|
var nameAttribute = manifest.CreateAttribute("android", "name", "http://schemas.android.com/apk/res/android");
|
||
|
nameAttribute.InnerText = name;
|
||
|
metaNode.SetAttributeNode(nameAttribute);
|
||
|
|
||
|
var valueAttribute = manifest.CreateAttribute("android", "value", "http://schemas.android.com/apk/res/android");
|
||
|
valueAttribute.InnerText = value;
|
||
|
metaNode.SetAttributeNode(valueAttribute);
|
||
|
|
||
|
|
||
|
var applicationNode = manifest.SelectSingleNode("/manifest/application");
|
||
|
if (applicationNode == null) return false;
|
||
|
applicationNode.AppendChild(metaNode);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static string GetAdmobAppId()
|
||
|
{
|
||
|
return FileParse.GetConfigByKey(FileParse.KEY_Admob_AppID);
|
||
|
}
|
||
|
|
||
|
private static string GetMetaDataValue(XmlDocument manifest, string name)
|
||
|
{
|
||
|
var xpath = $"/manifest/application/meta-data[@android:name='{name}']";
|
||
|
var node = manifest.DocumentElement?.SelectSingleNode(xpath, GetNamespaceManager(manifest));
|
||
|
if (node is XmlElement element)
|
||
|
{
|
||
|
return element.GetAttribute("android:value");
|
||
|
}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
private static XmlNamespaceManager GetNamespaceManager(XmlDocument manifest)
|
||
|
{
|
||
|
var namespaceManager = new XmlNamespaceManager(manifest.NameTable);
|
||
|
namespaceManager.AddNamespace("android", "http://schemas.android.com/apk/res/android");
|
||
|
return namespaceManager;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
#endif
|