Skip to content

Commit dc993ff

Browse files
authored
Merge pull request #125 from brunomikoski/feature/new-tweaks
Feature/new tweaks
2 parents 3b448af + 989b9c0 commit dc993ff

File tree

60 files changed

+679
-173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+679
-173
lines changed

CHANGELOG.MD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [2.0.8]
10+
### Changed
811
- Fixed a small typo in the package description.
12+
- Since [2.0.3] the collection now uses the Editor for drawing the items on the collection, but this can cause some issues depending of how customized it is inside a ReorderableList, so you now can choose the item to use it or not by toggling the `Use Custom Editor` inside the advanced settings
13+
- Upgraded the `CollectionCustomEditor` to use `BaseEditor`
14+
- Updated `CollectionItemPicker<>` to use `IndirectReference<>` to store the items, since also contains the `LongGuid` reference to the Collection, allowing to work with multiple collections of the same time _(It should automatically upgrade to the new version automatically)_
15+
- Added ability to compare `CollectionItemPicker<>` to a `IList<>` of the same type
16+
- Other small fixes and improvements
17+
918

1019
## [2.0.7]
1120
### Changed
@@ -459,6 +468,7 @@ public bool IsValidConsumable(Consumable consumable)
459468
### Added
460469
- First initial working version
461470

471+
[2.0.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.8
462472
[2.0.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.7
463473
[2.0.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.6
464474
[2.0.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.5

Scripts/Editor/Core/SOCSettings.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ public static SOCSettings Instance
6565
[SerializeField]
6666
private string generatedScriptsDefaultFilePath = @"Assets\Generated\Scripts";
6767
public string GeneratedScriptsDefaultFilePath => generatedScriptsDefaultFilePath;
68-
6968

69+
[SerializeField]
70+
private List<LongGuid> useCustomEditorDrawer = new List<LongGuid>();
71+
72+
7073
private static readonly GUIContent namespacePrefixGUIContent = new GUIContent(
7174
"Prefix",
7275
"When using the Create New Collection wizard," +
@@ -156,12 +159,37 @@ public void SetGeneratedScriptsDefaultFilePath(string assetPath)
156159
generatedScriptsDefaultFilePath = assetPath;
157160
Save();
158161
}
159-
162+
160163
public void Save()
161164
{
162165
string json = EditorJsonUtility.ToJson(this, prettyPrint: true);
163166
File.WriteAllText(STORAGE_PATH, json);
164167
}
165168

169+
170+
public bool ShouldDrawUsingCustomEditor(ScriptableObjectCollection collection)
171+
{
172+
return useCustomEditorDrawer.Contains(collection.GUID);
173+
}
174+
175+
public void SetUseCustomEditor(ScriptableObjectCollection collection, bool useCustomEditor)
176+
{
177+
if (useCustomEditor)
178+
{
179+
if (!useCustomEditorDrawer.Contains(collection.GUID))
180+
{
181+
useCustomEditorDrawer.Add(collection.GUID);
182+
Save();
183+
}
184+
}
185+
else
186+
{
187+
if (useCustomEditorDrawer.Contains(collection.GUID))
188+
{
189+
useCustomEditorDrawer.Remove(collection.GUID);
190+
Save();
191+
}
192+
}
193+
}
166194
}
167195
}

Scripts/Editor/Core/Utility.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using UnityEditor;
5+
6+
namespace BrunoMikoski.ScriptableObjectCollections
7+
{
8+
public abstract class BaseEditor<T> : Editor where T : class
9+
{
10+
protected T Target => target as T;
11+
12+
/// <summary>
13+
/// Return the Serialized property for a field, and exclude it from being automatically
14+
/// displayed in the inspector. Call this when you need to provide a custom UX for a field.
15+
/// </summary>
16+
/// <typeparam name="TValue">Magic experssion code</typeparam>
17+
/// <param name="expr">Call format is FindAndExcludeProperty(x => x.myField)</param>
18+
/// <returns>The serialized property for the field</returns>
19+
protected SerializedProperty FindAndExcludeProperty<TValue>(Expression<Func<T, TValue>> expr)
20+
{
21+
SerializedProperty p = FindProperty(expr);
22+
ExcludeProperty(p.name);
23+
return p;
24+
}
25+
26+
/// <summary>
27+
/// Return the Serialized property for a field.
28+
/// </summary>
29+
/// <typeparam name="TValue">Magic experssion code</typeparam>
30+
/// <param name="expr">Call format is FindProperty(x => x.myField)</param>
31+
/// <returns>The serialized property for the field</returns>
32+
protected SerializedProperty FindProperty<TValue>(Expression<Func<T, TValue>> expr)
33+
{
34+
return serializedObject.FindProperty(FieldPath(expr));
35+
}
36+
37+
/// <summary>
38+
/// Magic code to get the string name of a field. Will not build if the field name changes.
39+
/// </summary>
40+
/// <typeparam name="TValue">Magic experssion code</typeparam>
41+
/// <param name="expr">Call format is FieldPath(x => x.myField)</param>
42+
/// <returns>The string name of the field</returns>
43+
protected string FieldPath<TValue>(Expression<Func<T, TValue>> expr)
44+
{
45+
return ReflectionUtility.GetFieldPath(expr);
46+
}
47+
48+
/// <summary>Obsolete, do not use. Use the overload, which is more performant</summary>
49+
/// <returns>List of property names to exclude</returns>
50+
protected virtual List<string> GetExcludedPropertiesInInspector() { return mExcluded; }
51+
52+
/// <summary>Get the property names to exclude in the inspector.</summary>
53+
/// <param name="excluded">Add the names to this list</param>
54+
protected virtual void GetExcludedPropertiesInInspector(List<string> excluded)
55+
{
56+
excluded.Add("m_Script");
57+
}
58+
59+
/// <summary>
60+
/// Exclude a property from automatic inclusion in the inspector
61+
/// when DrawRemainingPropertiesInInspector() is called
62+
/// </summary>
63+
/// <param name="propertyName">The property to exclude</param>
64+
protected void ExcludeProperty(string propertyName)
65+
{
66+
mExcluded.Add(propertyName);
67+
}
68+
69+
/// <summary>Check whenther a property is in the excluded list</summary>
70+
/// <param name="propertyName">The property to check</param>
71+
/// <returns>True if property is excluded from automatic inclusion in the inspector
72+
/// when DrawRemainingPropertiesInInspector() is called</returns>
73+
protected bool IsPropertyExcluded(string propertyName)
74+
{
75+
return mExcluded.Contains(propertyName);
76+
}
77+
78+
/// <summary>
79+
/// Draw the inspector
80+
/// </summary>
81+
public override void OnInspectorGUI()
82+
{
83+
BeginInspector();
84+
DrawRemainingPropertiesInInspector();
85+
}
86+
87+
List<string> mExcluded = new List<string>();
88+
89+
/// <summary>
90+
/// Clients should call this at the start of OnInspectorGUI.
91+
/// Updates the serialized object and Sets up for excluded properties.
92+
/// </summary>
93+
protected virtual void BeginInspector()
94+
{
95+
serializedObject.Update();
96+
mExcluded.Clear();
97+
GetExcludedPropertiesInInspector(mExcluded);
98+
}
99+
100+
/// <summary>
101+
/// Draw a property in the inspector, if it is not excluded.
102+
/// Property is marked as drawn, so will not be drawn again
103+
/// by DrawRemainingPropertiesInInspector()
104+
/// </summary>
105+
/// <param name="p">The property to draw</param>
106+
protected virtual void DrawPropertyInInspector(SerializedProperty p)
107+
{
108+
if (!IsPropertyExcluded(p.name))
109+
{
110+
EditorGUI.BeginChangeCheck();
111+
EditorGUILayout.PropertyField(p);
112+
if (EditorGUI.EndChangeCheck())
113+
serializedObject.ApplyModifiedProperties();
114+
ExcludeProperty(p.name);
115+
}
116+
}
117+
118+
/// <summary>
119+
/// Draw all remaining unexcluded undrawn properties in the inspector.
120+
/// </summary>
121+
protected void DrawRemainingPropertiesInInspector()
122+
{
123+
EditorGUI.BeginChangeCheck();
124+
DrawPropertiesExcluding(serializedObject, mExcluded.ToArray());
125+
if (EditorGUI.EndChangeCheck())
126+
serializedObject.ApplyModifiedProperties();
127+
}
128+
}
129+
}

Scripts/Editor/CustomEditors/BaseEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)