zaro

Understanding Unity Packages

Published in Unity Package Development 5 mins read

To make a Unity package, you're typically developing a Custom Package (often referred to as a Unity Package Manager or UPM package), which is a modular collection of assets and code reusable across multiple projects. Alternatively, you might be looking to export an Asset Package (a .unitypackage file) for simple asset sharing. This guide primarily focuses on developing Custom Packages, as suggested by the provided reference, while also briefly covering Asset Packages.

Before diving into the creation process, it's essential to distinguish between the two main types of "Unity packages":

  • Custom Package (UPM Package): These are structured modules that follow a specific directory convention, managed by the Unity Package Manager. They are ideal for reusable code libraries, tools, or asset collections that need robust versioning, dependency management, and easy integration into different projects. The provided reference specifically outlines steps for creating this type of package.
  • Asset Package (.unitypackage): This is a simple archive file containing selected assets from a Unity project. It's a quick way to share specific assets (e.g., a 3D model, a script, a shader) with others or transfer them between projects without the overhead of a full UPM package.

Developing a Custom Unity Package (UPM)

Developing a custom Unity package involves a structured approach to ensure modularity, maintainability, and compatibility. The process generally follows these key steps:

Step 1: Creating the Package Shell

The first step is to create an empty shell for the package. This involves setting up the basic directory structure and the essential package.json manifest file.

  • Method 1: Manual Creation (Recommended for Local Packages)

    1. Navigate to your Unity project's root folder in your file explorer.
    2. Open the Packages folder. If it doesn't exist (e.g., in a new project), create it.
    3. Inside the Packages folder, create a new subfolder for your package. Use a unique reverse-domain name convention (e.g., com.yourcompany.mypackage).
    4. Inside your package folder, create a new text file named package.json. This file is the manifest for your package, containing essential metadata.
  • Method 2: Using Unity's Built-in Template (if available in newer Unity versions)
    In some newer Unity versions, you might find options like Assets > Create > Package or similar, which can generate a basic package structure.

Example package.json content:

{
  "name": "com.yourcompany.mypackage",
  "version": "1.0.0",
  "displayName": "My Custom Unity Package",
  "description": "A sample custom Unity package.",
  "unity": "2019.3",
  "unityRelease": "0b5",
  "keywords": [
    "utility",
    "tools"
  ],
  "author": {
    "name": "Your Name",
    "email": "[email protected]",
    "url": "http://www.yourwebsite.com"
  }
}

Step 2: Implementing Package Content

Next, you need to implement your tools, libraries, and any Assets your package requires. This is where you add all the functional components of your package.

  • Code: Write C# scripts for editor tools, runtime logic, custom inspectors, or utilities.
  • Assets: Include any Textures, Materials, Prefabs, Shaders, Audio clips, or other assets that your package depends on or provides.
  • Plugins: If your package requires external libraries (DLLs), place them in a Plugins folder.

Example:
For a simple utility package, you might add a folder named Runtime for scripts that run in play mode, and an Editor folder for scripts that provide editor-time functionality.

Step 3: Adhering to Package Layout Conventions

It's crucial to make sure the layout of your package follows the package layout convention for Unity packages. A standardized structure makes your package discoverable, maintainable, and compatible with Unity's Package Manager.

Here's a common package folder structure:

Folder Name Description
Editor/ Contains C# scripts and assets used only in the Unity Editor.
Runtime/ Contains C# scripts and assets used in both Editor and at runtime.
Tests/ Contains unit tests and integration tests for the package (see Step 4).
Documentation~/ Optional: Contains markdown files (.md) for package documentation.
Samples~/ Optional: Contains example scenes or usage demonstrations.
package.json Required: The package manifest file with metadata.
CHANGELOG.md Optional: Details changes between package versions.
LICENSE.md Optional: Specifies the licensing terms for your package.
README.md Optional: Provides a brief overview and instructions for the package.

(Note: Folders with a ~ suffix, like Documentation~, are hidden from the Unity editor's Project window but are still part of the package.)

Step 4: Adding Tests for Robustness

Finally, add tests to your package. Writing tests is a best practice for any reusable code, ensuring stability and preventing regressions as your package evolves.

  • Test Assemblies: Create a Tests folder within your package. Inside, you'll typically have subfolders like Editor and Runtime (or PlayMode) for test scripts.
  • Assembly Definitions (.asmdef): Create .asmdef files within these test folders to define test assemblies. Link them to the main package assemblies and add references to UnityEditor.TestRunner and UnityEngine.TestRunner.

Example Test Assembly Definition:

{
  "name": "YourPackage.Tests.Editor",
  "references": [
    "YourPackage.Runtime", // Reference your main package assembly
    "UnityEngine.TestRunner",
    "UnityEditor.TestRunner"
  ],
  "includePlatforms": [
    "Editor"
  ],
  "excludePlatforms": [],
  "allowUnsafeCode": false,
  "overrideReferences": false,
  "precompiledReferences": [],
  "autoReferenced": true,
  "defineConstraints": [
    "UNITY_INCLUDE_TESTS"
  ],
  "versionDefines": [],
  "noEngineReferences": false
}

Additional Considerations

Beyond these core steps, custom package development often involves:

  • Versioning: Incrementing the version field in package.json for new releases.
  • Documentation: Providing clear instructions and API references in your Documentation~ folder.
  • Distribution: Sharing your package as a local package, via Git URL, or by publishing to a custom registry.

What about Exporting an Asset Package (.unitypackage)?

If your goal is simply to export a collection of assets into a .unitypackage file, the process is much simpler:

  1. Select Assets: In your Unity project's Project window, select all the assets, folders, and files you want to include in your package.
  2. Export: Go to Assets > Export Package... in the Unity menu.
  3. Review and Export: A dialog box will appear, listing all selected assets. You can deselect any items you don't want to include. Ensure "Include dependencies" is checked if you want to automatically include assets that your selected items rely on.
  4. Save: Click Export... and choose a location to save your .unitypackage file.

This .unitypackage file can then be imported into any other Unity project via Assets > Import Package > Custom Package....