在Editor中异步获取依赖版本

        public static void GetPackageVersionAsync(string packageName, Action<string> callback)
        {
            ListRequest Request = Client.List(); // List packages installed for the project
            EditorApplication.CallbackFunction progress = null;
            progress = delegate()
            {
                if (!Request.IsCompleted)
                {
                    return;
                }

                EditorApplication.update -= progress;

                if (Request.Status == StatusCode.Success)
                {
                    foreach (var package in Request.Result)
                    {
                        if (package.name == packageName)
                        {
                            callback(package.version);
                            return;
                        }
                    }
                }
                else if (Request.Status >= StatusCode.Failure)
                {
                    Debug.Log(Request.Error.message);
                }
            };
            EditorApplication.update += progress;
        }

同步获取依赖版本

        public static string GetPackageVersion(string packageName)
        {
            ListRequest Request = Client.List(); // List packages installed for the project

            while (!Request.IsCompleted)
            {
                Thread.Sleep(100);
            }

            if (Request.Status == StatusCode.Success)
            {
                foreach (var package in Request.Result)
                {
                    if (package.name == packageName)
                    {
                        return package.version;
                    }
                }
            }
            else if (Request.Status >= StatusCode.Failure)
            {
                Debug.Log(Request.Error.message);
            }

            return "";
        }

UPM

Unity Package Management,是Unity官方推出的一种包管理模式。

Unity导入外部资源有两种方式,一种是通过.unitypackage名称的文件把资源导入到Assets目录,一种是把资源放在Pacakges目录。

Pacakges方式支持:内嵌、git仓库、网址三种形式。

UPM就是Packages目录方式,它可以指定依赖的名称、版本等信息。

Unity支持在UnityEditor中配置Registry,从而可以从私服上下载资源。

使用内嵌的最大好处就是:开发者可以修改Packages里面的内容。

使用git仓库的好处是:可以省掉与registry打交道的过程,同时享受网址模式的好处。

使用网址的好处:正规,当遇到升级的时候,可以显示可用的升级。

OpenUPM

OpenUPM是一个非官方的UPM工具,包括一个nodejs命令行和一个网站。

https://openupm.com/

国服:https://openupm.cn/

竞品:

使用方式与普通的Unity使用方式类似,可以手动添加registry,也可以使用openupm的命令行工具。

创建包的方式

openupm中管理了多少个包?这一切信息都在一个github仓库中:https://github.com/openupm/openupm

创建github仓库其实就是创建一个yaml文件,描述清楚包的仓库地址、license等信息。

这个yaml文件的创建可以使用一个网页工具创建,也可以直接编辑一个yaml文件提交上去。在这个仓库的PullRequest里面可以找到很多例子:https://github.com/openupm/openupm/pull/4015

使用国内版openupm访问github会报错,所以创建包的时候需要使用国际版:https://openupm.com/packages/add/

openupm严重依赖github,需要发布的包必须放在github上,并且使用tag管理版本。

https://github.com/openupm/openupm/compare/master...weiyinfu:openupm:patch-1

data/packages/weiyinfu.pico.integrationsdk.yml

使用包

https://openupm.cn/packages/weiyinfu.pico.integrationsdk/#close

方法一:直接改manifest

编辑Packages/manifest.json,添加scopeRegistries和dependency,然后保存

{    "scopedRegistries": [        {            "name": "package.openupm.cn",            "url": "https://package.openupm.cn",            "scopes": [                "weiyinfu.pico.integrationsdk"            ]        }    ],    "dependencies": {        "weiyinfu.pico.integrationsdk": "2.1.6"    }}

方法二:使用openupm命令

openupm-cn add weiyinfu.pico.integrationsdk