iOS/AndroidではUnityIAPを使っているけれど、Windowsでは使用していない。
それにも関わらず、UnityIAPが有効になっていると勝手にUnity Analyticsも有効になり気持ち悪いから
Windowsビルドの時だけパッケージ依存を削除したい・・・みたいなことありますよね。
(※UnityIAPのUnity Analytics依存は4.7.0で無くなりました!めでたい!・・・いや、なんで入ってたの?)
そんなときに使える方法を2つ紹介します。
正攻法
UnityEditor.PackageManager.Clientから操作することで実現できます。
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
public static class PackageManagerController
{
private static RemoveRequest _removeRequest;
[MenuItem("Test/Remove Unity IAP")]
public static void RemoveUnityIap()
{
RemovePackage("com.unity.purchasing");
}
public static void RemovePackage(string packageId)
{
_removeRequest = Client.Remove(packageId);
EditorApplication.update += CheckRemoveRequestProgress;
}
private static void CheckRemoveRequestProgress()
{
if (_removeRequest.IsCompleted)
{
EditorApplication.update -= CheckRemoveRequestProgress;
if (_removeRequest.Status == StatusCode.Success)
{
Debug.Log($"Package '{_removeRequest.PackageIdOrName}' removed successfully.");
}
else if (_removeRequest.Status == StatusCode.Failure)
{
Debug.LogError($"Failed to remove package '{_removeRequest.PackageIdOrName}'. Error: {_removeRequest.Error.message}");
}
}
}
}
ゴリ押しでmanifest.jsonから削除してしまう
この記事を見つけた人は正攻法を求めているのはわかっていますが、
CIでビルドを回しているなら、Unityプロジェクトを開く前にmanifest.jsonから該当行を削除してしまうのがオススメです。
- Unity Editorを開いてからパッケージ操作をすると、再度ビルドが走ってしまい時間の無駄。
- Unity Editorを開きっぱなしで操作すると、何かの拍子に依存が残っていると嫌。
var encoding = new UTF8Encoding(false);
var manifestFilePath = Path.Combine(UnityPath, "Packages", "manifest.json");
var manifest = File.ReadAllLines(manifestFilePath, encoding).ToList();
manifest.RemoveAll(x => x.Contains("com.unity.purchasing"));
File.WriteAllText(manifestFilePath, string.Join("\n", manifest) + "\n", encoding);