How to Make a Unity Package (For the Unity Package Manager)

Let’s say you have a bunch of code you want to drag around with you to every new Unity project, or maybe just the next project you’re doing for the same company. Don’t just copy and paste the same directory over and over again forcing you to also copy changes and updates. Create a package instead!

Unity has switched over to a package model for a lot of the engine’s capabilities. And it also allows you to import non-Asset Store packages straight from GitHub or even locally. So how do you take what you have and make it an easily distributable package? Follow these steps.

  1. In your Project window, below all your regular files is a Packages folder. Right click on it and select Show in Explorer.
  2. In that Packages folder, create a new directory and name it however you like. This will be the root of your package.
    1. Do remember that the folder name can not contain spaces.
  3. Create a new “package.json” file.
    1. This page right here explains everything about the structure of the file.
    2. And here is an example file.
  4. Back in Unity, your new package should be visible with the name you gave it.
  5. Clicking on package file will open it in the Editor UI, allowing you to edit it more conveniently so you don’t have to go back to the JSON.
  6. In that Inspector window, you can also easily add Dependencies if your package requires it which is a much easier workflow than typing them out.
  7. After this basic set up, you’ll want to add your content, this link here will show you the folder and file structure required of a package.
    1. You need the package.json file.
    2. Adding a “README.md”, “CHANGELOG.md”, and “LICENSE.md” files is general practice and provides a better user experience.
    3. The “Editor” folder should contain your editor scripts.
    4. The “Runtime” folder should contain your runtime scripts.
    5. And you can add additional “Tests” and “Documentation~” under their respective folders.
      1. Under “Tests” you do have to add “Editor” and/or “Runtime” sub-folders depending on what you’re testing.
    6. You may also add additional folders and files as you wish.
  8. Just drop your scripts, assets, and whatever else you want from your current project into the correct folder in the package (Regular scripts into “Runtime”, editor scripts into “Editor”, and test scripts into “Tests”).
  9. Then, for every folder you have files in, right click it and select “Create\Assembly Definition”.
    1. It is recommended to name them in reverse web notation in a way that is easy to understand.
    2. For tests, you need to add a reference to your relevant assembly definition file (The runtime file for runtime tests and editor file for editor tests) and to the Unity TestRunner assemblies (Yes, both).
      1. Also, make sure that Editor is the only platform selected.
  10. That’s it, your done defining.
  11. If you move the package anywhere else for safe keeping, you can add it from the Package Manager by pressing the ‘+’ sign, selecting “Add from Disk” and finding your package.json file.
  12. But it is recommended that you upload the contents of your package (Not the folder itself but everything in it) to some Git repository and then you can always add it from Git and your users will be much happier for a constantly updated package.

Posted in Practice, Programming, Thinking Out Loud by with comments disabled.