以编程方式检查资产更新

时间:2018-02-21 14:13:41

标签: unity3d

我无法找到任何方法以编程方式获取当前版本的资产(来自资源商店),以使其与当前安装的资产相匹配。

另外,我没有找到资产商店API(联盟计划https://unity3d.com/affiliates除外,但这似乎不是为此设计的。我错了吗?)

虽然我可以获取HTTP页面并进行解析,但那是猴子的补丁。

1 个答案:

答案 0 :(得分:1)

有一种方法可以以编程方式检查 Package Manager 中的 Unity 2021 中的过期资产。

示例

这是一个允许您检查过时资产包的类。导入脚本后,转到 Custom 菜单并单击 3 个新创建的菜单项之一。不久之后,如果有任何过时的资产,编辑器会通过控制台窗口通知您。

代码

using System;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;

public static class AssetVersionChecker
{
    private static ListRequest Request;
    private static Action actionToHandle;

    [MenuItem("Custom/Check Asset Packages For Updates (Latest)")]
    private static void CheckPackagesForLatestUpdates() => Handle(
        "Latest",
        () => PrintOutdatedPackages(GetOutdatedLatestPackages()));

    [MenuItem("Custom/Check Asset Packages For Updates (Latest Compatible)")]
    private static void CheckPackagesForLatestCompatibleUpdates() => Handle(
        "Latest Compatible",
        () => PrintOutdatedPackages(GetOutdatedLatestCompatiblePackages()));

    [MenuItem("Custom/Check Asset Packages For Updates (Verified)")]
    private static void CheckPackagesForVerifiedUpdates() => Handle(
        "Verified",
        () => PrintOutdatedPackages(GetOutdatedVerifiedPackages()));
    
    private static void Handle(string label, Action action)
    {
        Debug.Log($"Checking for updates ({label})...");

        // Listing packages installed in the project
        Request = Client.List();
        actionToHandle = action;

        EditorApplication.update += OnEditorApplicationUpdate;
    }

    private static void OnEditorApplicationUpdate()
    {
        if (Request.IsCompleted)
        {
            if (Request.Status == StatusCode.Success)
                actionToHandle();
            else if (Request.Status >= StatusCode.Failure)
                Debug.Log(Request.Error.message);

            EditorApplication.update -= OnEditorApplicationUpdate;
        }
    }

    private static void PrintOutdatedPackages(IEnumerable<PrintablePackageInfo> packages)
    {
        if (packages.Count() == 0)
        {
            Debug.Log("All packages are up to date!");
            return;
        }

        foreach (var package in packages)
        {
            Debug.Log(
                $"{package.Name} out of date! " +
                $"Current: {package.CurrentVersion}, " +
                $"Latest: {package.LatestVersion}");
        }
    }

    private static IEnumerable<PrintablePackageInfo> GetOutdatedLatestPackages() => Request
        .Result
        .Where(x => x.version != x.versions.latest)
        .Select(x => new PrintablePackageInfo(x.name, x.version, x.versions.latest));

    private static IEnumerable<PrintablePackageInfo> GetOutdatedLatestCompatiblePackages() => Request
        .Result
        .Where(x => x.version != x.versions.latestCompatible)
        .Select(x => new PrintablePackageInfo(x.name, x.version, x.versions.latestCompatible));

    private static IEnumerable<PrintablePackageInfo> GetOutdatedVerifiedPackages() => Request
        .Result
        .Where(x => x.versions.verified != string.Empty)
        .Where(x => x.version != x.versions.verified)
        .Select(x => new PrintablePackageInfo(x.name, x.version, x.versions.verified));

    struct PrintablePackageInfo
    {
        public readonly string Name;
        public readonly string CurrentVersion;
        public readonly string LatestVersion;

        public PrintablePackageInfo(string name, string currentVersion, string latestVersion)
        {
            Name = name;
            CurrentVersion = currentVersion;
            LatestVersion = latestVersion;
        }
    }
}

输出

enter image description here

参考文献

这是帮助我编写此解决方案的 Unity 文档页面:
https://docs.unity3d.com/Manual/upm-api.html