Created
February 27, 2021 20:00
-
-
Save kodai100/756213008d135de9a6b9d6f798e9e35a to your computer and use it in GitHub Desktop.
エディタ起動(コンパイル)時に自動でPackageManagerの指定パッケージをインストールさせるエディタスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[InitializeOnLoad] | |
public static class AutoImportRequiredPackages | |
{ | |
static readonly List<string> requiredPackageList = new List<string>() | |
{ | |
"[email protected]", | |
"[email protected]", | |
"[email protected]" | |
}; | |
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