Skip to content

Commit

Permalink
Import rule supports specifying asset group name.
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Jun 29, 2019
1 parent bd4c5c2 commit c9fd725
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 16 deletions.
6 changes: 6 additions & 0 deletions AddressableImporter/Editor/AddressableImportRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class AddressableImportRule
/// </summary>
public AddressableImportRuleMatchType matchType;

/// <summary>
/// Addressable group name
/// </summary>
[Tooltip("Leaves blank for the default group.")]
public string groupName;

/// <summary>
/// Label reference list
/// </summary>
Expand Down
38 changes: 29 additions & 9 deletions AddressableImporter/Editor/AddressableImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class AddressableImporter : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
var type = typeof(AddressableImporter);
var settings = AddressableAssetSettingsDefaultObject.Settings;
var importSettings = AddressableImportSettings.Instance;
if (importSettings.rules == null || importSettings.rules.Count == 0)
Expand All @@ -22,22 +21,30 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
{
if (rule.Match(path))
{
var entry = CreateOrUpdateAddressableAssetEntry(settings, path, rule.labels);
entriesAdded.Add(entry);
if (rule.HasLabel)
Debug.LogFormat("[{0}] Entry created for {1} with labels {2}", type, path, string.Join(", ", entry.labels));
else
Debug.LogFormat("[{0}] Entry created for {1}", type, path);
var entry = CreateOrUpdateAddressableAssetEntry(settings, path, rule.groupName, rule.labels);
if (entry != null)
{
entriesAdded.Add(entry);
if (rule.HasLabel)
Debug.LogFormat("[AddressableImporter] Entry created for {0} with labels {1}", path, string.Join(", ", entry.labels));
else
Debug.LogFormat("[AddressableImporter] Entry created for {0}", path);
}
}
}
}
if (entriesAdded.Count > 0)
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true);
}

static AddressableAssetEntry CreateOrUpdateAddressableAssetEntry(AddressableAssetSettings settings, string path, IEnumerable<string> labels)
static AddressableAssetEntry CreateOrUpdateAddressableAssetEntry(AddressableAssetSettings settings, string path, string groupName, IEnumerable<string> labels)
{
var group = settings.DefaultGroup;
var group = GetGroup(settings, groupName);
if (group == null)
{
Debug.LogErrorFormat("[AddressableImporter] Failed to find group {0} when importing {1}. Please check the group exists, then reimport the asset.", groupName, path);
return null;
}
var guid = AssetDatabase.AssetPathToGUID(path);
var entry = settings.CreateOrMoveEntry(guid, group);
// Override address if address is a path
Expand All @@ -51,5 +58,18 @@ static AddressableAssetEntry CreateOrUpdateAddressableAssetEntry(AddressableAsse
}
return entry;
}

/// <summary>
/// Find asset group by given name. Return default group if given name is null.
/// </summary>
static AddressableAssetGroup GetGroup(AddressableAssetSettings settings, string groupName)
{
if (groupName != null)
groupName.Trim();
if (string.IsNullOrEmpty(groupName))
return settings.DefaultGroup;
return settings.groups.Find(g => g.Name == groupName);
}

}

7 changes: 7 additions & 0 deletions LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ The importer marks assets as addressable, by applying to files having a path mat

## Usage

You should create a single AddressableImportSettings file located at `Assets/AddressableAssetsData/AddressableImportSettings.asset` in your project. To create it, go to `Assets/AddressableAssetsData` folder, right click in your project window and choose `Create > Addressable Assets > Import Settings`.
You should create a single AddressableImportSettings file located at `Assets/AddressableAssetsData/AddressableImportSettings.asset`. To create it, go to `Assets/AddressableAssetsData` folder, right click in your project window and choose `Create > Addressable Assets > Import Settings`.

If no settings file exists, an empty one will be created for you, when import any new asset.
If no settings file exists, an empty one will be created when importing any new asset.

Once the settings file selected, you can edit rules in the inspector window.
Once the settings file selected, you can edit rules in the inspector window. Then click `File > Save Project` to apply the changes.

![AddressableImportSettings Insepctor](./Documentation~/AddressableImportSettings-Insepctor.png)

Rule properties
Create a rule
- Path, the path pattern
- Match type
- Wildcard, `*` matches any number of characters, `?` matches a single character
- Regex
- Label references, the labels to add
- Group name, leaves blank for the default group
- Labels, the labels to add

Rule Examples

Expand All @@ -28,6 +29,6 @@ Rule Examples
| Wildcard | Asset/Sprites/Level??/*.asset |
| Regex | ^Assets/Models/.*\\.fbx |

Notice for moved or re-imported assets
Notices for moved or re-imported assets
- The importer will not override existing labels.
- The importer will only override address if it looks like a path (starts with `Assets/`). In another word, if you changed the address, and reimport or move it later, the address remains no change.
- The importer will only override address if it looks like a path (starts with `Assets/`). In another word, if you changed the address, then reimport or move it, the address remains no change.
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9fd725

Please sign in to comment.