创建自定义组件并结合package manager管理

使用背景

想要实现一个可以组件分离的、可以迭代的、使用环境无法修改的(保证纯净)组件管理。

技术方向

采用unity package manager + git来组织并实现。

技术方案

整体结构

  1. 制作组件的仓库A,此仓库也包含测试组件的职责
  2. 承载组件的仓库B,未来使用组件仓库会调用B
  3. 使用组件的项目C

制作仓库A流程

此仓库用于制作、测试组件,其项目结构如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<root>
├── Editor
│ ├── Unity.[YourPackageName].Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── Unity.[YourPackageName].asmdef
│ └── RuntimeExample.cs
├── Tests
│ ├── Editor
│ │ ├── Unity.[YourPackageName].Editor.Tests.asmdef
│ │ └── EditorExampleTest.cs
│ └── Runtime
│ ├── Unity.[YourPackageName].Tests.asmdef
│ └── RuntimeExampleTest.cs

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Match3Game
├── Editor
│ ├── Unity.com.xxx.match3.Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── Unity.com.xxx.match3.asmdef
│ └── RuntimeExample.cs
├── Tests
│ ├── Editor
│ │ ├── Unity.com.xxx.match3.Editor.Tests.asmdef
│ │ └── EditorExampleTest.cs
│ └── Runtime
│ ├── Unity.com.xxx.match3.Tests.asmdef
│ └── RuntimeExampleTest.cs

其中Rumtime目录为运行时代码部分,Unity.[YourPackageName].asmdef为程序集。

其他资源可以自行组织目录结构。

仓库B制作

创建自定义仓库分为两种:嵌入式,外部package。本文只讨论外部package形式。

创建外部package可以跟随一下流程:

  1. 创建一个文件夹
  2. 创建package.json
  3. 将仓库A所做内容复制到仓库B中

目录结构如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<root>
├── package.json
├── README.md
├── CHANGELOG.md
├── LICENSE.md
├── Editor
│ ├── Unity.[YourPackageName].Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── Unity.[YourPackageName].asmdef
│ └── RuntimeExample.cs
├── Tests
│ ├── Editor
│ │ ├── Unity.[YourPackageName].Editor.Tests.asmdef
│ │ └── EditorExampleTest.cs
│ └── Runtime
│ ├── Unity.[YourPackageName].Tests.asmdef
│ └── RuntimeExampleTest.cs
└── Documentation~
└── [YourPackageName].md

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Match3Game
├── package.json
├── README.md
├── CHANGELOG.md
├── LICENSE.md
├── Editor
│ ├── Unity.com.xxx.match3.Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── Unity.com.xxx.match3.asmdef
│ └── RuntimeExample.cs
├── Tests
│ ├── Editor
│ │ ├── Unity.com.xxx.match3.Editor.Tests.asmdef
│ │ └── EditorExampleTest.cs
│ └── Runtime
│ ├── Unity.com.xxx.match3.Tests.asmdef
│ └── RuntimeExampleTest.cs
└── Documentation~
└── com.xxx.match3.md

下面详细介绍文件的

package.json:package的信息文件内容,以json形式存储

关键词 类型 描述
name String 唯一名,建议使用反网址格式,例如com.company.xxx(必填
version String 建议使用MAJOR.MINOR.PATCH格式,例如3.2.1(必填
displayName String 显示名字,使用这个package的项目,会在目录结构中显示这个名字(推荐填写)
description String 对package的描述(推荐填写)
unity String 最低Unity支持版本(推荐填写)
unityRelease String 标记unity特殊版本(详情查看官方文档)
dependencies Object 一个package依赖表。key是package name,value是版本。表示此package需要依赖的其他packages。
keywords Array of String 关键词数组,用于在unity package manger中匹配搜索。
author Object package作者信息

package.json实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"name": "com.unity.example",
"version": "1.2.3",
"displayName": "Package Example",
"description": "This is an example package",
"unity": "2019.1",
"unityRelease": "0b5",
"dependencies": {
"com.unity.some-package": "1.0.0",
"com.unity.other-package": "2.0.0"
},
"keywords": [
"keyword1",
"keyword2",
"keyword3"
],
"author": {
"name": "Unity",
"email": "unity@example.com",
"url": "https://www.unity3d.com"
}
}

README.md:package介绍文档

CHANGELOG.md:修改日志

LICENCED.md:版权

Documentation:文档目录

项目C使用

  1. 将仓库B上传到git后,获得git地址,这里推荐使用ssh公钥方式访问。

  2. 新建unity项目(或者使用已存在项目),编辑Package下的manifest.json文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    {
    "dependencies": {
    "com.readpan.firstpackage": "ssh://yourepos#v1.0.0",
    "com.unity.collab-proxy": "1.2.16",
    "com.unity.ext.nunit": "1.0.0",
    "com.unity.ide.rider": "1.1.0",
    "com.unity.ide.vscode": "1.1.2",
    "com.unity.package-manager-ui": "2.2.0",
    "com.unity.test-framework": "1.0.13",
    "com.unity.textmeshpro": "2.0.1",
    "com.unity.timeline": "1.1.0",
    "com.unity.ugui": "1.0.0",
    "com.unity.modules.ai": "1.0.0",
    "com.unity.modules.androidjni": "1.0.0",
    "com.unity.modules.animation": "1.0.0",
    "com.unity.modules.assetbundle": "1.0.0",
    "com.unity.modules.audio": "1.0.0",
    "com.unity.modules.cloth": "1.0.0",
    "com.unity.modules.director": "1.0.0",
    "com.unity.modules.imageconversion": "1.0.0",
    "com.unity.modules.imgui": "1.0.0",
    "com.unity.modules.jsonserialize": "1.0.0",
    "com.unity.modules.particlesystem": "1.0.0",
    "com.unity.modules.physics": "1.0.0",
    "com.unity.modules.physics2d": "1.0.0",
    "com.unity.modules.screencapture": "1.0.0",
    "com.unity.modules.terrain": "1.0.0",
    "com.unity.modules.terrainphysics": "1.0.0",
    "com.unity.modules.tilemap": "1.0.0",
    "com.unity.modules.ui": "1.0.0",
    "com.unity.modules.uielements": "1.0.0",
    "com.unity.modules.umbra": "1.0.0",
    "com.unity.modules.unityanalytics": "1.0.0",
    "com.unity.modules.unitywebrequest": "1.0.0",
    "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    "com.unity.modules.unitywebrequestaudio": "1.0.0",
    "com.unity.modules.unitywebrequesttexture": "1.0.0",
    "com.unity.modules.unitywebrequestwww": "1.0.0",
    "com.unity.modules.vehicles": "1.0.0",
    "com.unity.modules.video": "1.0.0",
    "com.unity.modules.vr": "1.0.0",
    "com.unity.modules.wind": "1.0.0",
    "com.unity.modules.xr": "1.0.0"
    }
    }
  3. 注意manifest文件中,"com.readpan.firstpackage": "ssh://yourepos#v1.0.0"

    1. com.readpan.firstpackage为仓库B中配置文件(package.json)中的name
  4. ssh://yourepos:仓库地址

    1. #v1.0.0:指定仓库拉取特定版本,如不指定,则默认拉取HEAD(v1.0.0为git中Tag创建的名字)
  5. 打开项目C就会看到相应仓库