0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Unity】Addressable で指定ディレクトリ以下にあるアセットに自動でAddressを設定する

Posted at

Addressable Asset Sysytem では Addressable Group の EditorWidow からアドレスを設定する作りとなっており、自動化したい場合は自前でなにかしらの処理を書く必要がある

大体同じ処理を書くと思うので自動的にアドレスを設定する PostProcessor を作成した

using System;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;

public class AddressablePostProcessor : AssetPostprocessor
{
    private static readonly string targetPath = "Assets/AddressableAsset/";
    
    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        var dirty = false;
        dirty |= ChangeEntry(importedAssets, false);
        dirty |= ChangeEntry(deletedAssets, true);
        dirty |= ChangeEntry(movedAssets, false);
        
        if (dirty)
        {
            AssetDatabase.SaveAssets();
        }
    }

    private static bool ChangeEntry(string[] paths, bool delete)
    {
        var settings = AddressableAssetSettingsDefaultObject.Settings;
        var groups = settings.groups;
        var dirty = false;
       
        foreach (var path in paths)
        {
            if (!path.StartsWith(targetPath))
                continue;
            
            if (AssetDatabase.IsValidFolder(path))
                continue;

            var assetPath = path.Replace(targetPath, "");

            var targetGroup = settings.DefaultGroup;
            var rootIndex = assetPath.IndexOf(System.IO.Path.DirectorySeparatorChar, StringComparison.Ordinal);
            if (rootIndex >= 0)
            { 
                var rootPath = assetPath.Substring(0, rootIndex);
                var groupIndex = groups.FindIndex(g => g.Name == rootPath);
                if (groupIndex >= 0)
                {
                    targetGroup = groups[groupIndex];
                }
                else
                {
                    var groupTemplate = settings.GetGroupTemplateObject(0) as AddressableAssetGroupTemplate;
                    targetGroup = settings.CreateGroup(rootPath, false, false, false, null, groupTemplate.GetTypes());
                }
                
                assetPath = assetPath.Substring(rootIndex + 1);
            }

            var guid = AssetDatabase.AssetPathToGUID(path);
            if (delete)
            {
                settings.RemoveAssetEntry(guid);
            }
            else
            {
                var lastIndex = assetPath.LastIndexOf(".", StringComparison.Ordinal);
                var address = assetPath.Substring(0, lastIndex);
                
                var entry = settings.CreateOrMoveEntry(guid, targetGroup);
                if (entry.address == address)
                    continue;
                
                entry.address = address;
            }

            dirty = true;
        }

        return dirty;
    }
}

これを利用すると以下の様な構成の際に

スクリーンショット 2023-08-06 21.05.47.png

このようなアドレスが自動的に設定される

スクリーンショット 2023-08-06 21.06.44.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?