Last active
October 14, 2023 19:57
-
-
Save IJEMIN/21cbd8f9e1086727df08e53562d16484 to your computer and use it in GitHub Desktop.
Batch update all installed unity packages in the project
This file contains 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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEditor.PackageManager.Requests; | |
using UnityEditor.PackageManager; | |
using UnityEngine; | |
using Unity.EditorCoroutines.Editor; | |
namespace Unity.Editor.Example | |
{ | |
static class UnityPackageBatchUpdate | |
{ | |
[MenuItem("Utils/Update All Packages")] | |
static void UpdateAllPackages() | |
{ | |
EditorCoroutineUtility.StartCoroutineOwnerless(UpdateRoutine()); | |
} | |
private static IEnumerator UpdateRoutine() | |
{ | |
var listToUpdate = new List<string>(); | |
var listRequest = Client.List(); | |
while(!listRequest.IsCompleted) | |
{ | |
yield return new WaitForSeconds(0.1f); | |
} | |
var packageCollection = listRequest.Result; | |
foreach(var package in packageCollection) | |
{ | |
if(!package.isDirectDependency) | |
{ | |
continue; | |
} | |
var targetVersion = string.Empty; | |
for(var i = package.versions.compatible.Length - 1; i >= 0; i--) | |
{ | |
var version = package.versions.compatible[i]; | |
if(version.Contains("pre") || version.Contains("exp")) | |
{ | |
continue; | |
} | |
targetVersion = version; | |
break; | |
} | |
listToUpdate.Add($"{package.name}@{targetVersion}"); | |
} | |
Client.AddAndRemove(listToUpdate.ToArray(), Array.Empty<string>()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment