BuildPreProcessor.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using GooglePlayServices;
  6. using UnityEditor.Build;
  7. using UnityEditor.Build.Reporting;
  8. namespace GoogleMobileAds.Editor
  9. {
  10. /// <summary>
  11. /// Pre-processor that performs common setup tasks for all platforms before a build.
  12. /// </summary>
  13. public class BuildPreProcessor : IPreprocessBuildWithReport
  14. {
  15. // Set the callback order to be before EDM4U.
  16. // https://github.com/googlesamples/unity-jar-resolver/blob/master/source/AndroidResolver/src/PlayServicesPreBuild.cs#L39
  17. public int callbackOrder { get { return -1; } }
  18. private readonly static string _linkXmlAssetsPath =
  19. Path.Combine(Application.dataPath, "GoogleMobileAds", "link.xml");
  20. public void OnPreprocessBuild(BuildReport report)
  21. {
  22. // Unity's managed code stripping process does not inherently process `link.xml` files
  23. // in UPM packages. This pre-processor copies the `link.xml` file from the UPM package
  24. // to the Unity project's `Assets/GoogleMobileAds` directory if it does not exist.
  25. if (!File.Exists(_linkXmlAssetsPath))
  26. {
  27. CopyLinkXml();
  28. }
  29. }
  30. private static void CopyLinkXml()
  31. {
  32. if (!AssetDatabase.IsValidFolder(Path.Combine("Assets", "GoogleMobileAds")))
  33. {
  34. AssetDatabase.CreateFolder("Assets", "GoogleMobileAds");
  35. }
  36. var pathUtils = ScriptableObject.CreateInstance<EditorPathUtils>();
  37. if (pathUtils.IsPackageRootPath())
  38. {
  39. string parentDirectoryPath = pathUtils.GetParentDirectoryAssetPath();
  40. string linkXmlPackagePath = Path.Combine(parentDirectoryPath, "link.xml");
  41. if(String.IsNullOrEmpty(linkXmlPackagePath))
  42. {
  43. Debug.LogWarning("link.xml not found in the package.");
  44. return;
  45. }
  46. AssetDatabase.CopyAsset(linkXmlPackagePath, _linkXmlAssetsPath);
  47. }
  48. AssetDatabase.Refresh();
  49. }
  50. }
  51. }