Spaces:
Sleeping
Sleeping
Update UnityProject/Assets/Editor/TestBuilder.cs
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273743 unmodified linesusing System.Linq;using UnityMeshSimplifier;public class TestBuilder {static readonly string RAW_PATH = "Assets/InputRaw";static readonly string ASSETBUNDLE_DIR = "Assets/InputRaw/assetbundles";public static void ManualConvert() {// --- 1. PASTIKAN FOLDER ASSETBUNDLES BENAR-BENAR BERSIH ---if (Directory.Exists(ASSETBUNDLE_DIR)) {Directory.Delete(ASSETBUNDLE_DIR, true);}Directory.CreateDirectory(ASSETBUNDLE_DIR);// --- 2. PROSES PERAMPINGAN (TIMPA KE FILE FISIK) ---string[] allAssets = Directory.GetFiles(RAW_PATH, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".prefab")).ToArray();foreach (string path in allAssets) {// Gunakan LoadPrefabContents agar modifikasi bisa di-save sebagai Prefab baru/timpaGameObject go = PrefabUtility.LoadPrefabContents(path);bool isModified = false;if (go != null) {var meshFilters = go.GetComponentsInChildren<MeshFilter>();foreach (var mf in meshFilters) {if (mf.sharedMesh != null) {var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();meshSimplifier.Initialize(mf.sharedMesh);meshSimplifier.SimplifyMesh(0.5f);mf.sharedMesh = meshSimplifier.ToMesh();isModified = true;}}var skinnedMeshes = go.GetComponentsInChildren<SkinnedMeshRenderer>();foreach (var smr in skinnedMeshes) {if (smr.sharedMesh != null) {var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();meshSimplifier.Initialize(smr.sharedMesh);meshSimplifier.SimplifyMesh(0.5f);smr.sharedMesh = meshSimplifier.ToMesh();isModified = true;}}// INI KUNCI AGAR RIMPANG TERSIMPAN PERMANENif (isModified) {PrefabUtility.SaveAsPrefabAsset(go, path);}PrefabUtility.UnloadPrefabContents(go);}}AssetDatabase.SaveAssets();AssetDatabase.Refresh();// --- 3. BUILD ULANG ASSETBUNDLES (100% FRESH) ---string[] assets = Directory.GetFiles(RAW_PATH, "*.*", SearchOption.AllDirectories).Where(s => !s.EndsWith(".meta") && !s.Contains("assetbundles")).ToArray();foreach (string path in assets) {AssetImporter importer = AssetImporter.GetAtPath(path);if (importer != null) {importer.assetBundleName = BuildTarget.StandaloneLinux64.ToString();}}BuildPipeline.BuildAssetBundles(ASSETBUNDLE_DIR, BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneLinux64);EditorApplication.Exit(0);}}3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261273 unmodified linesusing System.Linq;using UnityMeshSimplifier;public class TestBuilder{static readonly string RAW_PATH = "Assets/InputRaw";static readonly string ASSETBUNDLE_DIR = "Assets/InputRaw/assetbundles";public static void ManualConvert(){BuildTarget[] targets = {BuildTarget.Android,BuildTarget.iOS,BuildTarget.StandaloneLinux64,BuildTarget.StandaloneOSX,BuildTarget.StandaloneWindows64,BuildTarget.WebGL};try{AssetDatabase.Refresh();// 1. Ambil semua aset asli di folder InputRawstring[] allAssets = Directory.GetFiles(RAW_PATH, "*.*", SearchOption.AllDirectories).Where(s => !s.EndsWith(".meta") && !s.Contains("assetbundles")).ToArray();if (!Directory.Exists(ASSETBUNDLE_DIR)) Directory.CreateDirectory(ASSETBUNDLE_DIR);// 2. PROSES PERAMPINGAN (SIMPLIFY) & LABELINGforeach (BuildTarget t in targets){string platformName = t.ToString();foreach (string path in allAssets){// Rampingkan poligon 50% untuk file 3D// Catatan: FBX adalah read-only, jadi kita hanya proses Prefab agar bisa disimpan permanen.if (path.EndsWith(".prefab")){// Load prefab secara benar untuk dimodifikasiGameObject go = PrefabUtility.LoadPrefabContents(path);if (go != null){SimplifyMeshInGameObject(go);// Simpan kembali perubahan ke hard drivePrefabUtility.SaveAsPrefabAsset(go, path);PrefabUtility.UnloadPrefabContents(go);}}// Beri label bundle sesuai nama platform (agar binary tanpa ekstensi)AssetImporter importer = AssetImporter.GetAtPath(path);if (importer != null) importer.assetBundleName = platformName;}Debug.Log($"🚀 [UNITY] Membangun binary platform: {platformName}");// Gunakan try-catch DI DALAM loop agar kegagalan satu platform tidak mematikan platform laintry{// Build binary platform tanpa ekstensi ke dalam folder assetbundles/BuildPipeline.BuildAssetBundles(ASSETBUNDLE_DIR,BuildAssetBundleOptions.ForceRebuildAssetBundle,t);}catch (System.Exception ex){Debug.LogWarning($"⚠️ [UNITY] Gagal build untuk {platformName}. Modul mungkin belum terinstall. Error: {ex.Message}");}}Debug.Log("✅ [UNITY] Build binary selesai.");EditorApplication.Exit(0);}catch (System.Exception e) {Debug.LogError($"💀 Error Fatal: {e.Message}");EditorApplication.Exit(1);}}static void SimplifyMeshInGameObject(GameObject go){// Pastikan ada folder untuk menyimpan file Mesh yang baru agar bisa dibaca AssetBundlestring meshDir = "Assets/InputRaw/SimplifiedMeshes";if (!Directory.Exists(meshDir)) Directory.CreateDirectory(meshDir);var meshFilters = go.GetComponentsInChildren<MeshFilter>();foreach (var mf in meshFilters) {if (mf.sharedMesh != null) {var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();meshSimplifier.Initialize(mf.sharedMesh);meshSimplifier.SimplifyMesh(0.5f);Mesh newMesh = meshSimplifier.ToMesh();// Simpan mesh baru sebagai aset fisik di diskstring newPath = $"{meshDir}/{go.name}_{mf.name}_{newMesh.GetInstanceID()}.asset";AssetDatabase.CreateAsset(newMesh, newPath);mf.sharedMesh = newMesh;}}var skinnedMeshes = go.GetComponentsInChildren<SkinnedMeshRenderer>();foreach (var smr in skinnedMeshes) {if (smr.sharedMesh != null) {var meshSimplifier = new UnityMeshSimplifier.MeshSimplifier();meshSimplifier.Initialize(smr.sharedMesh);meshSimplifier.SimplifyMesh(0.5f);Mesh newMesh = meshSimplifier.ToMesh();// Simpan mesh baru sebagai aset fisik di diskstring newPath = $"{meshDir}/{go.name}_{smr.name}_{newMesh.GetInstanceID()}_skinned.asset";AssetDatabase.CreateAsset(newMesh, newPath);smr.sharedMesh = newMesh;}}// Simpan database agar Unity mengenali aset mesh yang baru dibuatAssetDatabase.SaveAssets();}}
UnityProject/Assets/Editor/TestBuilder.cs+102-49