108 lines
3.0 KiB
C#
108 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System;
|
|
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// cette classe sert à réunir les prefabs et les scriptable objects importants. <br/>
|
|
/// Elle permet d'éviter de devoir mettre tous nos prefabs et scriptable objects dans le dossier Resources <br/>
|
|
/// et permet d'accéder aux éléments qu'on cherche par leur nom directement
|
|
/// </summary>
|
|
public class Database : SingletonBehaviour<Database>
|
|
{
|
|
#if UNITY_EDITOR
|
|
[Header("Editor section")]
|
|
[SerializeField]
|
|
private List<DefaultAsset> _folders;
|
|
|
|
public List<DefaultAsset> Folders => _folders;
|
|
#endif
|
|
|
|
public const string TYPE = nameof(TYPE);
|
|
|
|
public void FetchDatabase()
|
|
{
|
|
foreach (var folder in Folders)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(folder);
|
|
foreach (var file in GetAllPaths(path))
|
|
{
|
|
var scriptableObject = AssetDatabase.LoadAssetAtPath<ScriptableObject>(file);
|
|
if (scriptableObject && !ScriptableObjects.Contains(scriptableObject))
|
|
{
|
|
ScriptableObjects.Add(scriptableObject);
|
|
}
|
|
|
|
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
|
|
if (prefab && !Prefabs.Contains(prefab))
|
|
{
|
|
Prefabs.Add(prefab);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void FetchDatabaseStatic()
|
|
{
|
|
Instance.FetchDatabase();
|
|
}
|
|
|
|
private static string[] GetAllPaths(string target)
|
|
{
|
|
var files = Directory.GetFiles(target).ToList();
|
|
foreach (var dir in Directory.GetDirectories(target))
|
|
{
|
|
files.AddRange(GetAllPaths(dir));
|
|
}
|
|
return files.ToArray();
|
|
}
|
|
|
|
[Serializable]
|
|
public class DataList<T> : IEnumerable<T> where T : UnityEngine.Object
|
|
{
|
|
[SerializeField]
|
|
private List<T> elements;
|
|
public DataList() => elements = new List<T>();
|
|
|
|
public T this[string key] => elements.Find(x => x.name == key);
|
|
|
|
public void Add(T element) => elements.Add(element);
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
{
|
|
return elements.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return elements.GetEnumerator();
|
|
}
|
|
|
|
public static implicit operator DataList<T>(List<T> list) => new DataList<T>() { elements = list };
|
|
}
|
|
|
|
[SerializeField]
|
|
private List<GameObject> _prefabs;
|
|
[SerializeField]
|
|
private List<ScriptableObject> _scriptableObjects;
|
|
|
|
public DataList<GameObject> Prefabs => _prefabs;
|
|
public DataList<ScriptableObject> ScriptableObjects => _scriptableObjects;
|
|
|
|
public Database()
|
|
{
|
|
_prefabs = new List<GameObject>();
|
|
_scriptableObjects = new List<ScriptableObject>();
|
|
}
|
|
#if UNITY_EDITOR
|
|
|
|
#endif
|
|
} |