Spaces:
Sleeping
Sleeping
Update UnityProject/Assets/Editor/TestBuilder.cs
Browse files
UnityProject/Assets/Editor/TestBuilder.cs
CHANGED
|
@@ -8,7 +8,6 @@ using UnityMeshSimplifier;
|
|
| 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,62 +15,52 @@ public class TestBuilder
|
|
| 16 |
{
|
| 17 |
try
|
| 18 |
{
|
| 19 |
-
Debug.Log("🛠️ [
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
// Lakukan optimasi mesh (diset 0.05f atau 5% agar m_DataSize sangat kecil)
|
| 47 |
-
OptimizeAndEmbedMesh(go, exportPath);
|
| 48 |
|
| 49 |
-
//
|
| 50 |
PrefabUtility.SaveAsPrefabAsset(go, exportPath);
|
| 51 |
PrefabUtility.UnloadPrefabContents(go);
|
| 52 |
|
| 53 |
-
//
|
| 54 |
-
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
AssetDatabase.SaveAssets();
|
| 59 |
AssetDatabase.Refresh();
|
| 60 |
|
| 61 |
-
//
|
| 62 |
-
|
| 63 |
|
| 64 |
-
Debug.Log("✅ [UNITY]
|
| 65 |
EditorApplication.Exit(0);
|
| 66 |
}
|
| 67 |
catch (Exception e)
|
| 68 |
{
|
| 69 |
-
Debug.LogError($"💀 [
|
| 70 |
EditorApplication.Exit(1);
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
-
static void
|
| 75 |
{
|
| 76 |
var renderers = go.GetComponentsInChildren<Renderer>();
|
| 77 |
foreach (var ren in renderers)
|
|
@@ -81,59 +70,47 @@ public class TestBuilder
|
|
| 81 |
|
| 82 |
if (sourceMesh != null)
|
| 83 |
{
|
| 84 |
-
// Optimasi Mesh
|
| 85 |
var simplifier = new MeshSimplifier();
|
| 86 |
simplifier.Initialize(sourceMesh);
|
| 87 |
-
simplifier.SimplifyMesh(0.05f); //
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 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
|
| 105 |
{
|
| 106 |
-
string
|
| 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(
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
-
static void
|
| 117 |
{
|
| 118 |
BuildTarget[] targets = {
|
| 119 |
-
BuildTarget.Android,
|
| 120 |
-
BuildTarget.
|
| 121 |
-
BuildTarget.WebGL,
|
| 122 |
-
BuildTarget.StandaloneWindows64,
|
| 123 |
-
BuildTarget.StandaloneOSX,
|
| 124 |
-
BuildTarget.StandaloneLinux64
|
| 125 |
};
|
| 126 |
|
| 127 |
foreach (var t in targets)
|
| 128 |
{
|
| 129 |
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
|
| 130 |
-
buildMap[0].assetBundleName = t.ToString();
|
| 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);
|
| 137 |
}
|
| 138 |
}
|
| 139 |
}
|
|
|
|
| 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 |
{
|
| 16 |
try
|
| 17 |
{
|
| 18 |
+
Debug.Log("🛠️ [UNITY] Memulai Export Struktur Lengkap...");
|
| 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 |
+
if (prefabPaths.Length == 0) throw new Exception("Folder Assets/InputRaw kosong!");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
foreach (string path in prefabPaths)
|
| 28 |
{
|
| 29 |
string fileName = Path.GetFileName(path);
|
| 30 |
string exportPath = Path.Combine(FINAL_EXPORT_DIR, fileName);
|
| 31 |
|
|
|
|
| 32 |
GameObject go = PrefabUtility.LoadPrefabContents(path);
|
| 33 |
if (go != null)
|
| 34 |
{
|
| 35 |
+
// 1. EMBED MESH KE PREFAB (Agar m_DataSize kecil)
|
| 36 |
+
ProcessAndEmbed(go, exportPath);
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
// 2. SIMPAN PREFAB KE FINAL PACKAGE
|
| 39 |
PrefabUtility.SaveAsPrefabAsset(go, exportPath);
|
| 40 |
PrefabUtility.UnloadPrefabContents(go);
|
| 41 |
|
| 42 |
+
// 3. SALIN PNG, SHADER, META (Universal)
|
| 43 |
+
CopyRelatedFiles(Path.GetDirectoryName(path), FINAL_EXPORT_DIR);
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
AssetDatabase.SaveAssets();
|
| 48 |
AssetDatabase.Refresh();
|
| 49 |
|
| 50 |
+
// 4. BUILD SEMUA BINARY PLATFORM
|
| 51 |
+
BuildPlatforms();
|
| 52 |
|
| 53 |
+
Debug.Log("✅ [UNITY] Build Berhasil Selesai.");
|
| 54 |
EditorApplication.Exit(0);
|
| 55 |
}
|
| 56 |
catch (Exception e)
|
| 57 |
{
|
| 58 |
+
Debug.LogError($"💀 [ERROR] {e.Message}");
|
| 59 |
EditorApplication.Exit(1);
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
+
static void ProcessAndEmbed(GameObject go, string exportPath)
|
| 64 |
{
|
| 65 |
var renderers = go.GetComponentsInChildren<Renderer>();
|
| 66 |
foreach (var ren in renderers)
|
|
|
|
| 70 |
|
| 71 |
if (sourceMesh != null)
|
| 72 |
{
|
|
|
|
| 73 |
var simplifier = new MeshSimplifier();
|
| 74 |
simplifier.Initialize(sourceMesh);
|
| 75 |
+
simplifier.SimplifyMesh(0.05f); // Agresif 5% poligon
|
| 76 |
+
Mesh newMesh = simplifier.ToMesh();
|
| 77 |
+
newMesh.name = sourceMesh.name + "_Internal";
|
| 78 |
+
|
| 79 |
+
// Tanam Mesh langsung ke dalam file Prefab
|
| 80 |
+
AssetDatabase.AddObjectToAsset(newMesh, exportPath);
|
| 81 |
+
|
| 82 |
+
if (ren is MeshRenderer) ren.GetComponent<MeshFilter>().sharedMesh = newMesh;
|
| 83 |
+
else if (ren is SkinnedMeshRenderer s) s.sharedMesh = newMesh;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
+
static void CopyRelatedFiles(string src, string dest)
|
| 89 |
{
|
| 90 |
+
foreach (string file in Directory.GetFiles(src, "*.*"))
|
|
|
|
| 91 |
{
|
| 92 |
string name = Path.GetFileName(file);
|
|
|
|
| 93 |
if (name.EndsWith(".prefab") || name.EndsWith(".prefab.meta")) continue;
|
| 94 |
+
File.Copy(file, Path.Combine(dest, name), true);
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
+
static void BuildPlatforms()
|
| 99 |
{
|
| 100 |
BuildTarget[] targets = {
|
| 101 |
+
BuildTarget.Android, BuildTarget.iOS, BuildTarget.WebGL,
|
| 102 |
+
BuildTarget.StandaloneWindows64, BuildTarget.StandaloneOSX, BuildTarget.StandaloneLinux64
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
};
|
| 104 |
|
| 105 |
foreach (var t in targets)
|
| 106 |
{
|
| 107 |
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
|
| 108 |
+
buildMap[0].assetBundleName = t.ToString();
|
| 109 |
buildMap[0].assetNames = AssetDatabase.FindAssets("t:Prefab", new[] { FINAL_EXPORT_DIR })
|
| 110 |
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
|
| 111 |
|
| 112 |
BuildPipeline.BuildAssetBundles(BUNDLE_SUBDIR, buildMap,
|
| 113 |
+
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression, t);
|
|
|
|
| 114 |
}
|
| 115 |
}
|
| 116 |
}
|