-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from FishingCactus/feature/select_implementat…
…ion_utils Feature/select implementation utils
- Loading branch information
Showing
38 changed files
with
1,353 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
182 changes: 182 additions & 0 deletions
182
Editor/ContextMenus/PropertyContextMenus/CreateAssetContextMenu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
using System; | ||
using System.Reflection; | ||
using UnityEditor; | ||
using UnityEditor.IMGUI.Controls; | ||
using UnityEditor.SceneManagement; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace FishingCactus | ||
{ | ||
public sealed class CreateAssetContextMenu : IPropertyContextMenuCallback | ||
{ | ||
private readonly static float MIN_DROPDOWN_WIDTH = 300f; | ||
|
||
private SerializedObject _targetObject = null; | ||
private SerializedProperty _targetProperty = null; | ||
private SelectImplementationDropdown _dropdown = null; | ||
|
||
public void OnPropertyContextMenu( | ||
Rect context_menu_rect, | ||
GenericMenu menu, | ||
SerializedProperty property | ||
) | ||
{ | ||
if( property.propertyType != SerializedPropertyType.ObjectReference ) | ||
{ | ||
return; | ||
} | ||
|
||
Type property_type = InspectorUtilities.GetPropertyType( property ); | ||
Type scriptable_object_type = typeof( ScriptableObject ); | ||
|
||
if( !scriptable_object_type.IsAssignableFrom( property_type ) ) | ||
{ | ||
return; | ||
} | ||
|
||
context_menu_rect.size = new Vector2( Mathf.Max( context_menu_rect.size.x, MIN_DROPDOWN_WIDTH ), context_menu_rect.size.y ); | ||
_targetObject = property.serializedObject; | ||
_targetProperty = property; | ||
|
||
menu.AddItem( new GUIContent( "Create asset" ), on: false, () => | ||
{ | ||
if( _dropdown != null ) | ||
{ | ||
_dropdown.OnImplementationSelected -= Dropdown_OnImplementationSelected; | ||
} | ||
var implementations = ReflectionUtilities.GetImplementations( property_type, IsValidImplementation ); | ||
_dropdown = new SelectImplementationDropdown( new AdvancedDropdownState(), property_type, IsValidImplementation ); | ||
_dropdown.Show( context_menu_rect ); | ||
_dropdown.OnImplementationSelected += Dropdown_OnImplementationSelected; | ||
} ); | ||
} | ||
|
||
private void Dropdown_OnImplementationSelected( | ||
Type type | ||
) | ||
{ | ||
CreateAssetOfType( _targetObject, _targetProperty, type ); | ||
_dropdown.OnImplementationSelected -= Dropdown_OnImplementationSelected; | ||
} | ||
|
||
private static bool IsValidImplementation( | ||
Type type | ||
) | ||
{ | ||
return !type.IsInterface | ||
&& !type.IsAbstract | ||
&& !type.ContainsGenericParameters; | ||
} | ||
|
||
private static void GetCreateAssetMenuAttributeInfo( | ||
Type asset_type, | ||
out string file_name | ||
) | ||
{ | ||
file_name = string.Empty; | ||
|
||
foreach( CustomAttributeData attribute in asset_type.CustomAttributes ) | ||
{ | ||
if( attribute.AttributeType != typeof( CreateAssetMenuAttribute ) ) | ||
{ | ||
continue; | ||
} | ||
|
||
for( int argument_index = 0; argument_index < attribute.NamedArguments.Count; argument_index++ ) | ||
{ | ||
var argument_info = attribute.NamedArguments[ argument_index ]; | ||
|
||
if( argument_info.MemberName == "fileName" ) | ||
{ | ||
file_name = ( string )argument_info.TypedValue.Value; | ||
} | ||
} | ||
|
||
if( !string.IsNullOrWhiteSpace( file_name ) ) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private static void CreateAssetOfType( | ||
SerializedObject serialized_object, | ||
SerializedProperty property, | ||
Type asset_type | ||
) | ||
{ | ||
Type project_window_util_type = typeof( ProjectWindowUtil ); | ||
MethodInfo get_active_folder_path = project_window_util_type.GetMethod( "TryGetActiveFolderPath", BindingFlags.Static | BindingFlags.NonPublic ); | ||
object[] parameters = new object[ 1 ]; | ||
bool has_project_window = ( bool )get_active_folder_path.Invoke( obj: null, parameters ); | ||
|
||
if( !has_project_window ) | ||
{ | ||
Debug.LogError( "No project window is currently opened" ); | ||
|
||
return; | ||
} | ||
|
||
ScriptableObject asset = ScriptableObject.CreateInstance( asset_type ); | ||
string asset_path = ( string )parameters[ 0 ]; | ||
// TODO: use custom project settings for asset name convention (eg "SO_{name}") | ||
string asset_name = $"SO_{asset_type.Name}"; | ||
|
||
GetCreateAssetMenuAttributeInfo( asset_type, out string file_name ); | ||
|
||
if( !string.IsNullOrWhiteSpace( file_name ) ) | ||
{ | ||
asset_name = file_name; | ||
} | ||
|
||
asset_path += $"/{asset_name}"; | ||
AddIndexInName( ref asset_path ); | ||
asset_path += ".asset"; | ||
AssetDatabase.CreateAsset( asset, asset_path ); | ||
|
||
serialized_object.Update(); | ||
property.objectReferenceValue = asset; | ||
serialized_object.ApplyModifiedProperties(); | ||
|
||
if( serialized_object.targetObject is Component target_component ) | ||
{ | ||
Scene scene = target_component.gameObject.scene; | ||
EditorSceneManager.MarkSceneDirty( scene ); | ||
EditorSceneManager.SaveScene( scene ); | ||
} | ||
|
||
AssetDatabase.SaveAssets(); | ||
} | ||
|
||
private static void AddIndexInName( | ||
ref string asset_path | ||
) | ||
{ | ||
string new_asset_path = asset_path; | ||
int index = 0; | ||
|
||
while( !string.IsNullOrEmpty( AssetDatabase.AssetPathToGUID( $"{new_asset_path}.asset", AssetPathToGUIDOptions.OnlyExistingAssets ) ) ) | ||
{ | ||
new_asset_path = GetFormatedAssetName( asset_path, index ); | ||
index++; | ||
} | ||
|
||
asset_path = new_asset_path; | ||
} | ||
|
||
private static string GetFormatedAssetName( | ||
string asset_path, | ||
int index | ||
) | ||
{ | ||
return EditorSettings.gameObjectNamingScheme switch | ||
{ | ||
EditorSettings.NamingScheme.SpaceParenthesis => $"{asset_path} ({index})", | ||
EditorSettings.NamingScheme.Dot => $"{asset_path}.{index}", | ||
_ => $"{asset_path}_{index}", | ||
}; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Editor/ContextMenus/PropertyContextMenus/CreateAssetContextMenu.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
Editor/ContextMenus/PropertyContextMenus/IPropertyContextMenuCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace FishingCactus | ||
{ | ||
public interface IPropertyContextMenuCallback | ||
{ | ||
public void OnPropertyContextMenu( Rect context_menu_rect, GenericMenu menu, SerializedProperty property ); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Editor/ContextMenus/PropertyContextMenus/IPropertyContextMenuCallback.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.