Unity’s Package Manager is so useful for distributing plugins/ code/ toolkits that you use a lot, and it’s possible to have your own packages hosted on GitHub.
I had to set this up for work recently — I found two threads [1, 2] on Unity’s forum, that helped, but were missing information that took me a while to work out. Below I’ll show what I did to get setup with npm, publishing packages, and how to import them into Unity.
To publish new packages and update them, npm is required. npm comes with Nodejs, download it from https://nodejs.org/en/ and select to install the additonal tools during installation. Once it has finished installing, reboot your machine.
To test npm has installed successfully open a command prompt and type:
npm version
If installed and working correctly, you should see something like this:
So that npm can authenticate with GitHub and publish packages, we need to create a Personal Access Token, click this link https://github.com/settings/tokens
(this is available from profile picture → Settings → Developer settings → Personal access tokens)
Click the ‘Generate new token’ button.
In the ‘Note’ textfield, type a relevant name for the token — this is best to be something that easily identifies the device using this token.
In the ‘Select scopes’ section below, just tick ‘write:packages’, everything else required will automatically be selected too.
Then scroll down and click ‘Generate token’.
The page will refresh and display your PAT, at this point, keep this page open as you cannot see this token again once the page is closed.
There’s some information you’ll need at this point, I set this up for my work so I’m using the GitHub organisation in this example, however I believe your username should also work — from here on, @MyOrg is used to refer to the GitHub organisation — replace anywhere it says ‘myorg’ with your GitHub organisation or username.
By default, all npm commands will point at the npm hosted registry url, we want to point to the GitHub package registry url.
Go to:
Windows: C:\Users\<yourusername>\
Mac: /Users/<your username>/
and look for a .npmrc file, if one doesn’t already exist create it, and then open the file in a text editor.
Paste in the following two lines:
registry=https://npm.pkg.github.com/@MyOrg
//npm.pkg.github.com/:_authToken=
Go back to your browser and copy the PAT created in the step above, paste it after authToken= in the .npmrc file. It should now look like this:
registry=https://npm.pkg.github.com/@MyOrg
//npm.pkg.github.com/:_authToken=0cd446905f2a9dbea98b6bf8b41535eabe93befe
(that auth token has been deleted and won’t work, don’t try and use it)
You can now save and close this file. It’s also now ok to close the browser window where you created the PAT.
A repository is required on GitHub to host your pacakges, create a new, empty, repository — you can have multiple packages in one repository.
This file is what everything uses for the data about a package, it’s name, version, and a load of other important information.
The first section to add is general information about the package.
{
"name": "com.myorg.<packagename>",
"displayName": "<packagename>",
"version": "1.0.0",
"unity": "2019.4",
"description": "<description>",
"keywords": [
"key X",
"key Y",
"key Z"
],
"category": "MyOrg",
"author": {
"name": "My Org",
"email": "developers@myorg.co.uk",
"url": "https://myorg.co.uk/"
}
}
We need to an additional property to point to the correct registry for the package.
"publishConfig": { "registry": "https://npm.pkg.github.com/@MyOrg" }
The repository property tells npm where to find the package on the above registry, which git repository and a directory the package is in on that repository. For the URL, enter the git URL for the repository created above.
"repository": {
"type": "git",
"url": "ssh://git@github.com:MyOrg/MyRepository.git",
"directory": "com.myorg.<packagename>"
}
A complete example package.json
{
"name": "com.myorg.<packagename>",
"displayName": "<packagename>",
"version": "1.0.0",
"unity": "2019.4",
"description": "<description>",
"keywords": [
"key X",
"key Y",
"key Z"
],
"category": "MyOrg",
"author": {
"name": "My Org",
"email": "developers@myorg.co.uk",
"url": "https://myorg.co.uk/"
}
,
"repository": {
"type": "git",
"url": "ssh://git@github.com:MyOrg/MyRepository.git",
"directory": "com.myorg.<packagename>"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/@MyOrg"
}
}
Unity requires a bit of setup to be able to access packages on GitHub, fortunately this is only required once per machine. Everyone on your team, that uses the Unity project, will be required to do this step next step of creating the upm config, even the artists.
Go to:
Windows: C:\Users\<yourusername>\
Mac: /Users/<your username>/
and look for a .upmconfig.toml file, if one doesn’t already exist create it, and then open the file in a text editor.
Paste the following into this file:
[npmAuth."https://npm.pkg.github.com/@MyOrg"]
token = "<PAT created on Github>"
email = "<your email>"
alwaysAuth = true
The token is the same PAT created for the npm authentication, you can grab it from your .npmrc file. Use your work email address as your email. Now save and close.
In Unity open the ‘Project Settings’ and go to ‘Package Manager’, enter the following details into the available fields:
Name: MyOrg Toolkit
URL: https://npm.pkg.github.com/@MyOrg
Scope(s): com.myorg
Notice that the scope is the same as the package.json created above, you don’t want to add any additional bits to this text — it will see every package within the same scope at the git repo/registry we’re pointing it at.
Unfortunately, Unity won’t display any of these packages in the Package Manager, so they require manually adding to the project manifest.json located in <UnityProject>/Packages/
Open the manifest.json and add the packages with the other dependencies:
"dependencies": {
"com.myorg.mypackage01": "1.0.1",
"com.myorg.mypackage02": "1.0.0",
"com.myorg.mypackage03": "1.0.0",
"com.myorg.mypackage04": "1.0.0",
"com.unity.cinemachine": "2.3.4",
…
}
Save, and return to Unity, the packages will get imported and be visible in the Package Manager. They’ll also show that updates are available and can be updated via the Package Manager now.
If you scroll down to the bottom of the manifest.json, you’ll notice that the scoped registery we added in Unity appears here, and looks like this:
"scopedRegistries": [
{
"name": "MyOrg Toolkit",
"url": "https://npm.pkg.github.com/@MyOrg",
"scopes": [
"com.myorg"
]
}
]
Now when you open the Package Manager, you’ll notice some console errors appear. These can be ignored — (I believe) it’s a limitation of the Unity Package Manager not be able to search through custom scoped registries, and returns an error.