Skip to content

Commit

Permalink
fix: check folder(s) should treat folder as import entry as well (close
Browse files Browse the repository at this point in the history
…: #26).
  • Loading branch information
favoyang committed May 17, 2020
1 parent 84cded7 commit 18923eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
31 changes: 24 additions & 7 deletions Documentation~/AddressableImporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Table of Contents
- [Group Replacement](#group-replacement)
- [Address Replacement](#address-replacement)
- [Label Replacement](#label-replacement)
- [Quick assets (re)import](#quick-assets-reimport)
- [About prefab mode](#about-prefab-mode)
- [Quick Assets Re-import](#quick-assets-re-import)
- [About Prefab Mode](#about-prefab-mode)

## Setup the Importer

Expand Down Expand Up @@ -36,18 +36,35 @@ Once the settings file selected, you can edit rules in the inspector window. The

| Type | Example |
|----------|---------------------------------------------------------------------------------|
| Wildcard | `Asset/Sprites/Icons` |
| Wildcard | `Asset/Sprites/Icons/*` |
| Wildcard | `Asset/Sprites/Level??/*.asset` |
| Regex | `^Assets/Models/.*\.fbx` |
| Regex | `Assets/Weapons/(?<prefix>(?<category>[^/]+)/(.*/)*)(?<asset>.*_Data.*\.asset)` |

Because directory itself can be an address entry as well. You need to design your rules with caution to avoid including both folder and files at the same time.

For example,

```
Assets/
t1/
1.txt
```

| Rule Path | Rule Type | Results | Comments |
|-------------------|-----------|---------------------|----------|
| `Assets/t1` | Wildcard | `t1` and `t1/1.txt` | bad |
| `Assets/t1/*.txt` | Wildcard | `t1/1.txt` | good |
| `^Assets/t1$` | Regex | `t1` | good |
| `^Assets/t1/.*` | Regex | `t1/1.txt` | good |

## Group Replacement

The dynamic group is supported by replacing `${name}` with the extracted value from the asset path, via the use of regex capture groups. Named capture groups can be referred to in `Group Name` via `${group}`. If groups are not named, they can be referred to numerically, via `$1`, `$2`, and so on. For more information, refer to [Microsoft Docs - Substitutions in Regular Expressions](https://docs.microsoft.com/en-us/dotnet/standard/base-types/substitutions-in-regular-expressions). This only works for match type Regex.

For convenience, path elements can be referred via `${PATH[index]}`. This works for all match types.

| Asset Path | Path | Group Name | Result |
| Asset Path | Rule Path | Group Name | Result |
|--------------------------|-----------------------------------------------|--------------------------|----------------|
| `Assets/Sprites/cat.png` | `Assets/Sprites/*.png` | `${PATH[1]}` | Sprites |
| `Assets/Sprites/cat.png` | `Assets/Sprites/*.png` | `${PATH[-1]}` | Sprites |
Expand All @@ -60,7 +77,7 @@ For convenience, path elements can be referred via `${PATH[index]}`. This works

Similar to [Group Replacement](#group-replacement), address replacement is also supported.

| Asset Path | Path | Address Replacement | Result |
| Asset Path | Rule Path | Address Replacement | Result |
|------------------------|-----------------------------------------------|-----------------------------------|------------------|
| `Assets/cat/cat01.png` | `Assets/(?<category>[^/]+)/(?<asset>.*)\.png` | `${category}-${asset}` | cat-cat01 |
| `Assets/cat/cat01.png` | `Assets/(?<category>[^/]+)/(?<asset>.*)\.png` | `${PATH[0]}:${category}-${asset}` | Assets:cat-cat01 |
Expand All @@ -76,7 +93,7 @@ In another word, if you are intending to manually change the address later, leav

The importer always overrides existing labels if `LabelMode = Replace`.

## Quick assets (re)import
## Quick Assets Re-import

The importer should apply the rules whenever an asset being imported, moved, or deleted. However, if you modified rules or want to apply rules to existing assets, you need to manually apply the rules. To quickly apply the rules, select target folder(s) in the project view, right-click to open the context menu, and then click `AddressablesImporter: Check Folder(s)`. The action is more efficient than force reimport assets.

Expand All @@ -89,6 +106,6 @@ You can also use `ReimportFolders` API to process any folders in code. e.g. re-i
AddressableImporter.FolderImporter.ReimportFolders(new string[] { "Assets" });
```

## About prefab mode
## About Prefab Mode

When both prefab mode (the preview scene for editing a prefab) and the autosave feature are enabled, every modification will cause the asset to be saved and trigger the importer, leads to slow response. For performance reasons, the importer will ignore the current editing asset.
27 changes: 18 additions & 9 deletions Editor/AddressableImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,39 @@ public class FolderImporter
/// <param name="settings">Reference to the <see cref="AddressableAssetSettings"/></param>
public static void ReimportFolders(IEnumerable<String> assetPaths)
{
HashSet<string> filesToImport = new HashSet<string>();
HashSet<string> pathsToImport = new HashSet<string>();
foreach (var assetPath in assetPaths)
{
if (Directory.Exists(assetPath))
{
// Add the folder itself.
pathsToImport.Add(assetPath.Replace('\\', '/'));
// Add sub-folders.
var dirsToAdd = Directory.GetDirectories(assetPath, "*", SearchOption.AllDirectories);
foreach (var dir in dirsToAdd)
{
// Filter out .dirname and dirname~, those are invisible to Unity.
if (!dir.StartsWith(".") && !dir.EndsWith("~"))
{
pathsToImport.Add(dir.Replace('\\', '/'));
}
}
// Add files.
var filesToAdd = Directory.GetFiles(assetPath, "*", SearchOption.AllDirectories);
foreach (var file in filesToAdd)
{
// Filter out meta and DS_Store files.
if (!file.EndsWith(".meta") && !file.EndsWith(".DS_Store"))
{
filesToImport.Add(file.Replace('\\', '/'));
pathsToImport.Add(file.Replace('\\', '/'));
}
}
}
}
if (filesToImport.Count > 0)
{
Debug.Log($"AddressablesImporter: Found {filesToImport.Count} assets...");
OnPostprocessAllAssets(filesToImport.ToArray(), new string[0], new string[0], new string[0]);
}
else
if (pathsToImport.Count > 0)
{
Debug.Log($"AddressablesImporter: No files to reimport");
Debug.Log($"AddressablesImporter: Found {pathsToImport.Count} asset paths...");
OnPostprocessAllAssets(pathsToImport.ToArray(), new string[0], new string[0], new string[0]);
}
}

Expand Down

0 comments on commit 18923eb

Please sign in to comment.