Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [2.3.2] - 2025-04-18
### Updated
- Update SubAssetPicker drawer to display mixed values and disable picker when multiple objects with different values are selected

### Fixed
- Fix SubAssetPicker would not work with items of serialized lists and arrays
- Fix SubAssetPicker would not search for derived classes in other assemblies than the base type's assembly
- Fix tried to show the SubAssetPicker context menu as many times as there are derived types

## [2.3.1] - 2025-04-04
### Fixed
- Path used in CreatePrefab is now relative to project folder.
Expand Down

This file was deleted.

85 changes: 85 additions & 0 deletions Editor/PropertyDrawer/AttributesDrawer/SubClassPickerDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace FishingCactus.CommonCode
{
[CustomPropertyDrawer( typeof( SubClassPicker ) )]
public class SubClassPickerDrawer : PropertyDrawer
{
private const string DefaultDisplay = "Pick a type";

private IEnumerable<Type> GetSubClasses(
Type base_type
)
{
if( ReflectionUtils.IsCollectionType( base_type )
&& !ReflectionUtils.TryGetEnumerableElementType( base_type, out base_type )
)
{
return Enumerable.Empty<Type>();
}

return TypeCache.GetTypesDerivedFrom( base_type )
.Where( type => type.IsClass && !type.IsAbstract );
}

public override float GetPropertyHeight(
SerializedProperty property,
GUIContent label
)
{
return EditorGUI.GetPropertyHeight( property, label );
}

public override void OnGUI(
Rect position,
SerializedProperty property,
GUIContent label
)
{
Type field_type = fieldInfo.FieldType;

string type_name = property.managedReferenceValue?.GetType().Name ?? DefaultDisplay;

Rect drop_down_rect = position;

drop_down_rect.x += EditorGUIUtility.labelWidth + 2;
drop_down_rect.width -= EditorGUIUtility.labelWidth + 2;
drop_down_rect.height = EditorGUIUtility.singleLineHeight;

bool previous_show_mixed_values = EditorGUI.showMixedValue;
EditorGUI.showMixedValue = property.hasMultipleDifferentValues;

using( new EditorGUI.DisabledGroupScope( property.hasMultipleDifferentValues ) )
{
if( EditorGUI.DropdownButton( drop_down_rect, new( type_name ), FocusType.Keyboard ) )
{
GenericMenu menu = new GenericMenu();
menu.AddItem( new GUIContent( "None" ), property.managedReferenceValue == null, () =>
{
property.managedReferenceValue = null;
property.serializedObject.ApplyModifiedProperties();
} );

foreach( Type type in GetSubClasses( field_type ) )
{
menu.AddItem( new GUIContent( type.Name ), type_name == type.Name, () =>
{
property.managedReferenceValue = type.GetConstructor( Type.EmptyTypes ).Invoke( null );
property.serializedObject.ApplyModifiedProperties();
} );
}

menu.ShowAsContext();
}
}

EditorGUI.PropertyField( position, property, label, true );

EditorGUI.showMixedValue = previous_show_mixed_values;
}
}
}
8 changes: 8 additions & 0 deletions Runtime/Utilities/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ out Type element_type
return false;
}

public static bool IsCollectionType(
Type list_type
)
{
return list_type.IsArray
|| ( list_type.IsGenericType && IsAssignableToGenericType( list_type, typeof( IEnumerable<> ) ) );
}

public static bool IsAssignableToGenericType(
Type type,
Type generic_type
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.fishingcactus.common-code",
"version": "2.3.1",
"version": "2.3.2",
"displayName": "FC - Common Code",
"description": "Code, Extensions and extra features for Fishing Cactus Games.",
"unity": "2020.3",
Expand Down