본문 바로가기
개발/Unity

Unity) Unity Package 모두 업데이트(Package All Update)

by 테샤르 2023. 7. 16.

Unity Package 모두 업데이트(Package All Update)

 

Unity Editor Coroutine을 사용해서 

포함된 패키지를 모두 다 업데이트하는 코드이다.

 

반응형
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
using Unity.EditorCoroutines.Editor;

static class PackagesUtill
{
    [MenuItem("Packages/Update All Packages")]
    private static void UpdateAllPackages()
    {
        EditorCoroutineUtility.StartCoroutineOwnerless(UpdatePackages());
    }

    [MenuItem("Packages/Show Package Name")]
    private static void ShowPackageName()
    {
        EditorCoroutineUtility.StartCoroutineOwnerless(ShowRoutine());
    }

    private static IEnumerator ShowRoutine()
    {
        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;
            }

            Debug.Log($"[CheckPackages] {package.name}");
        }
    }

    private static IEnumerator UpdatePackages()
    {
        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;
            }

            Debug.Log($"[CheckPackages] {package.name}");
            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>());
    }
}

 

 

사진

 

★☆

 

반응형

댓글