BuildPreProcessor.cs 2.1 KB

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