Last active
April 17, 2020 21:46
-
-
Save FuzzySlipper/e70665675f703fd1053394de930f06e6 to your computer and use it in GitHub Desktop.
Addressables are annoying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.Serializable] | |
public abstract class AssetEntry { | |
public AssetReference AssetReference = new AssetReference(); | |
public string Path; | |
public abstract System.Type Type { get; } | |
public abstract UnityEngine.Object AssetObject { get; } | |
} | |
[System.Serializable] | |
public class AssetEntry<T> : AssetEntry where T : UnityEngine.Object { | |
public override Type Type { get { return typeof(T); } } | |
public override Object AssetObject { get { return AssetReference.editorAsset; }} | |
} | |
//Examples | |
[System.Serializable] | |
public class GameObjectReference : AssetEntry<GameObject>{} | |
[System.Serializable] | |
public class MaterialReference : AssetEntry<Material> { } | |
[System.Serializable] | |
public class SpriteReference : AssetEntry<Sprite> { } | |
// Editor Drawer | |
[CustomPropertyDrawer(typeof(AssetEntry), true)] | |
public class AssetEntryDrawer : PropertyDrawer { | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { | |
if (property.isArray) { | |
for (int i = 0; i < property.arraySize; i++) { | |
Display(position, property.GetArrayElementAtIndex(i), label); | |
position.y += 20; | |
} | |
return; | |
} | |
Display(position, property, label); | |
} | |
private void Display(Rect position, SerializedProperty property, GUIContent label) { | |
if (property.GetTargetObjectOfProperty() is AssetEntry target) { | |
if (target.AssetObject == null) { | |
if (!string.IsNullOrEmpty(target.Path)) { | |
ApplyModified(property, target, AssetDatabase.LoadMainAssetAtPath(target.Path)); | |
} | |
} | |
else { | |
var path = AssetDatabase.GetAssetPath(target.AssetObject); | |
if (target.Path != path) { | |
target.Path = path; | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
label.tooltip = target.Path.Replace("Assets/", ""); | |
if (!string.IsNullOrEmpty(target.AssetReference.SubObjectName)) { | |
label.tooltip += "/"; | |
label.tooltip += target.AssetReference.SubObjectName; | |
} | |
var obj = EditorGUI.ObjectField(position, label, target.AssetObject, target.Type, false); | |
if (obj != target.AssetObject) { | |
ApplyModified(property, target, obj); | |
} | |
} | |
else { | |
EditorGUI.LabelField(position, property.type); | |
} | |
} | |
private void ApplyModified(SerializedProperty property, AssetEntry entry, UnityEngine.Object obj) { | |
if (obj != null) { | |
AddressableAssetEditorUtility.GetOrCreateEntry(obj); | |
} | |
entry.AssetReference.SetEditorAsset(obj); | |
entry.Path = obj != null ? AssetDatabase.GetAssetPath(obj) : ""; | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
// Addressable Editor Utility | |
public static class AddressableAssetEditorUtility { | |
public static AddressableAssetEntry GetOrCreateEntry(Object o) { | |
AddressableAssetSettings aaSettings = AddressableAssetSettingsDefaultObject.Settings; | |
AddressableAssetEntry entry = null; | |
bool foundAsset = AssetDatabase.TryGetGUIDAndLocalFileIdentifier(o, out var guid, out long _); | |
var path = AssetDatabase.GUIDToAssetPath(guid); | |
if (foundAsset && (path.ToLower().Contains("assets"))) { | |
if (aaSettings != null) { | |
entry = aaSettings.FindAssetEntry(guid); | |
} | |
} | |
if (entry != null) { | |
return entry; | |
} | |
entry = aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup); | |
return entry; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment