webzepetoku commited on
Commit
396e9bd
·
verified ·
1 Parent(s): c3a8f33

Update UnityProject/Assets/Editor/TestBuilder.cs

Browse files
UnityProject/Assets/Editor/TestBuilder.cs CHANGED
@@ -1,102 +1,111 @@
1
- using UnityEngine;
2
- using UnityEditor;
3
- using System.IO;
4
- using System.Collections.Generic;
5
-
6
- public class TestBuilder
7
- {
8
- public static void ManualConvert()
9
- {
10
- // 1. BACA INPUT DARI HUGGING FACE
11
- string inputFileName = GetArg("-inputFile");
12
- if (string.IsNullOrEmpty(inputFileName))
13
- {
14
- Debug.LogError("❌ Tidak ada file input!");
15
- return;
16
- }
17
-
18
- string rootPath = Directory.GetCurrentDirectory();
19
- string inputPath = Path.Combine(rootPath, "Assets", inputFileName);
20
- string outputDir = Path.Combine(rootPath, "output");
21
- string outputFileName = Path.GetFileNameWithoutExtension(inputFileName) + ".zepeto";
22
- string finalOutputPath = Path.Combine(outputDir, outputFileName);
23
-
24
- // 2. IMPORT & OPTIMASI TEXTURE (RAHASIA KUALITAS)
25
- // Kita paksa refresh agar Unity membaca settingan impor terbaru
26
- AssetDatabase.ImportAsset($"Assets/{inputFileName}", ImportAssetOptions.ForceUpdate);
27
-
28
- // Load Model
29
- GameObject fbx = AssetDatabase.LoadAssetAtPath<GameObject>($"Assets/{inputFileName}");
30
- if (fbx == null) return;
31
-
32
- // 3. AUTO-OPTIMIZER: TEXTURE COMPRESSION
33
- // Kita cari semua tekstur yang nempel di FBX ini dan set kompresinya
34
- Renderer[] renderers = fbx.GetComponentsInChildren<Renderer>();
35
- foreach (Renderer r in renderers)
36
- {
37
- foreach (Material m in r.sharedMaterials)
38
- {
39
- if (m.mainTexture != null)
40
- {
41
- string texPath = AssetDatabase.GetAssetPath(m.mainTexture);
42
- TextureImporter ti = AssetImporter.GetAtPath(texPath) as TextureImporter;
43
- if (ti != null)
44
- {
45
- // RAHASIA RAMPING 1: Pakai ASTC (Standar Mobile High Quality)
46
- TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings();
47
- settings.name = "Android";
48
- settings.overridden = true;
49
- settings.maxTextureSize = 2048; // Batasi max 2K (Biar gak kegedean)
50
- settings.format = TextureImporterFormat.ASTC_6x6; // Kompresi tinggi, kualitas bagus
51
- settings.compressionQuality = 100;
52
- ti.SetPlatformTextureSettings(settings);
53
- ti.SaveAndReimport();
54
- }
55
- }
56
- }
57
- }
58
-
59
- // 4. BUAT PREFAB
60
- GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(fbx);
61
- string prefabPath = "Assets/TempZepetoItem.prefab";
62
- PrefabUtility.SaveAsPrefabAsset(instance, prefabPath);
63
- GameObject.DestroyImmediate(instance);
64
-
65
- // 5. BUILD ASSET BUNDLE (RAHASIA RAMPING 2)
66
- // ChunkBasedCompression (LZ4) adalah standar Zepeto: Cepat load, ukuran kecil.
67
- AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
68
- buildMap[0].assetBundleName = "zepeto_bundle";
69
- buildMap[0].assetNames = new string[] { prefabPath };
70
-
71
- BuildPipeline.BuildAssetBundles(
72
- "TempBuild",
73
- buildMap,
74
- BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode,
75
- BuildTarget.Android // Zepeto base-nya Android/iOS
76
- );
77
-
78
- // 6. FINISHING
79
- string generatedFile = Path.Combine(rootPath, "TempBuild", "zepeto_bundle");
80
- if (File.Exists(generatedFile))
81
- {
82
- if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);
83
- if (File.Exists(finalOutputPath)) File.Delete(finalOutputPath);
84
- File.Move(generatedFile, finalOutputPath);
85
- Debug.Log($"✅ [OPTIMIZED] File Zepeto Ramping Tersimpan: {finalOutputPath}");
86
- }
87
-
88
- // Bersihkan sampah
89
- AssetDatabase.DeleteAsset($"Assets/{inputFileName}");
90
- AssetDatabase.DeleteAsset(prefabPath);
91
- }
92
-
93
- private static string GetArg(string name)
94
- {
95
- var args = System.Environment.GetCommandLineArgs();
96
- for (int i = 0; i < args.Length; i++)
97
- {
98
- if (args[i] == name && args.Length > i + 1) return args[i + 1];
99
- }
100
- return null;
101
- }
 
 
 
 
 
 
 
 
 
102
  }
 
1
+ using UnityEngine;
2
+ using UnityEditor;
3
+ using System.IO;
4
+ using System.Collections.Generic;
5
+
6
+ public class TestBuilder
7
+ {
8
+ public static void ManualConvert()
9
+ {
10
+ // 1. BACA INPUT DARI HUGGING FACE
11
+ string inputFileName = GetArg("-inputFile");
12
+ if (string.IsNullOrEmpty(inputFileName))
13
+ {
14
+ Debug.LogError("❌ Tidak ada file input!");
15
+ return;
16
+ }
17
+
18
+ string rootPath = Directory.GetCurrentDirectory();
19
+ string assetPath = $"Assets/{inputFileName}";
20
+ string outputDir = Path.Combine(rootPath, "output");
21
+ string outputFileName = Path.GetFileNameWithoutExtension(inputFileName) + ".zepeto";
22
+ string finalOutputPath = Path.Combine(outputDir, outputFileName);
23
+
24
+ // 2. RAHASIA RAMPING 1: OPTIMASI MESH (POLIGON & VERTEX)
25
+ // Sebelum load model, kita set dulu aturan import-nya
26
+ ModelImporter mi = AssetImporter.GetAtPath(assetPath) as ModelImporter;
27
+ if (mi != null)
28
+ {
29
+ mi.meshCompression = ModelImporterMeshCompression.High; // Kompresi poligon tinggi
30
+ mi.optimizeMeshPolygons = true;
31
+ mi.optimizeMeshVertices = true;
32
+ mi.weldVertices = true; // Menggabungkan vertex yang berdekatan (mengurangi berat)
33
+ mi.importBlendShapes = false; // Matikan jika tidak butuh (hemat size besar)
34
+ mi.addSecondaryUV = false;
35
+ AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
36
+ }
37
+
38
+ // Load Model setelah dioptimasi
39
+ GameObject fbx = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
40
+ if (fbx == null) return;
41
+
42
+ // 3. RAHASIA RAMPING 2: TEXTURE COMPRESSION (ASTC)
43
+ Renderer[] renderers = fbx.GetComponentsInChildren<Renderer>();
44
+ foreach (Renderer r in renderers)
45
+ {
46
+ foreach (Material m in r.sharedMaterials)
47
+ {
48
+ if (m != null && m.mainTexture != null)
49
+ {
50
+ string texPath = AssetDatabase.GetAssetPath(m.mainTexture);
51
+ TextureImporter ti = AssetImporter.GetAtPath(texPath) as TextureImporter;
52
+ if (ti != null)
53
+ {
54
+ TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings();
55
+ settings.name = "Android";
56
+ settings.overridden = true;
57
+ settings.maxTextureSize = 1024; // Zepeto menyarankan 1024 untuk item kecil
58
+ settings.format = TextureImporterFormat.ASTC_6x6;
59
+ settings.compressionQuality = 100;
60
+ ti.SetPlatformTextureSettings(settings);
61
+ ti.SaveAndReimport();
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ // 4. BUAT PREFAB
68
+ GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(fbx);
69
+ string prefabPath = "Assets/TempZepetoItem.prefab";
70
+ PrefabUtility.SaveAsPrefabAsset(instance, prefabPath);
71
+ GameObject.DestroyImmediate(instance);
72
+
73
+ // 5. BUILD ASSET BUNDLE (.ZEPETO)
74
+ if (!Directory.Exists("TempBuild")) Directory.CreateDirectory("TempBuild");
75
+
76
+ AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
77
+ buildMap[0].assetBundleName = "zepeto_bundle";
78
+ buildMap[0].assetNames = new string[] { prefabPath };
79
+
80
+ BuildPipeline.BuildAssetBundles(
81
+ "TempBuild",
82
+ buildMap,
83
+ BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode,
84
+ BuildTarget.Android
85
+ );
86
+
87
+ // 6. FINISHING & MOVE TO OUTPUT
88
+ string generatedFile = Path.Combine(rootPath, "TempBuild", "zepeto_bundle");
89
+ if (File.Exists(generatedFile))
90
+ {
91
+ if (!Directory.Exists(outputDir)) Directory.CreateDirectory(outputDir);
92
+ if (File.Exists(finalOutputPath)) File.Delete(finalOutputPath);
93
+ File.Move(generatedFile, finalOutputPath);
94
+ Debug.Log($"✅ [ALL OPTIMIZED] Poligon & Tekstur Ramping: {finalOutputPath}");
95
+ }
96
+
97
+ // Bersihkan sampah
98
+ AssetDatabase.DeleteAsset(assetPath);
99
+ AssetDatabase.DeleteAsset(prefabPath);
100
+ }
101
+
102
+ private static string GetArg(string name)
103
+ {
104
+ var args = System.Environment.GetCommandLineArgs();
105
+ for (int i = 0; i < args.Length; i++)
106
+ {
107
+ if (args[i] == name && args.Length > i + 1) return args[i + 1];
108
+ }
109
+ return null;
110
+ }
111
  }