Skip to content

Commit 0811fc9

Browse files
authored
Merge pull request #123 from FishingCactus/fix/sub_class_picker
fix/sub class picker
2 parents d9eef20 + 8403876 commit 0811fc9

6 files changed

Lines changed: 103 additions & 82 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## [2.3.2] - 2025-04-18
4+
### Updated
5+
- Update SubAssetPicker drawer to display mixed values and disable picker when multiple objects with different values are selected
6+
7+
### Fixed
8+
- Fix SubAssetPicker would not work with items of serialized lists and arrays
9+
- Fix SubAssetPicker would not search for derived classes in other assemblies than the base type's assembly
10+
- Fix tried to show the SubAssetPicker context menu as many times as there are derived types
11+
312
## [2.3.1] - 2025-04-04
413
### Fixed
514
- Path used in CreatePrefab is now relative to project folder.

Editor/PropertyDrawer/AttributesDrawer/SubClassPickerAttributeDrawer.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace FishingCactus.CommonCode
8+
{
9+
[CustomPropertyDrawer( typeof( SubClassPicker ) )]
10+
public class SubClassPickerDrawer : PropertyDrawer
11+
{
12+
private const string DefaultDisplay = "Pick a type";
13+
14+
private IEnumerable<Type> GetSubClasses(
15+
Type base_type
16+
)
17+
{
18+
if( ReflectionUtils.IsCollectionType( base_type )
19+
&& !ReflectionUtils.TryGetEnumerableElementType( base_type, out base_type )
20+
)
21+
{
22+
return Enumerable.Empty<Type>();
23+
}
24+
25+
return TypeCache.GetTypesDerivedFrom( base_type )
26+
.Where( type => type.IsClass && !type.IsAbstract );
27+
}
28+
29+
public override float GetPropertyHeight(
30+
SerializedProperty property,
31+
GUIContent label
32+
)
33+
{
34+
return EditorGUI.GetPropertyHeight( property, label );
35+
}
36+
37+
public override void OnGUI(
38+
Rect position,
39+
SerializedProperty property,
40+
GUIContent label
41+
)
42+
{
43+
Type field_type = fieldInfo.FieldType;
44+
45+
string type_name = property.managedReferenceValue?.GetType().Name ?? DefaultDisplay;
46+
47+
Rect drop_down_rect = position;
48+
49+
drop_down_rect.x += EditorGUIUtility.labelWidth + 2;
50+
drop_down_rect.width -= EditorGUIUtility.labelWidth + 2;
51+
drop_down_rect.height = EditorGUIUtility.singleLineHeight;
52+
53+
bool previous_show_mixed_values = EditorGUI.showMixedValue;
54+
EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
55+
56+
using( new EditorGUI.DisabledGroupScope( property.hasMultipleDifferentValues ) )
57+
{
58+
if( EditorGUI.DropdownButton( drop_down_rect, new( type_name ), FocusType.Keyboard ) )
59+
{
60+
GenericMenu menu = new GenericMenu();
61+
menu.AddItem( new GUIContent( "None" ), property.managedReferenceValue == null, () =>
62+
{
63+
property.managedReferenceValue = null;
64+
property.serializedObject.ApplyModifiedProperties();
65+
} );
66+
67+
foreach( Type type in GetSubClasses( field_type ) )
68+
{
69+
menu.AddItem( new GUIContent( type.Name ), type_name == type.Name, () =>
70+
{
71+
property.managedReferenceValue = type.GetConstructor( Type.EmptyTypes ).Invoke( null );
72+
property.serializedObject.ApplyModifiedProperties();
73+
} );
74+
}
75+
76+
menu.ShowAsContext();
77+
}
78+
}
79+
80+
EditorGUI.PropertyField( position, property, label, true );
81+
82+
EditorGUI.showMixedValue = previous_show_mixed_values;
83+
}
84+
}
85+
}

Editor/PropertyDrawer/AttributesDrawer/SubClassPickerAttributeDrawer.cs.meta renamed to Editor/PropertyDrawer/AttributesDrawer/SubClassPickerDrawer.cs.meta

File renamed without changes.

Runtime/Utilities/ReflectionUtils.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ out Type element_type
4141
return false;
4242
}
4343

44+
public static bool IsCollectionType(
45+
Type list_type
46+
)
47+
{
48+
return list_type.IsArray
49+
|| ( list_type.IsGenericType && IsAssignableToGenericType( list_type, typeof( IEnumerable<> ) ) );
50+
}
51+
4452
public static bool IsAssignableToGenericType(
4553
Type type,
4654
Type generic_type

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.fishingcactus.common-code",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"displayName": "FC - Common Code",
55
"description": "Code, Extensions and extra features for Fishing Cactus Games.",
66
"unity": "2020.3",

0 commit comments

Comments
 (0)