Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Jun 28, 2019
1 parent c707c6a commit bd4c5c2
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 2 deletions.
8 changes: 8 additions & 0 deletions AddressableImporter.meta

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

8 changes: 8 additions & 0 deletions AddressableImporter/Editor.meta

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

84 changes: 84 additions & 0 deletions AddressableImporter/Editor/AddressableImportRule.cs
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;
}
}
}
}
}
11 changes: 11 additions & 0 deletions AddressableImporter/Editor/AddressableImportRule.cs.meta

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

35 changes: 35 additions & 0 deletions AddressableImporter/Editor/AddressableImportSettings.cs
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 AddressableImporter/Editor/AddressableImportSettings.cs.meta

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

55 changes: 55 additions & 0 deletions AddressableImporter/Editor/AddressableImporter.cs
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;
}
}

11 changes: 11 additions & 0 deletions AddressableImporter/Editor/AddressableImporter.cs.meta

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 33 additions & 2 deletions README.md
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.

0 comments on commit bd4c5c2

Please sign in to comment.