public void GetPathAndGUIDFromTarget_FromNullObject_Fails() { Assert.IsFalse(AddressableAssetUtility.GetPathAndGUIDFromTarget(null, out var actualPath, out var actualGUID, out var actualType)); Assert.IsEmpty(actualPath); Assert.IsEmpty(actualGUID); Assert.IsEmpty(actualGUID); Assert.IsNull(actualType); }
static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create) { if (create && aaSettings.DefaultGroup.ReadOnly) { Debug.LogError("Current default group is ReadOnly. Cannot add addressable assets to it"); return; } Undo.RecordObject(aaSettings, "AddressableAssetSettings"); var targetInfos = new List <TargetInfo>(); foreach (var t in targets) { if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out var path, out var guid, out var mainAssetType)) { targetInfos.Add(new TargetInfo() { Guid = guid, Path = path, MainAssetType = mainAssetType }); } } if (!create) { targetInfos.ForEach(ti => { AddressableAssetGroup group = aaSettings.FindAssetEntry(ti.Guid).parentGroup; aaSettings.RemoveAssetEntry(ti.Guid); AddressableAssetUtility.OpenAssetIfUsingVCIntegration(group); }); } else { var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path)); var resourcePaths = resourceTargets.Select(t => t.Path).ToList(); var resourceGuids = resourceTargets.Select(t => t.Guid).ToList(); AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, resourcePaths, resourceGuids); var entriesAdded = new List <AddressableAssetEntry>(); var modifiedGroups = new HashSet <AddressableAssetGroup>(); var otherTargetInfos = targetInfos.Except(resourceTargets); foreach (var info in otherTargetInfos) { var e = aaSettings.CreateOrMoveEntry(info.Guid, aaSettings.DefaultGroup, false, false); entriesAdded.Add(e); modifiedGroups.Add(e.parentGroup); } foreach (var g in modifiedGroups) { g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true); AddressableAssetUtility.OpenAssetIfUsingVCIntegration(g); } aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false); } }
public void GetPathAndGUIDFromTarget_FromPrefabObject_Fails() { var obj = GameObject.CreatePrimitive(PrimitiveType.Cube); Assert.IsFalse(AddressableAssetUtility.GetPathAndGUIDFromTarget(obj, out var actualPath, out var actualGUID, out var actualType)); Assert.IsEmpty(actualPath); Assert.IsEmpty(actualGUID); Assert.IsEmpty(actualGUID); Assert.IsNull(actualType); }
public void GetPathAndGUIDFromTarget_FromPrefabAsset_ReturnsCorrectPathGUIDType() { var expectedGUID = CreateTestPrefabAsset(GetAssetPath("prefab1.prefab"), "prefab1"); var expectedPath = AssetDatabase.GUIDToAssetPath(expectedGUID); var obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(expectedPath); Assert.IsTrue(AddressableAssetUtility.GetPathAndGUIDFromTarget(obj, out var actualPath, out var actualGUID, out var actualType)); Assert.AreEqual(expectedPath, actualPath); Assert.AreEqual(expectedGUID, actualGUID); Assert.AreEqual(typeof(GameObject), actualType); AssetDatabase.DeleteAsset(expectedPath); }
static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create) { if (create && aaSettings.DefaultGroup.ReadOnly) { Debug.LogError("Current default group is ReadOnly. Cannot add addressable assets to it"); return; } Undo.RecordObject(aaSettings, "AddressableAssetSettings"); string path; var guid = string.Empty; //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel")) { var entriesAdded = new List <AddressableAssetEntry>(); var modifiedGroups = new HashSet <AddressableAssetGroup>(); Type mainAssetType; foreach (var t in targets) { if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType)) { if (create) { if (AddressableAssetUtility.IsInResources(path)) { AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, new List <string> { path }); } else { var e = aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup, false, false); entriesAdded.Add(e); modifiedGroups.Add(e.parentGroup); } } else { aaSettings.RemoveAssetEntry(guid); } } } if (create) { foreach (var g in modifiedGroups) { g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true); } aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false); } } }
static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create) { Undo.RecordObject(aaSettings, "AddressableAssetSettings"); string path; var guid = string.Empty; //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel")) { var entriesAdded = new List <AddressableAssetEntry>(); foreach (var t in targets) { if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid)) { if (create) { if (AddressableAssetUtility.IsInResources(path)) { AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, new List <string> { path }); } else { entriesAdded.Add(aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup, false, false)); } } else { aaSettings.RemoveAssetEntry(guid); } } } if (create) { aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true); } } }
static void OnPostHeaderGUI(Editor editor) { var aaSettings = AddressableAssetSettingsDefaultObject.Settings; AddressableAssetEntry entry = null; if (editor.targets.Length > 0) { int addressableCount = 0; bool foundValidAsset = false; bool foundAssetGroup = false; foreach (var t in editor.targets) { foundAssetGroup |= t is AddressableAssetGroup; foundAssetGroup |= t is AddressableAssetGroupSchema; if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out var path, out var guid, out var mainAssetType)) { // Is asset if (!BuildUtility.IsEditorAssembly(mainAssetType.Assembly)) { foundValidAsset = true; if (aaSettings != null) { entry = aaSettings.FindAssetEntry(guid); if (entry != null && !entry.IsSubAsset) { addressableCount++; } } } } } if (foundAssetGroup) { GUILayout.BeginHorizontal(); GUILayout.Label("Profile: " + AddressableAssetSettingsDefaultObject.GetSettings(true).profileSettings. GetProfileName(AddressableAssetSettingsDefaultObject.GetSettings(true).activeProfileId)); GUILayout.FlexibleSpace(); if (GUILayout.Button("System Settings", "MiniButton")) { EditorGUIUtility.PingObject(AddressableAssetSettingsDefaultObject.Settings); Selection.activeObject = AddressableAssetSettingsDefaultObject.Settings; } GUILayout.EndHorizontal(); } if (!foundValidAsset) { return; } if (addressableCount == 0) { if (GUILayout.Toggle(false, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } } else if (addressableCount == editor.targets.Length) { GUILayout.BeginHorizontal(); if (!GUILayout.Toggle(true, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(aaSettings, editor.targets, false); GUIUtility.ExitGUI(); } if (editor.targets.Length == 1 && entry != null) { string newAddress = EditorGUILayout.DelayedTextField(entry.address, GUILayout.ExpandWidth(true)); if (newAddress != entry.address) { if (newAddress.Contains("[") && newAddress.Contains("]")) { Debug.LogErrorFormat("Rename of address '{0}' cannot contain '[ ]'.", entry.address); } else { entry.address = newAddress; AddressableAssetUtility.OpenAssetIfUsingVCIntegration(entry.parentGroup, true); } } } GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); if (s_ToggleMixed == null) { s_ToggleMixed = new GUIStyle("ToggleMixed"); } if (GUILayout.Toggle(false, s_AddressableAssetToggleText, s_ToggleMixed, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } EditorGUILayout.LabelField(addressableCount + " out of " + editor.targets.Length + " assets are addressable."); GUILayout.EndHorizontal(); } if (entry != null) { GUILayout.BeginHorizontal(); GUILayout.Label(s_AddressableAllowLocalToggleText, GUILayout.ExpandWidth(false)); entry.allowLocalMode = (EnumLocalResourceMode)EditorGUILayout.EnumPopup(entry.allowLocalMode, GUILayout.ExpandWidth(true)); GUILayout.EndHorizontal(); } } }
static void OnPostHeaderGUI(Editor editor) { var aaSettings = AddressableAssetSettingsDefaultObject.Settings; AddressableAssetEntry entry = null; if (editor.targets.Length > 0) { int addressableCount = 0; bool foundValidAsset = false; bool foundAssetGroup = false; var targetInfos = new List <TargetInfo>(); foreach (var t in editor.targets) { foundAssetGroup |= t is AddressableAssetGroup; foundAssetGroup |= t is AddressableAssetGroupSchema; if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out var path, out var guid, out var mainAssetType)) { // Is asset if (!BuildUtility.IsEditorAssembly(mainAssetType.Assembly)) { foundValidAsset = true; var info = new TargetInfo() { Guid = guid, Path = path, MainAssetType = mainAssetType }; if (aaSettings != null) { entry = aaSettings.FindAssetEntry(guid); if (entry != null && !entry.IsSubAsset) { addressableCount++; info.Entry = entry; } } targetInfos.Add(info); } } } if (foundAssetGroup) { GUILayout.BeginHorizontal(); GUILayout.Label("Profile: " + AddressableAssetSettingsDefaultObject.GetSettings(true).profileSettings. GetProfileName(AddressableAssetSettingsDefaultObject.GetSettings(true).activeProfileId)); GUILayout.FlexibleSpace(); if (GUILayout.Button("System Settings", "MiniButton")) { EditorGUIUtility.PingObject(AddressableAssetSettingsDefaultObject.Settings); Selection.activeObject = AddressableAssetSettingsDefaultObject.Settings; } GUILayout.EndHorizontal(); } if (!foundValidAsset) { return; } // Overrides a DisabledScope in the EditorElement.cs that disables GUI drawn in the header when the asset cannot be edited. bool prevEnabledState = UnityEngine.GUI.enabled; UnityEngine.GUI.enabled = true; if (addressableCount == 0) { if (GUILayout.Toggle(false, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), targetInfos, true); } } else if (addressableCount == editor.targets.Length) { GUILayout.BeginHorizontal(); if (!GUILayout.Toggle(true, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(aaSettings, targetInfos, false); UnityEngine.GUI.enabled = prevEnabledState; GUIUtility.ExitGUI(); } if (editor.targets.Length == 1 && entry != null) { string newAddress = EditorGUILayout.DelayedTextField(entry.address, GUILayout.ExpandWidth(true)); if (newAddress != entry.address) { if (newAddress.Contains("[") && newAddress.Contains("]")) { Debug.LogErrorFormat("Rename of address '{0}' cannot contain '[ ]'.", entry.address); } else { entry.address = newAddress; AddressableAssetUtility.OpenAssetIfUsingVCIntegration(entry.parentGroup, true); } } } else { EditorGUILayout.LabelField(addressableCount + " out of " + editor.targets.Length + " assets are addressable."); } if (GUILayout.Button("Select")) { SelectEntriesInGroupsWindow(targetInfos); } GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); if (s_ToggleMixed == null) { s_ToggleMixed = new GUIStyle("ToggleMixed"); } if (GUILayout.Toggle(false, s_AddressableAssetToggleText, s_ToggleMixed, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), targetInfos, true); } EditorGUILayout.LabelField(addressableCount + " out of " + editor.targets.Length + " assets are addressable."); if (GUILayout.Button("Select")) { SelectEntriesInGroupsWindow(targetInfos); } GUILayout.EndHorizontal(); } UnityEngine.GUI.enabled = prevEnabledState; } }
static void OnPostHeaderGUI(Editor editor) { var aaSettings = AddressableAssetSettingsDefaultObject.Settings; var guid = string.Empty; AddressableAssetEntry entry = null; if (editor.targets.Length > 0) { int addressableCount = 0; bool foundValidAsset = false; foreach (var t in editor.targets) { string path; Type mainAssetType; if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType) && path.ToLower().Contains("assets") && mainAssetType != null && !BuildUtility.IsEditorAssembly(mainAssetType.Assembly)) { foundValidAsset = true; if (aaSettings != null) { entry = aaSettings.FindAssetEntry(guid); if (entry != null && !entry.IsSubAsset) { addressableCount++; } } } } if (!foundValidAsset) { return; } if (addressableCount == 0) { if (GUILayout.Toggle(false, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } } else if (addressableCount == editor.targets.Length) { GUILayout.BeginHorizontal(); if (!GUILayout.Toggle(true, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(aaSettings, editor.targets, false); } if (editor.targets.Length == 1 && entry != null) { entry.address = EditorGUILayout.DelayedTextField(entry.address, GUILayout.ExpandWidth(true)); } GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); if (s_ToggleMixed == null) { s_ToggleMixed = new GUIStyle("ToggleMixed"); } if (GUILayout.Toggle(false, s_AddressableAssetToggleText, s_ToggleMixed, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } EditorGUILayout.LabelField(addressableCount + " out of " + editor.targets.Length + " assets are addressable."); GUILayout.EndHorizontal(); } } }
static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create) { if (create && aaSettings.DefaultGroup.ReadOnly) { Debug.LogError("Current default group is ReadOnly. Cannot add addressable assets to it"); return; } Undo.RecordObject(aaSettings, "AddressableAssetSettings"); var targetInfos = new List <TargetInfo>(); foreach (var t in targets) { if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out var path, out var guid, out var mainAssetType)) { targetInfos.Add(new TargetInfo() { Guid = guid, Path = path, MainAssetType = mainAssetType }); } } if (!create) { List <AddressableAssetEntry> removedEntries = new List <AddressableAssetEntry>(targetInfos.Count); for (int i = 0; i < targetInfos.Count; ++i) { AddressableAssetEntry e = aaSettings.FindAssetEntry(targetInfos[i].Guid); AddressableAssetUtility.OpenAssetIfUsingVCIntegration(e.parentGroup); removedEntries.Add(e); aaSettings.RemoveAssetEntry(removedEntries[i], false); } if (removedEntries.Count > 0) { aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryRemoved, removedEntries, true, false); } } else { AddressableAssetGroup parentGroup = aaSettings.DefaultGroup; var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path)); if (resourceTargets.Any()) { var resourcePaths = resourceTargets.Select(t => t.Path).ToList(); var resourceGuids = resourceTargets.Select(t => t.Guid).ToList(); AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, parentGroup, resourcePaths, resourceGuids); } var otherTargetInfos = targetInfos.Except(resourceTargets); List <string> otherTargetGuids = new List <string>(targetInfos.Count); foreach (var info in otherTargetInfos) { otherTargetGuids.Add(info.Guid); } var entriesCreated = new List <AddressableAssetEntry>(); var entriesMoved = new List <AddressableAssetEntry>(); aaSettings.CreateOrMoveEntries(otherTargetGuids, parentGroup, entriesCreated, entriesMoved, false, false); bool openedInVC = false; if (entriesMoved.Count > 0) { AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup); openedInVC = true; aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesMoved, true, false); } if (entriesCreated.Count > 0) { if (!openedInVC) { AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup); } aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryAdded, entriesCreated, true, false); } } }
static void OnPostHeaderGUI(Editor editor) { var aaSettings = AddressableAssetSettingsDefaultObject.Settings; var guid = string.Empty; AddressableAssetEntry entry = null; if (editor.targets.Length > 0) { int addressableCount = 0; bool foundValidAsset = false; bool foundAssetGroup = false; foreach (var t in editor.targets) { string path; Type mainAssetType; if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType) && path.ToLower().Contains("assets") && mainAssetType != null) { // Is addressable group if (path.ToLower().Contains("addressableassetsdata/assetgroups")) { foundAssetGroup = true; } // Is asset if (!BuildUtility.IsEditorAssembly(mainAssetType.Assembly)) { foundValidAsset = true; if (aaSettings != null) { entry = aaSettings.FindAssetEntry(guid); if (entry != null && !entry.IsSubAsset) { addressableCount++; } } } } } if (foundAssetGroup) { GUILayout.BeginHorizontal(); GUILayout.Label("Profile: " + AddressableAssetSettingsDefaultObject.GetSettings(true).profileSettings. GetProfileName(AddressableAssetSettingsDefaultObject.GetSettings(true).activeProfileId)); GUILayout.FlexibleSpace(); if (GUILayout.Button("System Settings", "MiniButton")) { EditorGUIUtility.PingObject(AddressableAssetSettingsDefaultObject.Settings); Selection.activeObject = AddressableAssetSettingsDefaultObject.Settings; } GUILayout.EndHorizontal(); } if (!foundValidAsset) { return; } if (addressableCount == 0) { if (GUILayout.Toggle(false, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } } else if (addressableCount == editor.targets.Length) { GUILayout.BeginHorizontal(); if (!GUILayout.Toggle(true, s_AddressableAssetToggleText, GUILayout.ExpandWidth(false))) { SetAaEntry(aaSettings, editor.targets, false); } if (editor.targets.Length == 1 && entry != null) { entry.address = EditorGUILayout.DelayedTextField(entry.address, GUILayout.ExpandWidth(true)); } GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); if (s_ToggleMixed == null) { s_ToggleMixed = new GUIStyle("ToggleMixed"); } if (GUILayout.Toggle(false, s_AddressableAssetToggleText, s_ToggleMixed, GUILayout.ExpandWidth(false))) { SetAaEntry(AddressableAssetSettingsDefaultObject.GetSettings(true), editor.targets, true); } EditorGUILayout.LabelField(addressableCount + " out of " + editor.targets.Length + " assets are addressable."); GUILayout.EndHorizontal(); } } }
static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create) { /* * if (create && aaSettings.DefaultGroup.ReadOnly) * { * Debug.LogError("Current default group is ReadOnly. Cannot add addressable assets to it"); * return; * } */ Undo.RecordObject(aaSettings, "AddressableAssetSettings"); string path; var guid = string.Empty; //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel")) { var entriesAdded = new List <AddressableAssetEntry>(); var modifiedGroups = new HashSet <AddressableAssetGroup>(); Type mainAssetType; foreach (var t in targets) { if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType)) { if (create) { var ruleSet = aaSettings.assetRegisterRuleSet; if (!ruleSet.TryResolveRule(path, out var rule)) { rule = new AddressableAssetRegisterRuleSet.Rule { AssetGroup = aaSettings.DefaultGroup } } ; var assetGroup = rule.AssetGroup; if (AddressableAssetUtility.IsInResources(path)) { AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, assetGroup, new List <string> { path }); } else { var e = aaSettings.CreateOrMoveEntry(guid, assetGroup, false, false); if (rule.SimplifyAddress) { e.SetAddress(System.IO.Path.GetFileNameWithoutExtension(e.address), false); } entriesAdded.Add(e); modifiedGroups.Add(e.parentGroup); } } else { aaSettings.RemoveAssetEntry(guid); } } } if (create) { foreach (var g in modifiedGroups) { g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true); } aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false); } } }