Spaces:
Running
Running
| using UnityEngine; | |
| using UnityEditor; | |
| using System.IO; | |
| using 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 InputRaw | |
| string[] 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) & LABELING | |
| foreach (BuildTarget t in targets) | |
| { | |
| string platformName = t.ToString(); | |
| foreach (string path in allAssets) | |
| { | |
| // Rampingkan poligon 50% untuk file 3D | |
| if (path.EndsWith(".prefab") || path.EndsWith(".fbx")) { | |
| GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path); | |
| if (go != null) SimplifyMeshInGameObject(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}"); | |
| // Build binary platform tanpa ekstensi ke dalam folder assetbundles/ | |
| BuildPipeline.BuildAssetBundles( | |
| ASSETBUNDLE_DIR, | |
| BuildAssetBundleOptions.ForceRebuildAssetBundle, | |
| t | |
| ); | |
| } | |
| Debug.Log("β [UNITY] Build binary selesai."); | |
| EditorApplication.Exit(0); | |
| } | |
| catch (System.Exception e) { | |
| Debug.LogError($"π Error: {e.Message}"); | |
| EditorApplication.Exit(1); | |
| } | |
| } | |
| static void SimplifyMeshInGameObject(GameObject go) | |
| { | |
| 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(); | |
| } | |
| } | |
| 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(); | |
| } | |
| } | |
| } | |
| } |