Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 70279eb

Browse files
committed
feat: GetModels(includeSubFolders)
1 parent ae1f451 commit 70279eb

File tree

4 files changed

+66
-95
lines changed

4 files changed

+66
-95
lines changed

Packages/com.nuclearband.sodatabase/Runtime/SODatabase.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#nullable enable
2-
#if UNITY_EDITOR
3-
using UnityEditor;
4-
#endif
52
using System;
63
using System.Collections.Generic;
74
using System.Linq;
@@ -16,20 +13,29 @@ namespace NuclearBand
1613
public static class SODatabase
1714
{
1815
private static FolderHolder root = null!;
19-
20-
public static async Task Init(Action<float>? onProgress, Action? onComplete)
16+
17+
public static async void Init(Action<float>? onProgress, Action? onComplete)
18+
{
19+
await InitAsync(onProgress, onComplete);
20+
}
21+
public static async Task InitAsync(Action<float>? onProgress, Action? onComplete)
2122
{
2223
var loadHandler = Addressables.LoadResourceLocationsAsync(SODatabaseSettings.Label);
2324
#pragma warning disable 4014
2425
Task.Run(async () =>
2526
{
2627
while (!loadHandler.IsDone)
2728
{
28-
onProgress?.Invoke(loadHandler.PercentComplete);
29+
CallAction(() =>
30+
{
31+
onProgress?.Invoke(loadHandler.PercentComplete);
32+
});
2933
await Task.Delay(50);
3034
}
31-
32-
onProgress?.Invoke(loadHandler.PercentComplete);
35+
CallAction(() =>
36+
{
37+
onProgress?.Invoke(loadHandler.PercentComplete);
38+
});
3339
});
3440
#pragma warning restore 4014
3541
var resourceLocations = await loadHandler.Task;
@@ -54,7 +60,13 @@ public static async Task Init(Action<float>? onProgress, Action? onComplete)
5460
dataNodeName = dataNodeName.Substring(0, dataNodeName.IndexOf(".asset", StringComparison.Ordinal));
5561
curFolder.DataNodes.Add(dataNodeName, loadTask.Value.Result);
5662
}
57-
onComplete?.Invoke();
63+
64+
CallAction(onComplete);
65+
}
66+
67+
static async void CallAction(Action? action)
68+
{
69+
action?.Invoke();
5870
}
5971

6072
public static T GetModel<T>(string path) where T : DataNode
@@ -68,14 +80,35 @@ public static T GetModel<T>(string path) where T : DataNode
6880
return ((T) curFolder.DataNodes[dataNodeName])!;
6981
}
7082

71-
public static List<T> GetModels<T>(string path) where T : DataNode
83+
public static List<T> GetModels<T>(string path, bool includeSubFolders = false) where T : DataNode
7284
{
7385
var pathElements = path.Split('/');
7486
var curFolder = root;
75-
for (var i = 0; i < pathElements.Length; i++)
76-
curFolder = curFolder.FolderHolders[pathElements[i]];
87+
if (path != string.Empty)
88+
for (var i = 0; i < pathElements.Length; i++)
89+
{
90+
try
91+
{
92+
curFolder = curFolder.FolderHolders[pathElements[i]];
93+
}
94+
catch (Exception e)
95+
{
96+
97+
}
98+
}
7799

78-
return curFolder.DataNodes.Values.OfType<T>().ToList();
100+
var res = curFolder.DataNodes.Values.OfType<T>().ToList();
101+
if (!includeSubFolders)
102+
return res;
103+
104+
105+
foreach (var folderName in curFolder.FolderHolders.Keys)
106+
{
107+
var newPath = path == string.Empty ? folderName : $"{path}/{folderName}";
108+
res.AddRange(GetModels<T>(newPath, includeSubFolders));
109+
}
110+
111+
return res;
79112
}
80113

81114
public static void Save()
@@ -106,14 +139,15 @@ static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, st
106139
var resAdd = SaveFolderHolder(folderHolderPair.Value, fullPath);
107140
resAdd.ForEach(x => res.Add(x.Key, x.Value));
108141
}
142+
109143
return res;
110144
}
111145

112146
public static void Load()
113147
{
114148
LoadFolderHolder(root, string.Empty);
115149
}
116-
150+
117151
static void LoadFolderHolder(FolderHolder folderHolder, string path)
118152
{
119153
foreach (var dataNodePair in folderHolder.DataNodes)
@@ -136,4 +170,4 @@ static void LoadFolderHolder(FolderHolder folderHolder, string path)
136170
}
137171
}
138172
}
139-
}
173+
}

Packages/manifest.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
{
22
"dependencies": {
3-
"com.unity.2d.animation": "5.0.0",
4-
"com.unity.2d.pixel-perfect": "4.0.0",
5-
"com.unity.2d.psdimporter": "4.0.0",
63
"com.unity.2d.sprite": "1.0.0",
7-
"com.unity.2d.spriteshape": "5.0.0",
8-
"com.unity.2d.tilemap": "1.0.0",
94
"com.unity.addressables": "1.11.2",
105
"com.unity.ide.rider": "2.0.5",
116
"com.unity.ide.visualstudio": "2.0.2",
127
"com.unity.ide.vscode": "1.2.1",
138
"com.unity.nuget.newtonsoft-json": "2.0.0",
149
"com.unity.test-framework": "1.1.16",
1510
"com.unity.textmeshpro": "3.0.1",
16-
"com.unity.timeline": "1.4.1",
11+
"com.unity.timeline": "1.4.2",
1712
"com.unity.ugui": "1.0.0",
1813
"com.unity.modules.ai": "1.0.0",
1914
"com.unity.modules.androidjni": "1.0.0",

Packages/packages-lock.json

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,12 @@
99
"com.unity.nuget.newtonsoft-json": "2.0.0"
1010
}
1111
},
12-
"com.unity.2d.animation": {
13-
"version": "5.0.0",
14-
"depth": 0,
15-
"source": "registry",
16-
"dependencies": {
17-
"com.unity.2d.common": "4.0.0",
18-
"com.unity.mathematics": "1.1.0",
19-
"com.unity.2d.sprite": "1.0.0",
20-
"com.unity.modules.animation": "1.0.0",
21-
"com.unity.modules.uielements": "1.0.0"
22-
},
23-
"url": "https://packages.unity.com"
24-
},
25-
"com.unity.2d.common": {
26-
"version": "4.0.0",
27-
"depth": 1,
28-
"source": "registry",
29-
"dependencies": {
30-
"com.unity.2d.sprite": "1.0.0",
31-
"com.unity.modules.uielements": "1.0.0"
32-
},
33-
"url": "https://packages.unity.com"
34-
},
35-
"com.unity.2d.path": {
36-
"version": "4.0.0",
37-
"depth": 1,
38-
"source": "registry",
39-
"dependencies": {},
40-
"url": "https://packages.unity.com"
41-
},
42-
"com.unity.2d.pixel-perfect": {
43-
"version": "4.0.0",
44-
"depth": 0,
45-
"source": "registry",
46-
"dependencies": {},
47-
"url": "https://packages.unity.com"
48-
},
49-
"com.unity.2d.psdimporter": {
50-
"version": "4.0.0",
51-
"depth": 0,
52-
"source": "registry",
53-
"dependencies": {
54-
"com.unity.2d.common": "4.0.0",
55-
"com.unity.2d.animation": "5.0.0",
56-
"com.unity.2d.sprite": "1.0.0"
57-
},
58-
"url": "https://packages.unity.com"
59-
},
6012
"com.unity.2d.sprite": {
6113
"version": "1.0.0",
6214
"depth": 0,
6315
"source": "builtin",
6416
"dependencies": {}
6517
},
66-
"com.unity.2d.spriteshape": {
67-
"version": "5.0.0",
68-
"depth": 0,
69-
"source": "registry",
70-
"dependencies": {
71-
"com.unity.mathematics": "1.1.0",
72-
"com.unity.2d.common": "4.0.0",
73-
"com.unity.2d.path": "4.0.0",
74-
"com.unity.modules.physics2d": "1.0.0"
75-
},
76-
"url": "https://packages.unity.com"
77-
},
78-
"com.unity.2d.tilemap": {
79-
"version": "1.0.0",
80-
"depth": 0,
81-
"source": "builtin",
82-
"dependencies": {}
83-
},
8418
"com.unity.addressables": {
8519
"version": "1.11.2",
8620
"depth": 0,
@@ -123,13 +57,6 @@
12357
"dependencies": {},
12458
"url": "https://packages.unity.com"
12559
},
126-
"com.unity.mathematics": {
127-
"version": "1.1.0",
128-
"depth": 1,
129-
"source": "registry",
130-
"dependencies": {},
131-
"url": "https://packages.unity.com"
132-
},
13360
"com.unity.nuget.newtonsoft-json": {
13461
"version": "2.0.0",
13562
"depth": 0,
@@ -165,7 +92,7 @@
16592
"url": "https://packages.unity.com"
16693
},
16794
"com.unity.timeline": {
168-
"version": "1.4.1",
95+
"version": "1.4.2",
16996
"depth": 0,
17097
"source": "registry",
17198
"dependencies": {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &1
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 61
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: a287be6c49135cd4f9b2b8666c39d999, type: 3}
13+
m_Name:
14+
m_EditorClassIdentifier:
15+
assetDefaultFramerate: 60

0 commit comments

Comments
 (0)