2
1

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 5 years have passed since last update.

Addressable AssetsのSimplify Entry Nameでアドレス名にラベル追加で重複回避

Last updated at Posted at 2019-09-06

名称の重複について

UnityのAddressable AssetsではAsset Addressを文字列で名前指定して参照することができます。
Asset Referenceを使用していても、内部的にはこの文字列を使います。
これにより、重複する名称を設定すると正しく参照されないため、名前には気を付けないといけません。

例えば、うちに鯛と味噌という猫がいるんですが、このモデルをゲームで使うとします。
before.PNG
どちらもNeko.prefabという名前にしてしまうと、Simplify Entry Nameしたときに下のようにアドレスがNekoになってしまい、重複してしまいます。
before2.PNG
(prefab名変えるのが普通だと思うので、これは極端な例です。)

同名でもラベルで分けたい

プロジェクトにあるLibrary内のAddressable Assetsのパッケージキャッシュに手を加えます。
なので、パッケージのアップデートがあった際などに巻き戻ったりするかもなので注意です。

Editor\GUI\AddressableAssetsSettingsGroupTreeView.csの657行目あたりに以下を追記

Editor\GUI\AddressableAssetsSettingsGroupTreeView.cs
menu.AddItem (new GUIContent ("Simplify Entry Names With Label"), false, SimplifyAddressesWithLabel, selectedNodes);

同884行目あたりに以下を追記

protected void SimplifyAddressesWithLabel (object context) {
    List<AssetEntryTreeViewItem> selectedNodes = context as List<AssetEntryTreeViewItem>;
    if (selectedNodes == null || selectedNodes.Count < 1)
        return;
    var entries = new List<AddressableAssetEntry> ();
    HashSet<AddressableAssetGroup> modifiedGroups = new HashSet<AddressableAssetGroup> ();
    foreach (var item in selectedNodes) {
        if (item.entry.labels == null || item.entry.labels.Count < 1) {
            item.entry.SetAddress (Path.GetFileNameWithoutExtension (item.entry.AssetPath), false);
        } else {
            string label = m_Editor.settings.labelTable.GetString (item.entry.labels, 200);
            item.entry.SetAddress (label.Replace (", ", "_") + "_" + Path.GetFileNameWithoutExtension (item.entry.AssetPath), false);
        }
        entries.Add (item.entry);
        modifiedGroups.Add (item.entry.parentGroup);
    }
    foreach (var g in modifiedGroups)
        g.SetDirty (AddressableAssetSettings.ModificationEvent.EntryModified, entries, false, true);
    m_Editor.settings.SetDirty (AddressableAssetSettings.ModificationEvent.EntryModified, entries, true, false);
}

これでラベルを追加するSimplify Entry Name With Labelが増えます。
menu.png

実行すると、{ラベル}_ファイル名というアドレス名になります。
after1.PNG

ラベルが複数ある場合は以下のようになります。
after2.PNG

できたけど、はたしてこういうラベル運用でいいのか・・・

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?