Creating a Unity package's folder "Samples~" hides it

Advertisements

According to the documentation, one has to create a Samples~ folder, with a tilde at the end:

To add samples to your package:

Put the asset files or example C# code files under the Samples~ folder. You can have more than one sample in a package: each subfolder of the Samples~ folder contains one sample.

Note: The tilde character (~) tells Unity to ignore the contents the Samples~ folder. Such folders aren’t tracked with .meta files.

When you do that, the folder actually disappears in Unity!

Which means that you can no longer open the sample scene, edit it, etc, it’s gone…

Obviously, that makes no sense at all because the sample is part of the package.

Digging into the Package Manager one can see an Import button:

Clicking it will deploy the samples in Assets/Samples/..., that’s great news but that doesn’t really answer the question on how to keep authoring that package’s samples.

Still in the documentation, one has to fill package.json about samples contained in the package, did that but nothing appears.

Tried restarting Unity but that didn’t change anything.

Question:

How can one author a package’s sample if one cannot access it from within Unity?

>Solution :

This is the expected behavior.

In the package.json manifest in order to make it importable you additionally add a samples section like e.g.

samples : [
    {
       "displayName": "<name-to-appear-in-the-UI>",
       "description": "<brief-description>",
       "path": "Samples~/<sample-subfolder>"
    }
]

Now to the how to deal with this as the package developer:

There are different ways, but for me the best approach seems to use Symbolic Links.

Anyway in order to edit your package properly you need to embed it into a Unity project e.g. into

Packages/YourPackage

Again there are different ways for this

  • Some do this on he repository basis and commit the entire Unity project including the package.
  • Others use git submodules.
  • And yet others (like me) even do this as well via Symbolic Links.

Then in addition I keep the Samples~ folder so it is correctly committed into the repository.

And then in that development project I create a symbolic link as

Assets/Samples

which points to

../Packages/YourPackage/Samples~

I can recommend Link Shell Extension. With this installed you can simply drag and drop your Samples~ folder while holding right mouse button and then from the context menu select Drop as -> Symbolic Link. Then you can simply rename the symbolic link to Samples which will not affect the targeted Samples~

You will get a warning though

Disabling directory monitoring for current editor session. Found symlinks in project which cannot be used with directory monitoring. Directory monitoring settings can be changed in preferences

tbh I never had any issues so far with that approach and just ignored the warning. It anyway only happens in your local dev project. In this thread they suggested to rather go for hard links. I never tried this – might be worth a shot but I don’t know how hardlinks work in git, as long as it is a relative path symbolic links can simply be commited into the repository (basically just a file with a path)

Leave a ReplyCancel reply