2using System.Collections;
3using System.Collections.Generic;
6using NaughtyAttributes;
18 private readonly Queue<Action> dispatcher =
new Queue<Action>();
19 private readonly Dictionary<string, GameObject> loadedObjects =
new Dictionary<string, GameObject>();
20 private string currentPath =
"";
21 private bool loadingNow =
false;
22 private bool stopLoading =
false;
23 private short progress = 0;
24 private byte nextUnload = 1;
26 public bool EditorFastLoad =
true;
33 if (loadedObjects.ContainsKey(filepath))
35 go = loadedObjects[filepath];
49 if (FindObjectsOfType<Preloader>().Length > 1)
54 DontDestroyOnLoad(gameObject);
55 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"models"));
56 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"pictures"));
57 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"quizes"));
58 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"sounds"));
59 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"videos"));
60 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"scripts"));
61 Directory.CreateDirectory(Path.Combine(Application.streamingAssetsPath,
"helps"));
69 var list =
Directory.GetFiles(Application.streamingAssetsPath).ToList()
70 .Concat(
Directory.GetFiles(Path.Combine(Application.streamingAssetsPath,
"models")).ToList()
71 .Concat(
Directory.GetFiles(Path.Combine(Application.streamingAssetsPath,
"pictures")).ToList()));
72 var preloadFiles =
new List<string>();
73 foreach (var s
in list)
75 switch (s.Substring(s.LastIndexOf(
'.') + 1).ToLower())
149 StartCoroutine(WorkPreload(preloadFiles));
151 private bool IsPicture(
string file)
153 var ext = Path.GetExtension(file).ToLower();
155 return ext ==
".png" || ext ==
".jpg" || ext ==
".jpeg" || ext ==
".bmp";
158 private IEnumerator WorkPreload(IEnumerable<string> allFile)
160 var enumerable = allFile as
string[] ?? allFile.ToArray();
162 foreach (var s
in enumerable)
171 long MonoHeapSize =
UnityEngine.Profiling.Profiler.GetMonoHeapSizeLong();
174 Debug.Log($
"MonoHeapSize: {MonoHeapSize / 1024 / 1024}MB");
175 Debug.Log($
"usedHeapSize: {UnityEngine.Profiling.Profiler.usedHeapSizeLong / 1024 / 1024}MB");
179 Resources.UnloadUnusedAssets();
195 var fileData =
File.ReadAllBytes(s);
196 var filename = s.Substring(s.LastIndexOf(
'\\') + 1);
199 var tex =
new Texture2D(1, 1);
200 tex.LoadImage(fileData);
210 if (MonoHeapSize / 1024 / 1024 > 1024)
218 Resources.UnloadUnusedAssets();
224 Resources.UnloadUnusedAssets();
230 private void LoadModel(
string path)
234 using (var assetLoader =
new AssetLoaderAsync())
237 assetLoaderOptions.MaterialTransparencyMode = MaterialTransparencyMode.Cutout;
238 assetLoaderOptions.AdvancedConfigs.Add(AssetAdvancedConfig.CreateConfig(AssetAdvancedPropertyClassNames.FBXImportPreservePivots,
false));
239 var thread = assetLoader.LoadFromFile(path, assetLoaderOptions,
LoadRoot, progressCallback: ProgressCallback, onAssetLoaded: OnLoadFinished);
243 private void ProgressCallback(
float progress)
245 dispatcher.Enqueue(() =>
251 private void OnLoadFinished(GameObject go)
255 go.name = currentPath.Remove(0, currentPath.LastIndexOf(
"\\") + 1);
259 go.name = currentPath;
264 loadedObjects.Add(go.name, go);
268 Debug.LogError(
"Same named models, can't load into dictionary!");
273 ShaderChanger.
Change(go.GetComponentsInChildren<MeshRenderer>(),
"Ciconia Studio/Double Sided/Standard/Diffuse Bump");
281 while (dispatcher.Count > 0)
285 dispatcher?.Dequeue()();
TriLibCore.AssetLoaderOptions AssetLoaderOptions
bool HasTexture(string filename)
void AddTexture(string filename, Texture2D texture)
static GlobalTextureHolder instance
static bool IsInChild(Transform t, string childName)
bool HasFileLoaded(string filepath, out GameObject go)
static void Change(Material mat, string shaderName)