How to create a Custom Package in Unity
2 min readMay 6, 2022
Introduction
Unity give us the chance to create own custom package for the Package manager. This allow us to create modules to share between projects.
If we press the plus button on the package manager we are going to see four ways to import a custom package into our project:
- From Disk: in this option you have to select the folder of the package in your computer.
- From Tarball: https://docs.unity3d.com/Manual/upm-ui-tarball.html.
- From Git URL: in this option you have to paste the git url of the package, this is the best option and we are working with this.
- By Name: in this option you have to put the package id like
com.unity.example
, note that package has to be in unity registry.
Package Layout
<root>
├── package.json
├── README.md
├── LICENSE.md
├── Editor
│ ├── [company-name].[package-name].Editor.asmdef
│ └── EditorExample.cs
├── Runtime
│ ├── [company-name].[package-name].asmdef
│ └── RuntimeExample.cs
├── Tests
│ ├── Editor
│ │ ├── [company-name].[package-name].Editor.Tests.asmdef
│ │ └── EditorExampleTest.cs
│ └── Runtime
│ ├── [company-name].[package-name].Tests.asmdef
│ └── RuntimeExampleTest.cs
├── Samples~
├── SampleFolder1
├── SampleFolder2
└── ...
Create Git Repository
- Follow this repository for add it to templates:
https://github.com/LaloBerro/UnityPackageTemplate
- Create a New Repository
- Select the UnityPackageTemplate as template of the repository
- Put a name like UnityPackage-NameExample
- Create Repository.
- Add the package to unity from Git Url.
Add the content to the package
- Runtime Folder: in this folder add all the scripts that don’t have an editor reference.
- Editor Folder: in this folder add all the scripts that have an editor reference.
- If you package have test add it on the Test folder.
Create a Samples folder
- Add this line to package.json
"samples": [ { "displayName": "<displayname>", "description": "", "path": "Samples~/<foldername>" }
- Add the folder
Samples~
to the root of the package. - When the Samples folder in assets is ready to the package:
- Export the folder as a unity package.
- Save the package on the assets root folder.
- Put it on a folder named equal that the in package.json
<foldername>
. - Put this entire folder with his .meta on
/Samples~
. - On .gitIgnore delete the ignore of .unitypackages.
- If you want to update the samples, only you have to replace the .unitypackage in the folder
/Samples~/<foldername>/
.