problème : au moment de load une save, les tiles qui spawnaient des GameObjects au start les spawnaient malgré qu'ils l'avait déjà spawné dans la dernière session de jeu + problèmes de sérialisation divers solution : spawner le GameObject seulement si le lifetime de la tile est de zéro. correction des différents problèmes de sérialisation. note : les tiles ne semblent vraiment pas être faites pour avoir une update loop. mais bon, maintenant ça marche.
103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using System;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
|
|
[CustomEditor(typeof(Database))]
|
|
public class DatabaseEditor : Editor
|
|
{
|
|
public override void OnInspectorGUI()
|
|
{
|
|
DrawDefaultInspector();
|
|
|
|
if (GUILayout.Button("fetch assets"))
|
|
{
|
|
var targ = target as Database;
|
|
|
|
|
|
|
|
foreach (var folder in targ.Folders)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(folder);
|
|
foreach (var file in GetAllPaths(path))
|
|
{
|
|
var scriptableObject = AssetDatabase.LoadAssetAtPath<ScriptableObject>(file);
|
|
if (scriptableObject && !targ.ScriptableObjects.Contains(scriptableObject))
|
|
{
|
|
targ.ScriptableObjects.Add(scriptableObject);
|
|
}
|
|
|
|
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
|
|
if (prefab && !targ.Prefabs.Contains(prefab))
|
|
{
|
|
targ.Prefabs.Add(prefab);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
string[] GetAllPaths(string target)
|
|
{
|
|
var files = Directory.GetFiles(target).ToList();
|
|
foreach (var dir in Directory.GetDirectories(target))
|
|
{
|
|
files.AddRange(GetAllPaths(dir));
|
|
}
|
|
return files.ToArray();
|
|
}
|
|
}
|
|
#endif
|
|
public class Database : SingletonBehaviour<Database>
|
|
{
|
|
|
|
public const string TYPE = nameof(TYPE);
|
|
[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>();
|
|
}
|
|
[Header("Editor section")]
|
|
[SerializeField]
|
|
private List<DefaultAsset> _folders;
|
|
|
|
public List<DefaultAsset> Folders => _folders;
|
|
} |