private static void PreloadMeshes() { string path = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\\CustomMeshes"; if (!Directory.Exists(path)) { Dbgl($"Directory {path} does not exist! Creating."); Directory.CreateDirectory(path); return; } customMeshes.Clear(); foreach (string file in Directory.GetFiles(path, "*.obj")) { Dbgl($"Adding mesh {file}."); Mesh mesh = new ObjImporter().ImportFile(file); customMeshes.Add(mesh); } }
private static void PreloadMeshes() { foreach (AssetBundle ab in customAssetBundles.Values) { ab.Unload(true); } customMeshes.Clear(); customGameObjects.Clear(); customAssetBundles.Clear(); Dbgl($"Importing meshes"); string path = Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "CustomMeshes"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); return; } foreach (string dir in Directory.GetDirectories(path)) { string dirName = Path.GetFileName(dir); Dbgl($"Importing meshes: {dirName}"); customMeshes[dirName] = new Dictionary <string, Dictionary <string, CustomMeshData> >(); customGameObjects[dirName] = new Dictionary <string, Dictionary <string, GameObject> >(); foreach (string subdir in Directory.GetDirectories(dir)) { string subdirName = Path.GetFileName(subdir); Dbgl($"Importing meshes: {dirName}\\{subdirName}"); customMeshes[dirName][subdirName] = new Dictionary <string, CustomMeshData>(); customGameObjects[dirName][subdirName] = new Dictionary <string, GameObject>(); foreach (string file in Directory.GetFiles(subdir)) { try { SkinnedMeshRenderer renderer = null; Mesh mesh = null; Dbgl($"Importing {file} {Path.GetFileNameWithoutExtension(file)} {Path.GetFileName(file)} {Path.GetExtension(file).ToLower()}"); string name = Path.GetFileNameWithoutExtension(file); if (name == Path.GetFileName(file)) { AssetBundle ab = AssetBundle.LoadFromFile(file); customAssetBundles.Add(name, ab); GameObject prefab = ab.LoadAsset <GameObject>("Player"); if (prefab != null) { renderer = prefab.GetComponentInChildren <SkinnedMeshRenderer>(); if (renderer != null) { mesh = renderer.sharedMesh; Dbgl($"Importing {file} asset bundle as player"); } else { Dbgl($"No SkinnedMeshRenderer on {prefab}"); } if (mesh == null) { mesh = ab.LoadAsset <Mesh>("body"); } } else { mesh = ab.LoadAsset <Mesh>("body"); if (mesh != null) { Dbgl($"Importing {file} asset bundle as mesh"); } else { Dbgl("Failed to find body"); } } } else if (Path.GetExtension(file).ToLower() == ".fbx") { GameObject obj = MeshImporter.Load(file); GameObject obj2 = obj?.transform.Find("Player")?.Find("Visual")?.gameObject; // /* * int children = obj.transform.childCount; * for(int i = 0; i < children; i++) * { * Dbgl($"fbx child: {obj.transform.GetChild(i).name}"); * } */ mesh = obj.GetComponentInChildren <MeshFilter>().mesh; if (obj2 != null) { renderer = obj2.GetComponentInChildren <SkinnedMeshRenderer>(); } if (mesh != null) { if (renderer != null) { Dbgl($"Importing {file} fbx as player"); } else { Dbgl($"Importing {file} fbx as mesh"); } } } else if (Path.GetExtension(file).ToLower() == ".obj") { mesh = new ObjImporter().ImportFile(file); if (mesh != null) { Dbgl($"Imported {file} obj as mesh"); } } if (mesh != null) { customMeshes[dirName][subdirName].Add(name, new CustomMeshData(dirName, name, mesh, renderer)); Dbgl($"Added mesh data to customMeshes[{dirName}][{subdirName}][{name}]"); } } catch { } } } } }