Skip to content

Commit

Permalink
feat: add multiple AddressableImportSettings support (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
teck124 authored Oct 4, 2022
1 parent e359ea3 commit 5d63d14
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 101 deletions.
53 changes: 29 additions & 24 deletions Editor/AddressableImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
using UnityEditor.AddressableAssets;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityAddressableImporter.Helper;

#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif

[CreateAssetMenu(fileName = "AddressableImportSettings", menuName = "Addressables/Import Settings", order = 50)]
public class AddressableImportSettings : ScriptableObject
{
public const string kConfigObjectName = "addressableimportsettings";
public const string kDefaultPath = "Assets/AddressableAssetsData/AddressableImportSettings.asset";
[Tooltip("Toggle rules enabled state")]
[SerializeField]
public bool rulesEnabled = true;

[Tooltip("Creates a group if the specified group doesn't exist.")]
public bool allowGroupCreation = false;
Expand Down Expand Up @@ -59,30 +60,34 @@ public void CleanEmptyGroup()
}
}

public static AddressableImportSettings Instance
/// <summary>
/// Create AddressableImportSettings and Add it to AddressableImportSettingsList
/// </summary>
[MenuItem("Assets/Create/Addressables/Import Settings", false, 50)]
public static void CreateAsset()
{
get
string directoryPath = "Assets/";
string fileName = "AddressableImportSettings.asset";

foreach(var obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
AddressableImportSettings so;
// Try to locate settings from EditorBuildSettings
if (EditorBuildSettings.TryGetConfigObject(kConfigObjectName, out so))
return so;
// Try to locate settings from default path
so = AssetDatabase.LoadAssetAtPath<AddressableImportSettings>(kDefaultPath);
if (so != null) {
EditorBuildSettings.AddConfigObject(kConfigObjectName, so, true);
return so;
var assetPath = AssetDatabase.GetAssetPath(obj);
var assetDirectoryPath = AssetDatabase.IsValidFolder(assetPath) ? assetPath : Path.GetDirectoryName(assetPath);
if (AssetDatabase.IsValidFolder(assetDirectoryPath))
{
directoryPath = assetDirectoryPath;
}
// Try to locate settings from AssetDatabase
var path = AssetDatabase.FindAssets($"t:{nameof(AddressableImportSettings)}");
if (path.Length > 0) {
var assetPath = AssetDatabase.GUIDToAssetPath(path[0]);
so = AssetDatabase.LoadAssetAtPath<AddressableImportSettings>(assetPath);
EditorBuildSettings.AddConfigObject(kConfigObjectName, so, true);
return so;
}
return null;
}
}
AddressableImportSettings settings = ScriptableObject.CreateInstance<AddressableImportSettings>();
var filePath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(directoryPath, fileName));
AssetDatabase.CreateAsset(settings, filePath);

if (!AddressableImportSettingsList.Instance.SettingList.Contains(settings))
{
AddressableImportSettingsList.Instance.SettingList.Add(settings);
}

AssetDatabase.SaveAssets();
Selection.activeObject = settings;
}
}
92 changes: 92 additions & 0 deletions Editor/AddressableImportSettingsList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityAddressableImporter.Helper;

public class AddressableImportSettingsList : ScriptableObject
{
public const string kConfigObjectName = "addressableimportsettingslist";
public const string kDefaultPath = "Assets/AddressableAssetsData/AddressableImportSettingsList.asset";
public List<AddressableImportSettings> SettingList;
public List<AddressableImportSettings> EnabledSettingsList => SettingList.Where((s) => s?.rulesEnabled == true).ToList();

public static AddressableImportSettingsList Instance
{
get
{
AddressableImportSettingsList so;

// Try to locate settings from EditorBuildSettings
if (EditorBuildSettings.TryGetConfigObject(kConfigObjectName, out so))
{
return so;
}
// Try to locate settings from default path
so = AssetDatabase.LoadAssetAtPath<AddressableImportSettingsList>(kDefaultPath);
if (so != null)
{
EditorBuildSettings.AddConfigObject(kConfigObjectName, so, true);
return so;
}
// Try to locate settings from AssetDatabase
var guidList = AssetDatabase.FindAssets($"t:{nameof(AddressableImportSettingsList)}");
if (guidList.Length > 0)
{
var assetPath = AssetDatabase.GUIDToAssetPath(guidList[0]);
so = AssetDatabase.LoadAssetAtPath<AddressableImportSettingsList>(assetPath);
EditorBuildSettings.AddConfigObject(kConfigObjectName, so, true);
return so;
}

// If AddressableImportSettingsList doesn't exist but AddressableImportSettings exists. create automatically.
var importSettingsGuidList = AssetDatabase.FindAssets($"t:{nameof(AddressableImportSettings)}");
if (importSettingsGuidList.Length > 0)
{
var settingList = importSettingsGuidList.Select((guid) => AssetDatabase.LoadAssetAtPath<AddressableImportSettings>(AssetDatabase.GUIDToAssetPath(guid))).ToList();
var asset = ScriptableObject.CreateInstance<AddressableImportSettingsList>();
asset.SettingList = settingList;
var path = Path.Combine(Path.GetDirectoryName(AssetDatabase.GUIDToAssetPath(importSettingsGuidList[0])),
nameof(AddressableImportSettingsList) + ".asset");
Debug.LogFormat("AddressableImportSettingsList doesn't exist. so it is created automatically. path : {0}", path);
AssetDatabase.CreateAsset(asset, path);
AssetDatabase.SaveAssets();
so = asset;
EditorBuildSettings.AddConfigObject(kConfigObjectName, so, true);
return so;
}

return null;
}
}

public bool RemoveMissingImportSettings()
{
bool removedAnySettings = false;
for (int i = SettingList.Count - 1; i >= 0; --i)
{
if (SettingList[i] == null)
{
SettingList.RemoveAt(i);
removedAnySettings = true;
}
}

return removedAnySettings;
}

[ButtonMethod]
public void Reset()
{
var importSettingsGuidList = AssetDatabase.FindAssets($"t:{nameof(AddressableImportSettings)}");
SettingList = importSettingsGuidList.Select((guid) => AssetDatabase.LoadAssetAtPath<AddressableImportSettings>(AssetDatabase.GUIDToAssetPath(guid))).ToList();
AssetDatabase.SaveAssets();
}

[ButtonMethod]
public void Documentation()
{
Application.OpenURL("https://github.com/favoyang/unity-addressable-importer/blob/master/Documentation~/AddressableImporter.md");
}
}
11 changes: 11 additions & 0 deletions Editor/AddressableImportSettingsList.cs.meta

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

49 changes: 37 additions & 12 deletions Editor/AddressableImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,25 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
}
return;
}
var importSettings = AddressableImportSettings.Instance;
if (importSettings == null)
var importSettingsList = AddressableImportSettingsList.Instance;
if (importSettingsList == null)
{
Debug.LogWarningFormat("[AddressableImporter] import settings file not found.\nPlease go to Assets/AddressableAssetsData folder, right click in the project window and choose 'Create > Addressables > Import Settings'.");
return;
}
if (importSettings.rules == null || importSettings.rules.Count == 0)

var hasRuleSettingsList = importSettingsList.EnabledSettingsList.Where(s => s.rules.Count > 0).ToList();
var hasRules = hasRuleSettingsList.Count != 0;

if (!hasRules)
{
// if AddressableImportSettings is Deleted, Remove missing ImportSettings
if (importSettingsList.RemoveMissingImportSettings())
{
AssetDatabase.SaveAssets();
}
return;
}

// Cache the selection active object
var cachedSelectionActiveObject = selectionActiveObject;
Expand All @@ -85,8 +96,11 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
(float) i / importedAssets.Length))
break;

if (prefabStage == null || prefabAssetPath != importedAsset) // Ignore current editing prefab asset.
dirty |= ApplyImportRule(importedAsset, null, settings, importSettings);
foreach (var importSettings in hasRuleSettingsList)
{
if (prefabStage == null || prefabAssetPath != importedAsset) // Ignore current editing prefab asset.
dirty |= ApplyImportRule(importedAsset, null, settings, importSettings);
}
}
}
finally
Expand All @@ -100,25 +114,36 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
if (IsAssetIgnored(movedAsset))
continue;
var movedFromAssetPath = movedFromAssetPaths[i];
if (prefabStage == null || prefabAssetPath != movedAsset) // Ignore current editing prefab asset.
dirty |= ApplyImportRule(movedAsset, movedFromAssetPath, settings, importSettings);

foreach (var importSettings in hasRuleSettingsList)
{
if (prefabStage == null || prefabAssetPath != movedAsset) // Ignore current editing prefab asset.
dirty |= ApplyImportRule(movedAsset, movedFromAssetPath, settings, importSettings);
}
}

foreach (var deletedAsset in deletedAssets)
{
if (IsAssetIgnored(deletedAsset))
continue;
if (TryGetMatchedRule(deletedAsset, importSettings, out var matchedRule))

foreach (var importSettings in hasRuleSettingsList)
{
var guid = AssetDatabase.AssetPathToGUID(deletedAsset);
if (!string.IsNullOrEmpty(guid) && settings.RemoveAssetEntry(guid))
if (TryGetMatchedRule(deletedAsset, importSettings, out var matchedRule))
{
dirty = true;
Debug.LogFormat("[AddressableImporter] Entry removed for {0}", deletedAsset);
var guid = AssetDatabase.AssetPathToGUID(deletedAsset);
if (!string.IsNullOrEmpty(guid) && settings.RemoveAssetEntry(guid))
{
dirty = true;
Debug.LogFormat("[AddressableImporter] Entry removed for {0}", deletedAsset);
}
}
}
}

// if AddressableImportSettings is Deleted, Remove missing ImportSettings
dirty |= importSettingsList.RemoveMissingImportSettings();

if (dirty)
{
AssetDatabase.SaveAssets();
Expand Down
66 changes: 1 addition & 65 deletions Editor/Helper/AddressableImportSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,8 @@ namespace UnityAddressableImporter.Helper.Internal
#endif

[CustomEditor(typeof(AddressableImportSettings), true), CanEditMultipleObjects]
public class AddressableImportSettingsEditor : Editor
public class AddressableImportSettingsEditor : ScriptableObjectEditor<AddressableImportSettings>
{
private List<MethodInfo> _methods;
private AddressableImportSettings _target;

#if ODIN_INSPECTOR
private PropertyTree _propertyTree;
#endif

private void OnEnable()
{
_target = target as AddressableImportSettings;
if (_target == null) return;

#if ODIN_INSPECTOR
_propertyTree = PropertyTree.Create(_target);
#endif

_methods = AddressableImporterMethodHandler.CollectValidMembers(_target.GetType());
}

public override void OnInspectorGUI()
{

if (DrawWithOdin() == false)
{
DrawBaseEditor();
}

DrawCommands();

serializedObject.ApplyModifiedProperties();
}

private void DrawBaseEditor()
{
base.OnInspectorGUI();
}

private void DrawCommands()
{
if (_methods == null) return;
AddressableImporterMethodHandler.OnInspectorGUI(_target, _methods);
}

private void OnDisable()
{
#if ODIN_INSPECTOR
_propertyTree?.Dispose();
#endif
}

private bool DrawWithOdin()
{
var hasOdinAsset = false;
#if ODIN_INSPECTOR_3
hasOdinAsset = true;
#endif
if (!hasOdinAsset) return false;

#if ODIN_INSPECTOR
_propertyTree?.Draw();
_propertyTree?.ApplyChanges();
#endif

return true;
}
}
}
21 changes: 21 additions & 0 deletions Editor/Helper/AddressableImportSettingsListEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <summary>
/// ButtonMethodAttribute,
/// modified from https://github.com/Deadcows/MyBox/blob/master/Attributes/ButtonMethodAttribute.cs
/// </summary>
namespace UnityAddressableImporter.Helper.Internal
{
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;

#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
#endif

[CustomEditor(typeof(AddressableImportSettingsList), true), CanEditMultipleObjects]
public class AddressableImportSettingsListEditor : ScriptableObjectEditor<AddressableImportSettingsList>
{

}
}
11 changes: 11 additions & 0 deletions Editor/Helper/AddressableImportSettingsListEditor.cs.meta

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

Loading

0 comments on commit 5d63d14

Please sign in to comment.