Skip to content

Commit

Permalink
Merge pull request #25 from brunomikoski/feature/runtime-changes-to-c…
Browse files Browse the repository at this point in the history
…ollection

fix: runtime collection changes
  • Loading branch information
brunomikoski authored Aug 20, 2020
2 parents ab51393 + 977975f commit edfd051
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 63 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [1.2.2]
## Changed
- Fixed issue with the settings menu been displayed wrong
- Fixed an issue while converting numbers to literal numbers would not deal properly with initial special characters

## Added
- Added system to be able to Add/Remove items from the collection at runtime (Like loading new items from addressables)
- Added runtime / editor time fix to deal with dynamicly changed collections

## [1.2.1]
## Changed
- Fixed issues with indirect references
Expand Down Expand Up @@ -112,6 +121,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Add a setup wizzard for first time settings creation

[1.2.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.2
[1.2.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.1
[1.2.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.0
[1.1.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.9
Expand Down
105 changes: 52 additions & 53 deletions Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,78 +296,77 @@ private void DrawSettings(ScriptableObjectCollection collection)
{
using (new GUILayout.VerticalScope("Box"))
{
showSettings = EditorGUILayout.BeginFoldoutHeaderGroup(showSettings, "Settings", EditorStyles.foldoutHeader);
EditorGUI.indentLevel++;
showSettings = EditorGUILayout.Foldout(showSettings, "Settings", true);
EditorGUI.indentLevel--;

if (showSettings)
{
if (showSettings)
EditorGUI.indentLevel++;

using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUI.indentLevel++;
bool isAutomaticallyLoaded = EditorGUILayout.ToggleLeft("Automatically Loaded",
ScriptableObjectCollectionSettings.Instance.IsCollectionAutomaticallyLoaded(
collection));

using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
if (changeCheck.changed)
{
bool isAutomaticallyLoaded = EditorGUILayout.ToggleLeft("Automatically Loaded",
ScriptableObjectCollectionSettings.Instance.IsCollectionAutomaticallyLoaded(
ScriptableObjectCollectionSettings.Instance.SetCollectionAutomaticallyLoaded(
collection,
isAutomaticallyLoaded);
}
}

using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
GeneratedStaticFileType staticCodeGeneratorType =
(GeneratedStaticFileType) EditorGUILayout.EnumPopup("Static File Generator Type",
ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(
collection));

if (changeCheck.changed)
{
ScriptableObjectCollectionSettings.Instance.SetCollectionAutomaticallyLoaded(
collection,
isAutomaticallyLoaded);
}
if (changeCheck.changed)
{
ScriptableObjectCollectionSettings.Instance.SetStaticFileGeneratorTypeForCollection(
collection,
staticCodeGeneratorType);
}
}

using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
bool overwriteStaticFileLocation = false;
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
overwriteStaticFileLocation = EditorGUILayout.ToggleLeft(
"Overwrite Static File Location",
ScriptableObjectCollectionSettings.Instance.IsOverridingStaticFileLocation(collection));
if (changeCheck.changed)
{
GeneratedStaticFileType staticCodeGeneratorType =
(GeneratedStaticFileType) EditorGUILayout.EnumPopup("Static File Generator Type",
ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(
collection));

if (changeCheck.changed)
{
ScriptableObjectCollectionSettings.Instance.SetStaticFileGeneratorTypeForCollection(
collection,
staticCodeGeneratorType);
}
ScriptableObjectCollectionSettings.Instance.SetOverridingStaticFileLocation(
collection, overwriteStaticFileLocation);
}
}

bool overwriteStaticFileLocation = false;
if (overwriteStaticFileLocation)
{
DefaultAsset targetFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(
ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(
collection));
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
overwriteStaticFileLocation = EditorGUILayout.ToggleLeft(
"Overwrite Static File Location",
ScriptableObjectCollectionSettings.Instance.IsOverridingStaticFileLocation(collection));
if (changeCheck.changed)
{
ScriptableObjectCollectionSettings.Instance.SetOverridingStaticFileLocation(
collection, overwriteStaticFileLocation);
}
}
targetFolder = (DefaultAsset) EditorGUILayout.ObjectField("Target Folder",
targetFolder,
typeof(DefaultAsset), false);

if (overwriteStaticFileLocation)
{
DefaultAsset targetFolder = AssetDatabase.LoadAssetAtPath<DefaultAsset>(
ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(
collection));
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
if (changeCheck.changed)
{
targetFolder = (DefaultAsset) EditorGUILayout.ObjectField("Target Folder",
targetFolder,
typeof(DefaultAsset), false);

if (changeCheck.changed)
{
ScriptableObjectCollectionSettings.Instance.SetStaticFileFolderForCollection(
collection,
AssetDatabase.GetAssetPath(targetFolder));
}
ScriptableObjectCollectionSettings.Instance.SetStaticFileFolderForCollection(
collection,
AssetDatabase.GetAssetPath(targetFolder));
}
}

EditorGUI.indentLevel--;
}

EditorGUILayout.EndFoldoutHeaderGroup();
EditorGUI.indentLevel--;
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions Scripts/Runtime/CollectionsEditorBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if UNITY_EDITOR
using UnityEditor;

namespace BrunoMikoski.ScriptableObjectCollections
{
[InitializeOnLoad]
public static class CollectionsEditorBehaviour
{
static CollectionsEditorBehaviour()
{
EditorApplication.playModeStateChanged -= OnPlayModeChanged;
EditorApplication.playModeStateChanged += OnPlayModeChanged;
}

private static void OnPlayModeChanged(PlayModeStateChange playModeState)
{
if (playModeState == PlayModeStateChange.EnteredPlayMode)
CollectionsRegistry.Instance.PrepareForPlayMode();
else if(playModeState == PlayModeStateChange.ExitingPlayMode)
CollectionsRegistry.Instance.PrepareForEditorMode();
}
}
}
#endif
3 changes: 3 additions & 0 deletions Scripts/Runtime/CollectionsEditorBehaviour.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Scripts/Runtime/CollectionsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public void PostBuildProcess()
{
ReloadCollections();
}

#if UNITY_EDITOR
public void PrepareForPlayMode()
{
for (int i = 0; i < collections.Count; i++)
collections[i].PrepareForPlayMode();
}

public void PrepareForEditorMode()
{
for (int i = 0; i < collections.Count; i++)
collections[i].PrepareForEditorMode();
}

#endif
}
}

3 changes: 2 additions & 1 deletion Scripts/Runtime/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static string Sanitize(this string input)

public static string StartingNumbersToWords(this string input)
{
input = INVALID_CHARS_RGX.Replace(input, "");
StringBuilder targetNumberString = new StringBuilder();
int endIndex = 0;
bool needToConvert = false;
Expand All @@ -50,7 +51,7 @@ public static string StartingNumbersToWords(this string input)
}
else
{
endIndex = i - 1;
endIndex = i;
break;
}
}
Expand Down
54 changes: 46 additions & 8 deletions Scripts/Runtime/ScriptableObjectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public string GUID
}

[SerializeField]
private List<CollectableScriptableObject> items = new List<CollectableScriptableObject>();

private bool isReadyListDirty = true;
private List<CollectableScriptableObject> editorSerializedItems;

[SerializeField]
protected List<CollectableScriptableObject> items = new List<CollectableScriptableObject>();


private IReadOnlyList<CollectableScriptableObject> readOnlyList = new List<CollectableScriptableObject>();
public IReadOnlyList<CollectableScriptableObject> Items
{
Expand All @@ -34,6 +37,8 @@ public IReadOnlyList<CollectableScriptableObject> Items
return readOnlyList;
}
}

protected bool isReadyListDirty = true;

private void SyncGUID()
{
Expand Down Expand Up @@ -111,6 +116,7 @@ public bool Add(CollectableScriptableObject item)
return false;

items.Add(item);

item.SetCollection(this);
ObjectUtility.SetDirty(this);
isReadyListDirty = true;
Expand Down Expand Up @@ -173,6 +179,13 @@ public Type GetGenericEnumType()
return null;
}

public virtual void Sort()
{
items.Sort();
isReadyListDirty = true;
ObjectUtility.SetDirty(this);
}

public void Clear()
{
items.Clear();
Expand Down Expand Up @@ -254,7 +267,11 @@ public void Swap(int targetIndex, int newIndex)

public void ClearBadItems()
{
items = items.Where(o => o != null).Distinct().ToList();
for (int i = items.Count - 1; i >= 0; i--)
{
if (items[i].IsNull() || items[i] == null)
items.RemoveAt(i);
}
ObjectUtility.SetDirty(this);
isReadyListDirty = true;
}
Expand All @@ -276,9 +293,9 @@ public void ValidateGUID()
if (items[i] == items[j])
continue;

if (string.Equals(items[i].GUID, Items[j].GUID, StringComparison.Ordinal))
if (string.Equals(items[i].GUID, items[j].GUID, StringComparison.Ordinal))
{
Items[j].GenerateNewGUID();
items[j].GenerateNewGUID();
Debug.LogWarning($"Found duplicated GUID, please regenerate code of collection {this.name}",
this);
}
Expand Down Expand Up @@ -335,18 +352,39 @@ public bool TryGetCollectableByGUID(string itemGUID, out CollectableScriptableOb
collectableScriptableObject = null;
return false;
}

internal void PrepareForPlayMode()
{
editorSerializedItems = new List<CollectableScriptableObject>(items);
}

internal void PrepareForEditorMode()
{
items = new List<CollectableScriptableObject>(editorSerializedItems);
ObjectUtility.SetDirty(this);
}
}

public class ScriptableObjectCollection<ObjectType> : ScriptableObjectCollection, IList<ObjectType>
where ObjectType : CollectableScriptableObject
{
private IReadOnlyList<ObjectType> readOnlyList = new List<ObjectType>();
public new IReadOnlyList<ObjectType> Items
{
get
{
if (isReadyListDirty)
readOnlyList = items.Cast<ObjectType>().ToList().AsReadOnly();
return readOnlyList;
}
}

public new ObjectType this[int index]
{
get => (ObjectType)base[index];
set => base[index] = value;
}


public ObjectType GetCollectableByGUID(string targetGUID)
{
for (int i = 0; i < Items.Count; i++)
Expand All @@ -358,7 +396,7 @@ public ObjectType GetCollectableByGUID(string targetGUID)

return null;
}

public void Add(ObjectType item)
{
base.Add(item);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.scriptableobjectcollection",
"displayName": "Scriptable Object Collection",
"version": "1.2.1",
"version": "1.2.2",
"unity": "2018.4",
"description": "Scriptable Object Collection",
"keywords": [
Expand Down

0 comments on commit edfd051

Please sign in to comment.