forked from NuclearBand/NuclearScriptableObjectDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataNodeCreator.cs
More file actions
69 lines (58 loc) · 2.46 KB
/
DataNodeCreator.cs
File metadata and controls
69 lines (58 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#nullable enable
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using UnityEditor;
using UnityEngine;
namespace NuclearBand.Editor
{
public static class DataNodeCreator
{
public static void ShowDialog<T>(string defaultDestinationPath, Action<T>? onScriptableObjectCreated = null)
where T : ScriptableObject
{
var selector = new ScriptableObjectSelector<T>(defaultDestinationPath, onScriptableObjectCreated);
if (selector.SelectionTree.EnumerateTree().Count() == 1)
{
selector.SelectionTree.EnumerateTree().First().Select();
selector.SelectionTree.Selection.ConfirmSelection();
}
else
{
selector.ShowInPopup(200);
}
}
private class ScriptableObjectSelector<T> : OdinSelector<Type> where T : ScriptableObject
{
private readonly Action<T>? onScriptableObjectCreated;
private readonly string defaultDestinationPath;
public ScriptableObjectSelector(string defaultDestinationPath, Action<T>? onScriptableObjectCreated = null)
{
this.onScriptableObjectCreated = onScriptableObjectCreated;
this.defaultDestinationPath = defaultDestinationPath;
this.SelectionConfirmed += this.Save;
}
protected override void BuildSelectionTree(OdinMenuTree tree)
{
var scriptableObjectTypes = AssemblyUtilities.GetTypes(AssemblyTypeFlags.CustomTypes)
.Where(x => x.IsClass && !x.IsAbstract && x.InheritsFrom(typeof(T)) && typeof(T) != x);
tree.Selection.SupportsMultiSelect = false;
tree.Config.DrawSearchToolbar = true;
tree.AddRange(scriptableObjectTypes, x => x.GetNiceName())
.AddThumbnailIcons();
}
private void Save(IEnumerable<Type> selection)
{
var obj = (ScriptableObject.CreateInstance(selection.FirstOrDefault()) as T)!;
var dest = this.defaultDestinationPath.TrimEnd('/');
AssetDatabase.CreateAsset(obj, AssetDatabase.GenerateUniqueAssetPath(dest + "/" + "New.asset"));
AssetDatabase.Refresh();
onScriptableObjectCreated?.Invoke(obj);
}
}
}
}
#endif