Samples in Packages - Manual Setup

In Package Manager UI 2.0 (compatible with Unity 2019.1), you can include samples in a package.

A sample could be a piece of example code, or some beginner assets that can be optionally brought into the user’s project under the Assets folder.

In the future, the editor will expose packaging functionality to properly expose samples, but in the meantime, here’s is an example on how samples can be manually configured to be recognized in the Package Manager UI.

First, make sure to place your samples under the Samples~ folder.

<Root>
├── package.json
├── Samples~
│ ├── Sample Folder 1
│ │ └── …
│ └── Sample Folder 2
│ └── …
└── ...

Then you need to specify them in the package.json file. Note that each sample needs to point to a directory.

{
"samples": [
{
"displayName": "Sample Name 1",
"description": "Description for sample 1",
"path": "Samples~/Sample Folder 1"
},
{
"displayName": "Sample Name 2",
"description": "Description for sample 2",
"path": "Samples~/Sample Folder 2"
}
]
}

Then you’ll be able to see the import buttons in Package Manager UI as shown below.

When the user chooses to import a sample, samples will be copied to the project’s Assets/Samples/Package Name/Version/Sample Name directory, thus allowing it to be modified.

18 Likes

Thank you Ethan. As soon as I can move to Unity 2019.1 will jump into this.

It took me a long time to figure out why this wasn’t working for me. As it turns out, you need to spell samples with a lower-case s, so not upper-case like in your example :wink:

  "samples": [
2 Likes

Oops sorry about that. :frowning:

@col000r Thanks for pointing it out! I just updated the post

is this the only way assets like prefabs can be brought into projects from a package? or just the recommended way? what other options are there?

Hi @JakHussain

(sorry for the delay)

I don’t understand your question. Can you elaborate more on your use cases?

Regards,

Pascal

Sorry I should have elaborated more. I was hoping to design packages that would extend the create menu for the scene/hierarchy as cleanly as possible. For example, Unity offers menu options for creating UI objects that automatically add a canvas if there isn’t one already, and the same for an event system. But the prefabs of those exact objects aren’t in the assets folder because they shouldn’t be. They’re almost like “read only” prefabs that are just there to provide tools for developers.

With my own package, I’d like to refer to prefabed objects that would be instantiated via editor scripts without needing to import those originals into the assets folder. Basically to have the same set up and Unity’s own basic game objects.

Is this something that can be done or will be considered for the package manager? I know I can just create plain game objects and add components to them but sometimes we need sprite references and other stuff that we don’t necessarily want in the assets folder but in the packages folder.

@JakHussain No problems. I will transfer your question to the prefab team. Sadly, I don’t know enough about the features/limitations of Prefab system to properly respond to your question.

Let me reach out to them!

@JakHussain I just reach out the Prefab team. They told me there is no issue with using Prefab inside a package.

Is there a way to flatten the sample folder path? Imagine I have a package with a template structure inside a ‘WebGLTemplates’ folder. My custom Template only appears in Player Settings if my WebGLTemplates folder is located in “Assets/WebGLTemplates/MyTemplate”, but the Package Manager imports assets as follows:
"Assets/Samples/Package Name/Version/WebGLTemplates/…
Is there a solution to solve that issue?

1 Like

@onMouseUp You should avoid expecting an asset to be located at a specific folder location. This is not a good approach. Instead, base your logic on asset GUID presence or through scripting.

Regards,

Pascal

That’s a Unity requirement.

https://docs.unity3d.com/Manual/webgl-templates.html

See “Structure of a WebGL Template” section.

1 Like

@bdominguezvw Ok! I was not aware of this. I’ll reach out the WebGL team.

Thanks,

Pascal

1 Like

@onMouseUp Would you mind filling up a bug for this issue? (Unity QA: Building quality with passion). This is something for Unity to figure out ways to let you bundle a WebGlTemplate in a package.

Thanks,

Pascal

1 Like

As commented in this thread

WebGLTemplates location is hard-coded.

I’ll open a bug report

Reported - 1157275
Thanks

1 Like

@De-Panther , Thank you very much!

Thanks for response! I created a bug report → https://fogbugz.unity3d.com/default.asp?1157287_a5u6jn4stvnt4b9h
But nevertheless I still struggling with the question: How can I put assets into special folders like Plugins or Editor? With *.unitypackage file export/import behaves exactly how it should. Would be great if you could export Assets the old fashion way but with a “new” package as a result. Or maybe extracting assets into an existing package.

1 Like

Oh, didn’t see that @De-Panther already opened a thread regarding that topic! :slight_smile:

Any chance of having extra dependencies related to Samples?

For example, I don’t want my actual package to need Unity.ugui as a dependency but the sample will need it?

1 Like