Spaces:
Sleeping
Sleeping
Update UnityProject/Assets/Editor/TestBuilder.cs
Browse files
UnityProject/Assets/Editor/TestBuilder.cs
CHANGED
|
@@ -8,6 +8,7 @@ using UnityMeshSimplifier;
|
|
| 8 |
|
| 9 |
public class TestBuilder
|
| 10 |
{
|
|
|
|
| 11 |
static readonly string FINAL_EXPORT_DIR = "Assets/FinalPackage";
|
| 12 |
static readonly string BUNDLE_SUBDIR = "Assets/FinalPackage/assetbundles";
|
| 13 |
|
|
@@ -15,84 +16,105 @@ public class TestBuilder
|
|
| 15 |
{
|
| 16 |
try
|
| 17 |
{
|
| 18 |
-
Debug.Log("🛠️ [SYSTEM] Memulai
|
| 19 |
|
|
|
|
| 20 |
if (Directory.Exists(FINAL_EXPORT_DIR)) Directory.Delete(FINAL_EXPORT_DIR, true);
|
|
|
|
| 21 |
Directory.CreateDirectory(FINAL_EXPORT_DIR);
|
| 22 |
Directory.CreateDirectory(BUNDLE_SUBDIR);
|
| 23 |
|
|
|
|
| 24 |
string[] prefabPaths = Directory.GetFiles("Assets/InputRaw", "*.prefab", SearchOption.AllDirectories);
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
foreach (string path in prefabPaths)
|
| 27 |
{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
GameObject go = PrefabUtility.LoadPrefabContents(path);
|
| 29 |
if (go != null)
|
| 30 |
{
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
//
|
| 35 |
-
string prefabName = Path.GetFileName(path);
|
| 36 |
-
string exportPath = Path.Combine(FINAL_EXPORT_DIR, prefabName);
|
| 37 |
PrefabUtility.SaveAsPrefabAsset(go, exportPath);
|
| 38 |
PrefabUtility.UnloadPrefabContents(go);
|
| 39 |
|
| 40 |
-
//
|
| 41 |
-
CopyDependencies(path);
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
|
|
|
| 45 |
AssetDatabase.Refresh();
|
| 46 |
|
| 47 |
-
//
|
| 48 |
-
|
| 49 |
|
| 50 |
-
Debug.Log("✅ [UNITY]
|
| 51 |
EditorApplication.Exit(0);
|
| 52 |
}
|
| 53 |
catch (Exception e)
|
| 54 |
{
|
| 55 |
-
Debug.LogError($"💀 [FATAL] {e.Message}");
|
| 56 |
EditorApplication.Exit(1);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
-
static void
|
| 61 |
{
|
| 62 |
-
var options = SimplificationOptions.Default;
|
| 63 |
var renderers = go.GetComponentsInChildren<Renderer>();
|
| 64 |
foreach (var ren in renderers)
|
| 65 |
{
|
| 66 |
-
Mesh sourceMesh = (ren is MeshRenderer mr) ? ren.GetComponent<MeshFilter>()?.sharedMesh :
|
|
|
|
|
|
|
| 67 |
if (sourceMesh != null)
|
| 68 |
{
|
|
|
|
| 69 |
var simplifier = new MeshSimplifier();
|
| 70 |
simplifier.Initialize(sourceMesh);
|
| 71 |
-
simplifier.SimplifyMesh(0.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
-
static void CopyDependencies(string
|
| 82 |
{
|
| 83 |
-
string sourceDir = Path.GetDirectoryName(prefabPath);
|
| 84 |
string[] files = Directory.GetFiles(sourceDir, "*.*");
|
| 85 |
foreach (string file in files)
|
| 86 |
{
|
| 87 |
-
string
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
-
static void
|
| 94 |
{
|
| 95 |
-
// Daftar platform sesuai struktur 'sesudah'
|
| 96 |
BuildTarget[] targets = {
|
| 97 |
BuildTarget.Android,
|
| 98 |
BuildTarget.iOS,
|
|
@@ -105,11 +127,10 @@ public class TestBuilder
|
|
| 105 |
foreach (var t in targets)
|
| 106 |
{
|
| 107 |
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
|
| 108 |
-
buildMap[0].assetBundleName = t.ToString(); // Nama file biner (
|
| 109 |
buildMap[0].assetNames = AssetDatabase.FindAssets("t:Prefab", new[] { FINAL_EXPORT_DIR })
|
| 110 |
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
|
| 111 |
|
| 112 |
-
// Build langsung ke dalam folder assetbundles/
|
| 113 |
BuildPipeline.BuildAssetBundles(BUNDLE_SUBDIR, buildMap,
|
| 114 |
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
|
| 115 |
t);
|
|
|
|
| 8 |
|
| 9 |
public class TestBuilder
|
| 10 |
{
|
| 11 |
+
// Folder hasil akhir sebelum di-zip oleh Python
|
| 12 |
static readonly string FINAL_EXPORT_DIR = "Assets/FinalPackage";
|
| 13 |
static readonly string BUNDLE_SUBDIR = "Assets/FinalPackage/assetbundles";
|
| 14 |
|
|
|
|
| 16 |
{
|
| 17 |
try
|
| 18 |
{
|
| 19 |
+
Debug.Log("🛠️ [SYSTEM] Memulai Proses Konversi Universal...");
|
| 20 |
|
| 21 |
+
// 1. Bersihkan lingkungan
|
| 22 |
if (Directory.Exists(FINAL_EXPORT_DIR)) Directory.Delete(FINAL_EXPORT_DIR, true);
|
| 23 |
+
if (Directory.Exists(BUNDLE_SUBDIR)) Directory.Delete(BUNDLE_SUBDIR, true);
|
| 24 |
Directory.CreateDirectory(FINAL_EXPORT_DIR);
|
| 25 |
Directory.CreateDirectory(BUNDLE_SUBDIR);
|
| 26 |
|
| 27 |
+
// 2. Cari semua prefab di folder InputRaw (Bisa baju, rambut, celana, dll)
|
| 28 |
string[] prefabPaths = Directory.GetFiles("Assets/InputRaw", "*.prefab", SearchOption.AllDirectories);
|
| 29 |
|
| 30 |
+
if (prefabPaths.Length == 0) {
|
| 31 |
+
Debug.LogError("❌ [ERROR] Tidak ditemukan file .prefab di Assets/InputRaw!");
|
| 32 |
+
EditorApplication.Exit(1);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
foreach (string path in prefabPaths)
|
| 36 |
{
|
| 37 |
+
string fileName = Path.GetFileName(path);
|
| 38 |
+
string exportPath = Path.Combine(FINAL_EXPORT_DIR, fileName);
|
| 39 |
+
|
| 40 |
+
// --- PROSES EMBED MESH KE PREFAB ---
|
| 41 |
GameObject go = PrefabUtility.LoadPrefabContents(path);
|
| 42 |
if (go != null)
|
| 43 |
{
|
| 44 |
+
Debug.Log($"💎 [PROCESS] Optimasi & Embedding: {fileName}");
|
| 45 |
+
|
| 46 |
+
// Lakukan optimasi mesh (diset 0.05f atau 5% agar m_DataSize sangat kecil)
|
| 47 |
+
OptimizeAndEmbedMesh(go, exportPath);
|
| 48 |
|
| 49 |
+
// Simpan sebagai Prefab baru di folder ekspor
|
|
|
|
|
|
|
| 50 |
PrefabUtility.SaveAsPrefabAsset(go, exportPath);
|
| 51 |
PrefabUtility.UnloadPrefabContents(go);
|
| 52 |
|
| 53 |
+
// Salin file pendukung (Texture, Shader, Meta) agar struktur lengkap
|
| 54 |
+
CopyDependencies(Path.GetDirectoryName(path), FINAL_EXPORT_DIR);
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
+
AssetDatabase.SaveAssets();
|
| 59 |
AssetDatabase.Refresh();
|
| 60 |
|
| 61 |
+
// 3. PROSES BUILD BINARY (AssetBundles) untuk SEMUA PLATFORM
|
| 62 |
+
BuildAllBinaries();
|
| 63 |
|
| 64 |
+
Debug.Log("✅ [UNITY] Semua proses selesai. Binary telah dibuat.");
|
| 65 |
EditorApplication.Exit(0);
|
| 66 |
}
|
| 67 |
catch (Exception e)
|
| 68 |
{
|
| 69 |
+
Debug.LogError($"💀 [FATAL ERROR] {e.Message}\n{e.StackTrace}");
|
| 70 |
EditorApplication.Exit(1);
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
static void OptimizeAndEmbedMesh(GameObject go, string exportPath)
|
| 75 |
{
|
|
|
|
| 76 |
var renderers = go.GetComponentsInChildren<Renderer>();
|
| 77 |
foreach (var ren in renderers)
|
| 78 |
{
|
| 79 |
+
Mesh sourceMesh = (ren is MeshRenderer mr) ? ren.GetComponent<MeshFilter>()?.sharedMesh :
|
| 80 |
+
(ren is SkinnedMeshRenderer smr) ? smr.sharedMesh : null;
|
| 81 |
+
|
| 82 |
if (sourceMesh != null)
|
| 83 |
{
|
| 84 |
+
// Optimasi Mesh
|
| 85 |
var simplifier = new MeshSimplifier();
|
| 86 |
simplifier.Initialize(sourceMesh);
|
| 87 |
+
simplifier.SimplifyMesh(0.05f); // Sisa 5% poligon (Ultra Low)
|
| 88 |
+
|
| 89 |
+
Mesh optimizedMesh = simplifier.ToMesh();
|
| 90 |
+
optimizedMesh.name = sourceMesh.name + "_Optimized";
|
| 91 |
+
|
| 92 |
+
// --- INI PROSES ADD TO PREFAB (EMBEDDING) ---
|
| 93 |
+
// Simpan mesh biner langsung ke dalam file Prefab hasil ekspor
|
| 94 |
+
AssetDatabase.CreateAsset(optimizedMesh, "Assets/temp_mesh.asset");
|
| 95 |
+
AssetDatabase.AddObjectToAsset(optimizedMesh, exportPath);
|
| 96 |
+
|
| 97 |
+
// Terapkan mesh baru ke renderer
|
| 98 |
+
if (ren is MeshRenderer) ren.GetComponent<MeshFilter>().sharedMesh = optimizedMesh;
|
| 99 |
+
else if (ren is SkinnedMeshRenderer s) s.sharedMesh = optimizedMesh;
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
+
static void CopyDependencies(string sourceDir, string targetDir)
|
| 105 |
{
|
|
|
|
| 106 |
string[] files = Directory.GetFiles(sourceDir, "*.*");
|
| 107 |
foreach (string file in files)
|
| 108 |
{
|
| 109 |
+
string name = Path.GetFileName(file);
|
| 110 |
+
// Jangan salin ulang prefab mentah (kita pakai yang sudah di-optimize)
|
| 111 |
+
if (name.EndsWith(".prefab") || name.EndsWith(".prefab.meta")) continue;
|
| 112 |
+
File.Copy(file, Path.Combine(targetDir, name), true);
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
+
static void BuildAllBinaries()
|
| 117 |
{
|
|
|
|
| 118 |
BuildTarget[] targets = {
|
| 119 |
BuildTarget.Android,
|
| 120 |
BuildTarget.iOS,
|
|
|
|
| 127 |
foreach (var t in targets)
|
| 128 |
{
|
| 129 |
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
|
| 130 |
+
buildMap[0].assetBundleName = t.ToString(); // Nama file biner (Android, iOS, dll)
|
| 131 |
buildMap[0].assetNames = AssetDatabase.FindAssets("t:Prefab", new[] { FINAL_EXPORT_DIR })
|
| 132 |
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
|
| 133 |
|
|
|
|
| 134 |
BuildPipeline.BuildAssetBundles(BUNDLE_SUBDIR, buildMap,
|
| 135 |
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
|
| 136 |
t);
|