-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using UnityEngine.AddressableAssets; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
public enum AddressableImportRuleMatchType | ||
{ | ||
/// <summary> | ||
/// Simple wildcard | ||
/// *, matches any number of characters | ||
/// ?, matches a single character | ||
/// </summary> | ||
Wildcard = 0, | ||
|
||
/// <summary> | ||
/// Regex pattern | ||
/// </summary> | ||
Regex | ||
} | ||
|
||
[System.Serializable] | ||
public class AddressableImportRule | ||
{ | ||
/// <summary> | ||
/// Path pattern | ||
/// </summary> | ||
public string path; | ||
|
||
/// <summary> | ||
/// Path match type | ||
/// </summary> | ||
public AddressableImportRuleMatchType matchType; | ||
|
||
/// <summary> | ||
/// Label reference list | ||
/// </summary> | ||
public List<AssetLabelReference> labelRefs; | ||
|
||
// public bool simplified; | ||
|
||
public bool HasLabel | ||
{ | ||
get | ||
{ | ||
return labelRefs != null && labelRefs.Count > 0; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns True if given assetPath matched with the rule. | ||
/// </summary> | ||
public bool Match(string assetPath) | ||
{ | ||
if (matchType == AddressableImportRuleMatchType.Wildcard) | ||
{ | ||
if (path.Contains("*") || path.Contains("?")) | ||
{ | ||
var regex = "^" + Regex.Escape(path).Replace(@"\*", ".*").Replace(@"\?", "."); | ||
return Regex.IsMatch(assetPath, regex); | ||
} | ||
else | ||
return assetPath.StartsWith(path); | ||
} else if (matchType == AddressableImportRuleMatchType.Regex) | ||
return Regex.IsMatch(assetPath, path); | ||
return false; | ||
} | ||
|
||
public IEnumerable<string> labels | ||
{ | ||
get | ||
{ | ||
if (labelRefs == null) | ||
yield break; | ||
else | ||
{ | ||
foreach (var labelRef in labelRefs) | ||
{ | ||
yield return labelRef.labelString; | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System.Collections.Generic; | ||
|
||
[CreateAssetMenu(fileName = "AddressableImportSettings", menuName = "Addressable Assets/Import Settings", order = 50)] | ||
public class AddressableImportSettings : ScriptableObject | ||
{ | ||
public const string kDefaultConfigObjectName = "addressableimportsettings"; | ||
public const string kDefaultPath = "Assets/AddressableAssetsData/AddressableImportSettings.asset"; | ||
|
||
public List<AddressableImportRule> rules; | ||
|
||
public static AddressableImportSettings Instance | ||
{ | ||
get | ||
{ | ||
AddressableImportSettings so; | ||
// Try to ocate settings via EditorBuildSettings. | ||
if (EditorBuildSettings.TryGetConfigObject(kDefaultConfigObjectName, out so)) | ||
return so; | ||
// Try to locate settings via path. | ||
so = AssetDatabase.LoadAssetAtPath<AddressableImportSettings>(kDefaultPath); | ||
if (so == null) | ||
{ | ||
// Create new settings. | ||
so = CreateInstance<AddressableImportSettings>(); | ||
AssetDatabase.CreateAsset(so, kDefaultPath); | ||
AssetDatabase.SaveAssets(); | ||
Debug.LogFormat("[AddressableImportSettings] Created default settings at {0}", kDefaultPath); | ||
} | ||
EditorBuildSettings.AddConfigObject(kDefaultConfigObjectName, so, true); | ||
return so; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
AddressableImporter/Editor/AddressableImportSettings.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEditor; | ||
using UnityEditor.AddressableAssets; | ||
using UnityEditor.AddressableAssets.Settings; | ||
using System; | ||
|
||
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) | ||
return; | ||
var entriesAdded = new List<AddressableAssetEntry>(); | ||
foreach (string path in importedAssets) | ||
{ | ||
foreach (var rule in importSettings.rules) | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
if (entriesAdded.Count > 0) | ||
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true); | ||
} | ||
|
||
static AddressableAssetEntry CreateOrUpdateAddressableAssetEntry(AddressableAssetSettings settings, string path, IEnumerable<string> labels) | ||
{ | ||
var group = settings.DefaultGroup; | ||
var guid = AssetDatabase.AssetPathToGUID(path); | ||
var entry = settings.CreateOrMoveEntry(guid, group); | ||
// Override address if address is a path | ||
if (string.IsNullOrEmpty(entry.address) || entry.address.StartsWith("Assets/")) | ||
entry.address = path; | ||
// Add labels | ||
foreach (var label in labels) | ||
{ | ||
if (!entry.labels.Contains(label)) | ||
entry.labels.Add(label); | ||
} | ||
return entry; | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
# AddressableImporter | ||
A rule based addressable asset importer | ||
# Unity Addressable Importer | ||
A simple rule based addressable asset importer. | ||
|
||
The importer marks assets as addressable, by applying to files having a path matching the rule pattern. | ||
|
||
## 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`. | ||
|
||
If no settings file exists, an empty one will be created for you, when import any new asset. | ||
|
||
Once the settings file selected, you can edit rules in the inspector window. | ||
|
||
![AddressableImportSettings Insepctor](./Documentation~/AddressableImportSettings-Insepctor.png) | ||
|
||
Rule properties | ||
- Path, the path pattern | ||
- Match type | ||
- Wildcard, `*` matches any number of characters, `?` matches a single character | ||
- Regex | ||
- Label references, the labels to add | ||
|
||
Rule Examples | ||
|
||
| Type | Example | | ||
|----------|---------------------| | ||
| Wildcard | Asset/Sprites/Icons | | ||
| Wildcard | Asset/Sprites/Level??/*.asset | | ||
| Regex | ^Assets/Models/.*\\.fbx | | ||
|
||
Notice 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. |