Skip to content

Commit 31dec26

Browse files
committed
Possibility to filter and create valid ScriptableObjects
1 parent 45d1df2 commit 31dec26

6 files changed

Lines changed: 125 additions & 24 deletions

File tree

Assets/Editor Toolbox/Editor/Windows/ScriptableObjectCreationWindow.cs

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,38 @@
22
using System.Reflection;
33

44
using UnityEditor;
5-
using UnityEditor.IMGUI.Controls;
65
using UnityEngine;
6+
using Object = UnityEngine.Object;
77

88
namespace 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
}

Assets/Examples/Scripts/SampleScriptableObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using UnityEngine;
1+
using UnityEngine;
22

3-
[CreateAssetMenu]
3+
[CreateAssetMenu(fileName = "Sample Scriptable Object")]
44
public class SampleScriptableObject : ScriptableObject
55
{
66
public bool var1;

Assets/New Sample Sample SO.asset

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 15c192e004cb32d4b86a9cea8b49d83b, type: 3}
13+
m_Name: New Sample Sample SO
14+
m_EditorClassIdentifier:
15+
var1: 0
16+
var2: 0

Assets/New Sample Sample SO.asset.meta

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

Assets/SampleSampleSO.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using UnityEngine;
2+
3+
[CreateAssetMenu]
4+
public class SampleSampleSO : SampleScriptableObject
5+
{
6+
7+
}

Assets/SampleSampleSO.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)