Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active July 9, 2024 23:57
Show Gist options
  • Save instance-id/fac9dc8c0365d30e4046fd17ecb3bce1 to your computer and use it in GitHub Desktop.
Save instance-id/fac9dc8c0365d30e4046fd17ecb3bce1 to your computer and use it in GitHub Desktop.
An example of creating an Addressable GameObject from a prefab with a simplified name
// ------------------------------------------------------------------------------------ AddressInfo
// --- AddressInfo --------------------------------------------------------------------------------
public static class AddressInfo
{
public static string prefabGroup = "Prefabs";
public static string prefabLabel = "Prefab";
}
// ------------------------------------------------------------------------------ AddressableHelper
// --- AddressableHelper --------------------------------------------------------------------------
public static class AddressableHelper
{
static AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
public static void SimplifyAddresses(Dictionary<AddressableAssetEntry, AddressableAssetGroup> entries)
{
foreach (var group in entries)
group.Value.SetDirty(AddressableAssetSettings.ModificationEvent.EntryModified, group.Key, false, true);
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryModified, entries, true, false);
}
public static Object GetPrefabAsset(this GameObject go)
{
var pathToPrefab = GetPrefabPath(go);
return PrefabUtility.GetCorrespondingObjectFromSourceAtPath(go, pathToPrefab);
}
public static string GetPrefabPath(GameObject go)
{
return PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);
}
public static AddressableAssetEntry CreateAssetEntry<T>(T source, string groupName, string label) where T : Object
{
var entry = CreateAssetEntry(source, groupName);
if (source != null) source.AddAddressableAssetLabel(label);
return entry;
}
public static AddressableAssetGroup GetCurrentAddressableAssetGroup(this Object source)
{
if (source == null || !AssetDatabase.Contains(source))
return null;
var entry = source.GetAddressableAssetEntry();
return entry?.parentGroup;
}
}
// ----------------------------------------------------------------------------- CreatedAddressable
// --- CreatedAddressable -------------------------------------------------------------------------
public bool createAddressable;
public void CreatedAddressable(GameObject prefab)
{
AddressableAssetEntry prefabEntry;
if (createAddressable)
prefabEntry = AddressableHelper.CreateAssetEntry(
AssetDatabase.LoadAssetAtPath<GameObject>(AddressableHelper.GetPrefabPath(prefab)),
AddressInfo.prefabGroup,
AddressInfo.prefabLabel
);
else prefabEntry = assetList[i].gameObject.GetPrefabAsset().GetAddressableAssetEntry();
prefabEntry.SetAddress(Path.GetFileNameWithoutExtension(prefabEntry.address), false);
Dictionary<AddressableAssetEntry, AddressableAssetGroup> entries = new Dictionary<AddressableAssetEntry, AddressableAssetGroup>
{
{ prefabEntry, prefabEntry.parentGroup },
};
AddressableHelper.SimplifyAddresses(entries);
}
using System.Collections.Generic;
using System.IO;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
namespace instance.id.ECS.Editor
{
public class NameShortenerOdin : OdinEditorWindow
{
[MenuItem("Tools/instance.id/Name Shortener")]
private static void ShowWindow()
{
var window = GetWindow<NameShortenerOdin>();
window.position = GUIHelper.GetEditorWindowRect().AlignCenter(400, 400);
window.titleContent = new GUIContent("Name Shortener");
}
// ----------------------------------------------------------------------------- Asset List
// --- Asset List -------------------------------------------------------------------------
[BoxGroup("Addressable Tools")] //formatter:off
[TitleGroup("Addressable Tools/Create Short-named Assets")]
[LabelText("Add GameObjects")]
[LabelWidth(100)]
[SerializeField]
//formatter:on
public List<GameObject> assetList;
[TitleGroup("Addressable Tools/Create Short-named Assets")] //formatter:off
[LabelText("Create Addressable Asset?")]
[ToggleLeft]
[SerializeField]
//formatter:on
public bool createAddressable;
Dictionary<AddressableAssetEntry, AddressableAssetGroup> entries
= new Dictionary<AddressableAssetEntry, AddressableAssetGroup>();
[Button("Shorten Names")]
private void ShortenName()
{
entries.Clear();
for (int i = 0; i < assetList.Count; i++)
{
AddressableAssetEntry prefabEntry;
if (createAddressable)
prefabEntry = AddressableHelper.CreateAssetEntry(
assetList[i].gameObject,
AddressInfo.prefabGroup,
AddressInfo.prefabLabel
);
else prefabEntry = assetList[i].gameObject.GetPrefabAsset().GetAddressableAssetEntry();
prefabEntry.SetAddress(Path.GetFileNameWithoutExtension(prefabEntry.address), false);
entries.Add(prefabEntry, prefabEntry.parentGroup);
}
AddressableHelper.SimplifyAddresses(entries);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment