Skip to content

Read UnityPackage contents

Last updated

You can read the path, GUID, size, and SHA-256 hash of each file in a UnityPackage without importing it. You can also replace the reader, for example with one that uses a cache.

Call AmariUnityPackageContentReaders.TryRead. On success, it returns true and entries contains one item per file, in ascending GUID order. On failure, it returns false and errorMessage contains the reason (in English).

using com.amari_noa.unitypackage_pipeline_core.editor;
if (AmariUnityPackageContentReaders.TryRead(
"C:/Downloads/Example.unitypackage",
out var entries,
out var errorMessage))
{
foreach (var entry in entries)
{
UnityEngine.Debug.Log($"{entry.Pathname} {entry.AssetSha256}");
}
}

Pass a CancellationToken as the last argument to stop reading partway through.

Each item in entries (AmariUnityPackageContentEntry) has the following properties.

Property Description
Guid The GUID of the entry in the UnityPackage (lowercase)
Pathname The destination path (such as Assets/...)
HasAsset Whether the entry contains the file itself
AssetSize The size of the file in bytes
AssetSha256 The SHA-256 hash of the file (lowercase hexadecimal)
HasMeta Whether the entry contains a .meta file
MetaSha256 The SHA-256 hash of the .meta file (lowercase hexadecimal)
MetaGuid The GUID written in the .meta file

Implement IAmariUnityPackageContentReadProvider and register it with AmariUnityPackageContentReaders.RegisterProvider. After that, your reader is used both by AmariUnityPackageContentReaders.TryRead and by the pre-import change check.

using System.Collections.Generic;
using System.Threading;
using com.amari_noa.unitypackage_pipeline_core.editor;
internal sealed class CachedContentReadProvider : IAmariUnityPackageContentReadProvider
{
public bool TryRead(
string packagePath,
out IReadOnlyList<AmariUnityPackageContentEntry> entries,
out string errorMessage,
CancellationToken cancellationToken = default)
{
// Fall back to the standard reader when there is no cached result
return AmariUnityPackageContentReader.TryRead(packagePath, out entries, out errorMessage, cancellationToken);
}
}
  • Only one reader can be registered. A reader registered later replaces the previous one.
  • UnregisterProvider returns to the standard reader (AmariUnityPackageContentReader.TryRead). It only unregisters the reader if you pass the same instance that is currently registered.