Skip to content

Instantly share code, notes, and snippets.

@kodai100
Created February 27, 2021 20:00
Show Gist options
  • Save kodai100/756213008d135de9a6b9d6f798e9e35a to your computer and use it in GitHub Desktop.
Save kodai100/756213008d135de9a6b9d6f798e9e35a to your computer and use it in GitHub Desktop.
エディタ起動(コンパイル)時に自動でPackageManagerの指定パッケージをインストールさせるエディタスクリプト
[InitializeOnLoad]
public static class AutoImportRequiredPackages
{
static readonly List<string> requiredPackageList = new List<string>()
{
"com.unity.visualeffectgraph@10.2.2",
"com.unity.render-pipelines.universal@10.2.2",
"com.unity.inputsystem@1.0.2"
};
static AutoImportRequiredPackages()
{
Process();
}
private static async void Process()
{
foreach (var requiredPackage in requiredPackageList)
{
try
{
var installed = await CheckInstalled(requiredPackage);
if (!installed)
{
var result = await Install(requiredPackage);
}
}
catch (Exception e)
{
Debug.LogError("Installation failed: " + requiredPackage + "\n====\n" + e.Message);
}
}
}
private static async Task<bool> CheckInstalled(string packageName)
{
var listRequest = Client.List();
while (listRequest.Status == StatusCode.InProgress)
{
await Task.Delay(100);
}
if (listRequest.Status == StatusCode.Success)
{
var find = listRequest
.Result
.ToList()
.Any(packageCollection => packageCollection.packageId.StartsWith(packageName));
if(!find) Debug.Log("<color=yellow>Not installed: " + packageName + "</color>");
return find;
}
throw new Exception(listRequest.Error.message);
}
private static async Task<bool> Install(string packageName)
{
var addRequest = Client.Add(packageName);
while (addRequest.Status == StatusCode.InProgress)
{
await Task.Delay(100);
}
if (addRequest.Status == StatusCode.Success)
{
Debug.Log("<color=green>Install completed: " + addRequest.Result.packageId + "</color>");
return true;
}
throw new Exception(addRequest.Error.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment