Create samples for your package
Include optional samples that help users learn the package and import example content.
Read time 6 minutesLast updated 12 days ago
You can create samples to include in a Unity Package Manager (UPM) package you develop.
Although optional, including samples helps users learn how to use your package. A sample might be a piece of example code, some shaders and textures, some animation, or any other files that you typically find under the project's folder.
AssetsWhen you open the Package Manager window and select a package containing samples, an Import button appears in the package's details panel for each sample in the package. When you select Import, the Package Manager copies the whole subfolder structure for that sample under the project's folder.
AssetsTo add samples to your package:
-
Put the asset files or C# code files under the Samples folder. You can have more than one sample in a package. Each subfolder of thefolder has one sample.
Samples -
Open the package manifest file () for editing. To locate the file, refer to Locate the manifest file.
package.json -
If the file doesn't contain a JSON array called, add it.
samples -
For each sample you want to include, add a JSON object in thearray.
samples -
Include the following keys and values for each JSON object in thearray:
samplesKey
Description
displayNameThe name of the sample as it appears in the package details panel of the Package Manager window. description
(optional)A brief description of what the sample demonstrates or contains. This description appears in the Samples tab of the Package Manager window's details panel. pathThe path to the sample’s folder, starting with .Samplesimages
(optional)An array of strings that specifies image file paths, relative to the package root, starting with the sample folder path specified in . The images appear in the details panel when viewing a sample from the All Samples view.path -
Make sure thatcontains valid JSON. You can check its validity using an online JSON validator or directly in your code editor, if it supports JSON syntax checking.
package.json -
Save the file.
Images support
If you include the array, consider the following when adding images:
images- The recommended aspect ratio is 16:9, such as 1920 × 1080.
- Supported formats are BMP, EXR, HDR, IFF, JPG/JPEG, PICT, PNG, PSD, SVG, TGA, TIFF.
- The PNG format is recommended for images with an alpha channel or transparency.
Location of sample files
You can add your sample assets under subfolders of the folder of your package. For example, a package with shader samples might look something like this:
SamplesMyPackage ├── package.json └── Samples ├── SamplesHDRP │ ├── Images │ | ├── CarouselImage1.png │ | └── CarouselImage2.png │ ├── Shader │ | ├── Lit Texture Blend HDRP.ShaderGraph │ | └── Lit Vertex Color HDRP.ShaderGraph │ └── Textures │ ├── MossyRock.bmp │ └── SandyRock.bmp └── SamplesStandard │ ├── Images │ | ├── CarouselImage1.png │ | └── CarouselImage2.png │ ├── Shader │ | ├── StandardTextureBlend.shader │ | └── StandardVertexColor.shader │ └── Textures │ ├── MossyRock.bmp │ └── SandyRock.bmp └── SamplesUniversalRP ├── Images | ├── CarouselImage1.png | └── CarouselImage2.png ├── Shader | ├── Lit Texture Blend URP.ShaderGraph | └── Lit Vertex Color URP.ShaderGraph └── Textures ├── MossyRock.bmp └── SandyRock.bmp
Example of a samples array in the manifest file
Using the same structure as the example for Location of sample files, the array in looks similar to this:
samplespackage.json{ "samples": [ { "displayName": "HDRP Shaders", "description": "Contains sample shaders for the High Definition render pipeline", "path": "Samples/SamplesHDRP", "images": ["Samples/SamplesHDRP/Images/CarouselImage1.png", "Samples/SamplesHDRP/Images/CarouselImage2.png"] }, { "displayName": "Standard RP Shaders", "description": "Contains sample shaders for the Standard render pipeline", "path": "Samples/SamplesStandard", "images": ["Samples/SamplesStandard/Images/CarouselImage1.png", "Samples/SamplesStandard/Images/CarouselImage2.png"] }, { "displayName": "URP Shaders", "description": "Contains sample shaders for the Universal render pipeline", "path": "Samples/SamplesUniversalRP", "images": ["Samples/SamplesUniversalRP/Images/CarouselImage1.png", "Samples/SamplesUniversalRP/Images/CarouselImage2.png"] } ]}
Samples folder naming convention
When creating your package with the Create package function, the creation process performs several operations. These operations include:
- Creating a folder structure in your package folder, including the creation of a subfolder.
Samples - Generating a sample configuration file at . For more information, refer to Define sample metadata with .sample.json.
Samples/Example/.sample.json
If you choose to delete all files and control samples metadata with the package manifest file (), keep the value (in the array in as . Don't append a trailing tilde ().
.sample.jsonpackage.jsonpathsamplespackage.jsonSamples~Any time you run a Unity process that packs your package (such as the export process), Unity renames the folder to . Appending the tilde hides the sample in the folder of the Project window when others install your package. During the packing process, Unity also adjusts to in the package manifest file that gets cached in the target project. You don't need to adjust the path values for samples in your local package manifest file before you export or pack your package. The Package Manager will resolve the sample path correctly. After developers import your sample, they can find it in the folder of the Project window.
SamplesSamples~PackagesSamplesSamples~AssetsDefine sample metadata with .sample.json
While developing your package, you might find it inconvenient to edit the samples' value in your package manifest file from to to test it as an installed package. Instead of renaming while testing, another solution is to define sample-specific metadata in a file inside the individual sample subfolder (or subfolders) within the folder for your package. When you pack your package, the packing tool checks for files. If the tool finds any files, it ignores the entire entry in the package manifest () and replaces it with the metadata collected from the files.
pathSamplesSamples~Samples.sample.jsonSamples.sample.json.sample.jsonsamplespackage.json.sample.jsonThe properties you can define in are:
.sample.jsondisplayNamedescription- (array)
images
Best practices when using .sample.json
Don't set a property in . The operations determine the sample path based on the location of the files.
path.sample.jsonpack.sample.jsonIf you have multiple sample folders directly under the folder, make sure you create one file for each subfolder directly under . Store each samples' metadata in those files. Otherwise, some samples might be missing in the installed package or have missing or incorrect metadata.
Samples.sample.jsonSamplesIf you specify the array in , the values must start with , even if the directory in your development directory doesn't end with the tilde ().
images.sample.json"Samples~/"Samples~Don't mix metadata sources. The recommended best practice is to specify samples metadata in either the file or in the file (or files). The presence of even one file anywhere under causes the packing tool to completely ignore the array in . Every sample must therefore have its own file. Otherwise, those samples will not appear when others install your package. Use either files (one per sample) or the array in :
package.json.sample.json.sample.jsonSamplessamplespackage.json.sample.json.sample.jsonsamplespackage.json- If you plan to use , create one file per sample, store all samples metadata in those files, and remove any overlapping metadata from
.sample.json. If you include thepackage.jsonarray, make sure all values start withimages. Be sure to include the trailing tilde."Samples~/" - If you plan to use for all samples metadata, delete all
package.jsonfiles from the.sample.jsondirectory tree.Samples