I'm trying to delay the build process in Unity when building locally.
Before building I want to remove some packages via the Client.Remove
method in a prebuild script. The build should only continue after the packages were removed.
The Prebuild class looks like this:
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;
[Serializable]
public class PreBuildProcessor : IPreprocessBuildWithReport {
[SerializeField] private static RemoveRequest request;
[SerializeField] private Queue<string> packages = new Queue<string>();
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report) {
Debug.Log("Removing unused scripts...");
RemovePackages();
}
private void RemovePackages() {
#if !ADS_ENABLED
packages.Enqueue("com.unity.ads");
#endif
#if !ANALYTICS_ENABLED
packages.Enqueue("com.unity.analytics");
#endif
#if !IAP_ENABLED
packages.Enqueue("com.unity.purchasing");
#endif
#if !REVIEW_ENABLED
packages.Enqueue("com.google.play.review");
#endif
#if !NOTIFICATIONS_ENABLED
packages.Enqueue("com.unity.mobile.notifications");
#endif
if (packages.Count != 0) {
request = Client.Remove(packages.Dequeue());
EditorApplication.update += PackageRemovalProgress;
}
}
private void PackageRemovalProgress() {
Debug.Log("Removing....");
if (request.IsCompleted) {
switch (request.Status) {
case StatusCode.Failure: // couldn't remove package
Debug.LogError("Couldn't remove package '" + request.PackageIdOrName + "': " + request.Error.message);
break;
case StatusCode.InProgress:
break;
case StatusCode.Success:
Debug.Log("Removed package: " + request.PackageIdOrName);
break;
}
if (packages.Count > 0) {
var nextRequestStr = packages.Dequeue();
Debug.Log("Requesting removal of '" + nextRequestStr + "'.");
request = Client.Remove(nextRequestStr);
} else { // no more packages to remove
EditorApplication.update -= PackageRemovalProgress;
}
}
return;
}
}
#endif
The first two packages are removed and the "Removed Package" log message is printed into the console. The third package is removed but I don't get a log message. The last two packages are not removed at all and I don't get any notification or log that anything failed.
Is there any way to delay the build until all packages are removed?