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.
- In your Project window, below all your regular files is a Packages folder. Right click on it and select Show in Explorer.
- In that Packages folder, create a new directory and name it however you like. This will be the root of your package.
- Do remember that the folder name can not contain spaces.
- Create a new “package.json” file.
- Back in Unity, your new package should be visible with the name you gave it.
- 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.
- 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.
- 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.
- You need the package.json file.
- Adding a “README.md”, “CHANGELOG.md”, and “LICENSE.md” files is general practice and provides a better user experience.
- The “Editor” folder should contain your editor scripts.
- The “Runtime” folder should contain your runtime scripts.
- And you can add additional “Tests” and “Documentation~” under their respective folders.
- Under “Tests” you do have to add “Editor” and/or “Runtime” sub-folders depending on what you’re testing.
- You may also add additional folders and files as you wish.
- 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”).
- Then, for every folder you have files in, right click it and select “Create\Assembly Definition”.
- It is recommended to name them in reverse web notation in a way that is easy to understand.
- 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).
- Also, make sure that Editor is the only platform selected.
- That’s it, your done defining.
- 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.
- 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 Eran with .