Skip to content

Commit c73f4e2

Browse files
committed
fix: more tweaks
1 parent 630f550 commit c73f4e2

File tree

6 files changed

+133
-4
lines changed

6 files changed

+133
-4
lines changed

CHANGELOG.MD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [2.3.1]
10+
### Added
11+
- Added Addressables support again, now you if addressables is available on your project and the collection is set to non auto loaded, and the collection is set to use addressables, will write some helper code on the static file to load the collectiom from the addressables.
12+
- Added a Addressables Sample project as well
13+
- Added Reset Settings context menu option
14+
- Added the Write Addressable Loading Methods toggle for the collection~~~~
15+
16+
### Changed
17+
- Updated the visuals of the CollectionEditor again, make it a bit tighter and more readable
18+
- Refactored the settings again (sorry, some settings will be lost again) to make the system more extensible and easier to maintain
19+
20+
921
## [2.3.0]
1022
### Changed
1123
- Refactored CollectionCustomEditor to use UI Elements, getting rid of multiple hacks to make it work with the IMGUI and Reorderable list
@@ -541,6 +553,7 @@ public bool IsValidConsumable(Consumable consumable)
541553
### Added
542554
- First initial working version
543555

556+
[2.3.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.1
544557
[2.3.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.0
545558
[2.2.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.2.4
546559
[2.2.3]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.2.3

Editor/UXML/CollectionCustomEditorTreeAsset.uxml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
</ui:VisualElement>
1616
<ui:VisualElement name="BottonGroup">
1717
<ui:Instance template="GroupBoxVisualElement" name="GroupBoxVisualElement">
18-
<ui:VisualElement style="flex-grow: 1; border-top-left-radius: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; border-bottom-left-radius: 0; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">
18+
<ui:VisualElement name="advanced-options-visual-element" style="flex-grow: 1; border-top-left-radius: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; border-bottom-left-radius: 0; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px;">
1919
<ui:Toggle label="Automaticaly Loaded" name="automatic-loaded-toggle" binding-path="automaticallyLoaded" tooltip="When enabled, this is automatically loaded by the system when the registry loads from Resources/Addressables. If disabled, it is removed prior to playtime and the reference is deleted from the registry at build time." class="unity-base-field__aligned" style="flex-grow: 0; -unity-font-style: normal;" />
2020
<ui:Toggle label="Write as Partial Class" name="write-partial-class-toggle" tooltip="If enabled, attempts to declare the new static class as partial, allowing direct usage from the same type. This option is disabled when the class or generated file resides in a different assembly." class="unity-base-field__aligned" />
2121
<ui:Toggle label="Use Base Class for Items" name="base-class-for-items-toggle" tooltip="If enabled, this will enforce the use of the base type in the static file declaration, regardless of any objects extending it to a child type." class="unity-base-field__aligned" />
2222
<ui:Toggle label="Enforce Indirect Access" name="enforce-indirect-access" tooltip="Useful if the collection is not auto-loaded, loaded through addressables, or if better memory management is desired. This setting ensures that there are no direct references to the scriptable object." class="unity-base-field__aligned" />
2323
<uie:ObjectField label="Generated Scripts Folde" type="UnityEditor.DefaultAsset, UnityEditor.CoreModule" allow-scene-objects="false" name="generated-scripts-parent-folder" tooltip="Specifies the target folder for generating the static file access. If left null, the file will be generated in the same location as the original file." class="unity-base-field__aligned" />
2424
<ui:TextField picking-mode="Ignore" label="Static Filename" value="filler text" name="static-filename-textfield" is-delayed="true" class="unity-base-field__aligned" />
2525
<ui:TextField picking-mode="Ignore" label="Namespace" value="filler text" name="namespace-textfield" is-delayed="true" class="unity-base-field__aligned" />
26+
<ui:Toggle label="Generate Addressables Loading Methods" name="write-addressables-load-toggle" class="unity-base-field__aligned" />
2627
</ui:VisualElement>
2728
<ui:VisualElement style="flex-grow: 1; flex-direction: row; height: 26px; border-top-left-radius: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; border-bottom-left-radius: 0;">
2829
<ui:Button text="Generate Static File" parse-escape-sequences="true" display-tooltip-when-elided="true" tooltip="Uses Code Generation to generate the Static File to access collection items by code." name="generate-static-file-button" style="flex-grow: 1; padding-right: 0; padding-left: -2px; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-bottom: 0; border-bottom-right-radius: 0; border-bottom-left-radius: 4px; border-top-left-radius: 0; border-top-right-radius: 0;" />

Scripts/Editor/Core/CodeGenerationUtility.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,12 @@ private static void WriteNonAutomaticallyLoadedCollectionItems(ScriptableObjectC
574574
AppendLine(writer, indentation, "}");
575575

576576
AppendLine(writer, indentation);
577-
578-
577+
578+
579+
if (!SOCSettings.Instance.GetWriteAddressableLoadingMethods(collection))
580+
{
581+
return;
582+
}
579583
#if ADDRESSABLES_ENABLED
580584
string assetPath = AssetDatabase.GetAssetPath(collection);
581585
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;

Scripts/Editor/Core/CollectionSettings.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public class CollectionSettings
1515
public bool UseBaseClassForItems;
1616
public bool EnforceIndirectAccess;
1717

18+
private bool writeAddressableLoadingMethods;
19+
public bool WriteAddressableLoadingMethods => !CollectionsRegistry.Instance.GetCollectionByGUID(Guid).AutomaticallyLoaded && writeAddressableLoadingMethods;
20+
21+
1822
public CollectionSettings(ScriptableObjectCollection targetCollection)
1923
{
2024
Guid = targetCollection.GUID;
@@ -46,5 +50,10 @@ public CollectionSettings(ScriptableObjectCollection targetCollection)
4650
UseBaseClassForItems = false;
4751
EnforceIndirectAccess = false;
4852
}
53+
54+
public void SetWriteAddressableLoadingMethods(bool evtNewValue)
55+
{
56+
writeAddressableLoadingMethods = evtNewValue;
57+
}
4958
}
5059
}

Scripts/Editor/Core/SOCSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,5 +303,17 @@ public void ResetSettings(ScriptableObjectCollection collection)
303303
return;
304304
}
305305
}
306+
307+
public void SetWriteAddressableLoadingMethods(ScriptableObjectCollection collection, bool evtNewValue)
308+
{
309+
CollectionSettings settings = GetOrCreateCollectionSettings(collection);
310+
settings.SetWriteAddressableLoadingMethods(evtNewValue);
311+
Save();
312+
}
313+
314+
public bool GetWriteAddressableLoadingMethods(ScriptableObjectCollection collection)
315+
{
316+
return GetOrCreateCollectionSettings(collection).WriteAddressableLoadingMethods;
317+
}
306318
}
307319
}

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using System.Text.RegularExpressions;
56
using JetBrains.Annotations;
67
using UnityEditor;
8+
#if ADDRESSABLES_ENABLED
9+
using UnityEditor.AddressableAssets;
10+
using UnityEditor.AddressableAssets.Settings;
11+
#endif
712
using UnityEditor.Compilation;
813
using UnityEditor.UIElements;
914
using UnityEngine;
@@ -43,6 +48,9 @@ public class CollectionCustomEditor : Editor
4348
private Button expandShrinkButton;
4449

4550
private List<ScriptableObject> filteredItems = new();
51+
52+
private HelpBox helpbox;
53+
private Toggle writeAddressablesToggle;
4654

4755
protected virtual bool CanBeReorderable
4856
{
@@ -158,7 +166,12 @@ public override VisualElement CreateInspectorGUI()
158166
generateItemsButton.RegisterCallback<MouseUpEvent>(OnClickGenerateItems);
159167
generateItemsButton.style.display = generatorType != null ? DisplayStyle.Flex : DisplayStyle.None;
160168

161-
169+
Toggle automaticLoadToggle = root.Q<Toggle>("automatic-loaded-toggle");
170+
automaticLoadToggle.RegisterValueChangedCallback(evt =>
171+
{
172+
UpdateHelpBox();
173+
});
174+
162175
Toggle writeAsPartialClass = root.Q<Toggle>("write-partial-class-toggle");
163176
writeAsPartialClass.value = SOCSettings.Instance.GetWriteAsPartialClass(collection);
164177
writeAsPartialClass.SetEnabled(CodeGenerationUtility.CheckIfCanBePartial(collection));
@@ -198,6 +211,7 @@ public override VisualElement CreateInspectorGUI()
198211
enforceIndirectAccessToggle.RegisterValueChangedCallback(evt =>
199212
{
200213
SOCSettings.Instance.SetEnforceIndirectAccess(collection, evt.newValue);
214+
UpdateHelpBox();
201215
});
202216

203217

@@ -210,7 +224,27 @@ public override VisualElement CreateInspectorGUI()
210224
InspectorElement.FillDefaultInspector(imguiContainer, serializedObject, this);
211225

212226

227+
VisualElement advancedOptionsVisualElement = root.Q<VisualElement>("advanced-options-visual-element");
228+
helpbox = new HelpBox
229+
{
230+
style =
231+
{
232+
display = DisplayStyle.None
233+
}
234+
};
235+
236+
advancedOptionsVisualElement.Add(helpbox);
213237

238+
writeAddressablesToggle = root.Q<Toggle>("write-addressables-load-toggle");
239+
writeAddressablesToggle.style.display = IsAddressableAsset(collection) && !automaticLoadToggle.value ? DisplayStyle.Flex : DisplayStyle.None;
240+
writeAddressablesToggle.SetValueWithoutNotify(SOCSettings.Instance.GetWriteAddressableLoadingMethods(collection));
241+
writeAddressablesToggle.RegisterValueChangedCallback(evt =>
242+
{
243+
SOCSettings.Instance.SetWriteAddressableLoadingMethods(collection, evt.newValue);
244+
UpdateHelpBox();
245+
});
246+
247+
214248
expandShrinkButton = root.Q<Button>("expand-button");
215249

216250
expandShrinkButton.clickable.activators.Clear();
@@ -219,6 +253,8 @@ public override VisualElement CreateInspectorGUI()
219253
ToolbarSearchField toolbarSearchField = root.Q<ToolbarSearchField>();
220254
toolbarSearchField.RegisterValueChangedCallback(OnSearchInputChanged);
221255

256+
UpdateHelpBox();
257+
222258
return root;
223259
}
224260

@@ -469,6 +505,60 @@ private void OnSearchValueChanged(string targetText)
469505

470506
collectionItemListView.RefreshItems();
471507
}
508+
509+
510+
private void UpdateHelpBox()
511+
{
512+
bool isAddressableAsset = IsAddressableAsset(collection);
513+
514+
HelpBoxMessageType helpBoxMessageType = HelpBoxMessageType.Warning;
515+
516+
StringBuilder finalMessage = new StringBuilder();
517+
518+
if (isAddressableAsset && !collection.AutomaticallyLoaded && !SOCSettings.Instance.GetWriteAddressableLoadingMethods(collection))
519+
{
520+
finalMessage.AppendLine("You can use the Write Addressable Loading Methods to generate the loading methods for this collection.");
521+
helpBoxMessageType = HelpBoxMessageType.Info;
522+
}
523+
524+
if (isAddressableAsset && collection.AutomaticallyLoaded)
525+
{
526+
finalMessage.AppendLine(
527+
"This collection is set to be automatically loaded but it's also an Addressables asset, Maybe this should be set to be load manually ");
528+
529+
helpBoxMessageType = HelpBoxMessageType.Warning;
530+
}
531+
532+
if (isAddressableAsset && !collection.AutomaticallyLoaded && !SOCSettings.Instance.GetEnforceIndirectAccess(collection))
533+
{
534+
finalMessage.AppendLine("This collection is an not automatically loaded Addressables asset, you should consider enforcing indirect access to avoid loading all the items at once.");
535+
536+
helpBoxMessageType = HelpBoxMessageType.Warning;
537+
}
538+
539+
if (finalMessage.Length > 0)
540+
{
541+
helpbox.style.display = DisplayStyle.Flex;
542+
helpbox.messageType = helpBoxMessageType;
543+
helpbox.text = finalMessage.ToString();
544+
}
545+
else
546+
{
547+
helpbox.style.display = DisplayStyle.None;
548+
}
549+
}
550+
551+
private bool IsAddressableAsset(ScriptableObject target)
552+
{
553+
#if ADDRESSABLES_ENABLED
554+
string assetPath = AssetDatabase.GetAssetPath(target);
555+
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
556+
AddressableAssetEntry entry = settings.FindAssetEntry(AssetDatabase.AssetPathToGUID(assetPath));
557+
return entry != null;
558+
#else
559+
return false;
560+
#endif
561+
}
472562

473563
private ScriptableObject AddNewItemOfType(Type targetType, bool autoFocusForRename = true)
474564
{

0 commit comments

Comments
 (0)