22using System . Reflection ;
33
44using UnityEditor ;
5- using UnityEditor . IMGUI . Controls ;
65using UnityEngine ;
6+ using Object = UnityEngine . Object ;
77
88namespace Toolbox . Editor . Windows
99{
10+ using Toolbox . Editor . Internal ;
11+
1012 public class ScriptableObjectCreationWindow : EditorWindow
1113 {
12- private SearchField searchField ;
14+ private class TypeConstraintScriptableObject : TypeConstraintStandard
15+ {
16+ public TypeConstraintScriptableObject ( ) : base ( typeof ( ScriptableObject ) , TypeSettings . Class , false , false )
17+ { }
18+
19+
20+ public override bool IsSatisfied ( Type type )
21+ {
22+ return Attribute . IsDefined ( type , typeof ( CreateAssetMenuAttribute ) ) && base . IsSatisfied ( type ) ;
23+ }
24+ }
25+
1326
27+ private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintScriptableObject ( ) ;
28+ private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext ( sharedConstraint , TypeGrouping . None , true ) ;
29+ private static readonly TypeField typeField = new TypeField ( sharedConstraint , sharedAppearance ) ;
1430
15- [ MenuItem ( "Assets/Create/ScriptableObject Creation Window" , priority = 5 ) ]
31+ private Type activeType ;
32+ private int instancesCount = 1 ;
33+ private string baseName ;
34+ private Object defaultObject ;
35+
36+ [ MenuItem ( "Assets/Create/Toolbox/ScriptableObject Creation Window" , priority = 5 ) ]
1637 internal static void Initialize ( )
1738 {
1839 var window = GetWindow < ScriptableObjectCreationWindow > ( ) ;
@@ -21,41 +42,65 @@ internal static void Initialize()
2142 }
2243
2344
24- private void OnEnable ( )
25- {
26- searchField = new SearchField ( ) ;
27- }
28-
2945 private void OnGUI ( )
3046 {
31- using ( new EditorGUILayout . HorizontalScope ( ) )
47+ using ( new EditorGUILayout . VerticalScope ( EditorStyles . helpBox ) )
3248 {
33- using ( new EditorGUILayout . VerticalScope ( ) )
34- {
35- DrawSearchPanel ( ) ;
36- }
37-
38- using ( new EditorGUILayout . VerticalScope ( ) )
39- {
40- DrawCreatePanel ( ) ;
41- }
49+ DrawSettingsPanel ( ) ;
4250 }
51+
52+ DrawCreatePanel ( ) ;
4353 }
4454
45- private void DrawSearchPanel ( )
55+
56+ private void DrawSettingsPanel ( )
4657 {
47- var rect = GUILayoutUtility . GetRect ( 100.0f , 16.0f ) ;
48- searchField . OnGUI ( rect , string . Empty ) ;
58+ EditorGUILayout . LabelField ( "Settings" , EditorStyles . boldLabel ) ;
59+ var rect = EditorGUILayout . GetControlRect ( true ) ;
60+ typeField . OnGui ( rect , true , OnTypeSelected , activeType ) ;
61+ if ( activeType == null )
62+ {
63+ return ;
64+ }
65+
66+ instancesCount = EditorGUILayout . IntField ( "Instances To Create" , instancesCount ) ;
67+ instancesCount = Mathf . Min ( instancesCount , 1 ) ;
68+ baseName = EditorGUILayout . TextField ( "Base Name" , baseName ) ;
69+ EditorGUI . BeginChangeCheck ( ) ;
70+ defaultObject = EditorGUILayout . ObjectField ( new GUIContent ( "Default Object" ) , defaultObject , activeType , false ) ;
71+ if ( EditorGUI . EndChangeCheck ( ) )
72+ {
73+ //TODO: validate default Object
74+ }
4975 }
5076
5177 private void DrawCreatePanel ( )
5278 {
5379 if ( GUILayout . Button ( "Create" ) )
5480 {
55- Debug . Log ( GetActiveFolderPath ( ) ) ;
81+ CreateObjects ( ) ;
5682 }
5783 }
5884
85+ private void CreateObjects ( )
86+ {
87+ CreateObjects ( activeType , defaultObject ) ;
88+ }
89+
90+ private void CreateObjects ( Type targetType , Object defaultObject )
91+ {
92+ //TODO: validate default Object
93+ for ( var i = 0 ; i < instancesCount ; i ++ )
94+ {
95+ //TODO: instantiate SOs
96+ }
97+ }
98+
99+ private Object CreateObject ( Type targetType , Object defaultObject )
100+ {
101+ return defaultObject != null ? Instantiate ( defaultObject ) : CreateInstance ( targetType ) ;
102+ }
103+
59104 private static string GetActiveFolderPath ( )
60105 {
61106 Type projectWindowUtilType = typeof ( ProjectWindowUtil ) ;
@@ -64,5 +109,19 @@ private static string GetActiveFolderPath()
64109 string pathToCurrentFolder = obj . ToString ( ) ;
65110 return pathToCurrentFolder ;
66111 }
112+
113+ private void OnTypeSelected ( Type type )
114+ {
115+ activeType = type ;
116+ var attribute = type ? . GetCustomAttribute < CreateAssetMenuAttribute > ( ) ;
117+ if ( attribute != null )
118+ {
119+ baseName = attribute . fileName ;
120+ }
121+ else
122+ {
123+ baseName = string . Empty ;
124+ }
125+ }
67126 }
68127}
0 commit comments