Spaces:
webzepetoku
/
Sleeping

webzepetoku commited on
Commit
e50a4c1
·
verified ·
1 Parent(s): e42f073

Update UnityProject/Assets/Editor/TestBuilder.cs

Browse files
UnityProject/Assets/Editor/TestBuilder.cs CHANGED
@@ -3,6 +3,7 @@ using UnityEditor;
3
  using System;
4
  using System.IO;
5
  using System.Linq;
 
6
  using System.IO.Compression;
7
  using UnityMeshSimplifier;
8
 
@@ -15,59 +16,59 @@ public class TestBuilder
15
  {
16
  try
17
  {
18
- Debug.Log("🛠️ [UNITY] Inisialisasi Project (Mode Embedding Mesh)...");
19
  AssetDatabase.Refresh();
20
 
21
  if (Directory.Exists(ASSETBUNDLE_DIR)) Directory.Delete(ASSETBUNDLE_DIR, true);
22
  Directory.CreateDirectory(ASSETBUNDLE_DIR);
23
 
24
- string[] prefabPaths = Directory.GetFiles(MAIN_FOLDER, "*.prefab", SearchOption.AllDirectories);
 
 
25
 
26
- // 1. Paksa Read/Write agar data poligon bisa diambil
27
- ForceEnableMeshReadWrite(prefabPaths);
28
 
29
- Debug.Log("✨ [UNITY] Memulai perampingan mesh & embedding data...");
 
 
 
30
  foreach (string path in prefabPaths)
31
  {
32
- // Load Prefab secara mendalam
33
  GameObject go = PrefabUtility.LoadPrefabContents(path);
34
  if (go != null)
35
  {
36
- // Kirim path prefab agar mesh bisa ditanam di dalamnya
37
- SimplifyAndEmbedMesh(go, path);
38
-
39
- // Simpan kembali prefab (Sekarang datanya tertanam di dalam file .prefab)
40
  PrefabUtility.SaveAsPrefabAsset(go, path);
41
  PrefabUtility.UnloadPrefabContents(go);
42
- Debug.Log($"✅ [EMBEDDED] {Path.GetFileName(path)} sudah berisi data mesh internal.");
43
  }
44
  }
45
 
 
 
 
 
 
 
46
  AssetDatabase.SaveAssets();
47
  AssetDatabase.Refresh();
48
 
49
  EditorUtility.UnloadUnusedAssetsImmediate();
50
  GC.Collect();
51
 
52
- // 2. Build AssetBundles (Binary OS)
53
  BuildTarget[] targets = {
54
  BuildTarget.Android,
55
  BuildTarget.iOS,
56
  BuildTarget.StandaloneLinux64,
57
- BuildTarget.StandaloneOSX,
58
- BuildTarget.StandaloneWindows64,
59
- BuildTarget.WebGL
60
  };
61
 
62
  foreach (BuildTarget t in targets)
63
  {
64
  if (!BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Standalone, t) &&
65
- !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Android, t) &&
66
- !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.iOS, t) &&
67
- !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.WebGL, t))
68
- {
69
- continue;
70
- }
71
 
72
  string platformName = t.ToString();
73
  foreach (string path in prefabPaths)
@@ -76,7 +77,7 @@ public class TestBuilder
76
  if (importer != null) importer.assetBundleName = platformName;
77
  }
78
 
79
- Debug.Log($"🚀 [BUILD] Mencetak binary untuk: {platformName}");
80
  BuildPipeline.BuildAssetBundles(
81
  ASSETBUNDLE_DIR,
82
  BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
@@ -84,15 +85,15 @@ public class TestBuilder
84
  );
85
  }
86
 
87
- // 3. Repacking Final ke .zepeto
88
  string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
89
- string fileName = $"hairku_{timestamp}.zepeto";
90
  string repackPath = Path.Combine(Directory.GetParent(Application.dataPath).FullName, fileName);
91
 
92
  if (File.Exists(repackPath)) File.Delete(repackPath);
93
  ZipFile.CreateFromDirectory(MAIN_FOLDER, repackPath);
94
 
95
- Debug.Log($"✅ [SUCCESS] Hasil akhir .zepeto siap dengan mode internal mesh.");
96
  EditorApplication.Exit(0);
97
  }
98
  catch (Exception e)
@@ -102,61 +103,62 @@ public class TestBuilder
102
  }
103
  }
104
 
105
- static void ForceEnableMeshReadWrite(string[] prefabPaths)
106
  {
107
- foreach (var path in prefabPaths)
108
  {
109
- string[] deps = AssetDatabase.GetDependencies(path);
110
- foreach (var dep in deps)
111
  {
112
- ModelImporter mi = AssetImporter.GetAtPath(dep) as ModelImporter;
113
- if (mi != null && !mi.isReadable)
114
- {
115
- mi.isReadable = true;
116
- mi.SaveAndReimport();
117
- }
 
 
 
 
 
 
 
 
 
 
118
  }
119
  }
120
  }
121
 
122
- static void SimplifyAndEmbedMesh(GameObject go, string prefabPath)
123
  {
124
  var options = SimplificationOptions.Default;
125
  options.PreserveBorderEdges = false;
 
126
 
127
- // Proses MeshFilter
128
- var meshFilters = go.GetComponentsInChildren<MeshFilter>();
129
- foreach (var mf in meshFilters) {
130
- if (mf.sharedMesh != null) {
131
- var meshSimplifier = new MeshSimplifier();
132
- meshSimplifier.Initialize(mf.sharedMesh);
133
- meshSimplifier.SimplificationOptions = options;
134
- meshSimplifier.SimplifyMesh(0.5f);
135
-
136
- Mesh newMesh = meshSimplifier.ToMesh();
137
- newMesh.name = mf.sharedMesh.name + "_Internal";
138
-
139
- // RAHASIA UTAMA: Tanam mesh ke dalam file prefab
140
- AssetDatabase.AddObjectToAsset(newMesh, prefabPath);
141
- mf.sharedMesh = newMesh;
142
- }
143
- }
144
 
145
- // Proses SkinnedMesh (Untuk rambut/baju)
146
- var skinnedMeshes = go.GetComponentsInChildren<SkinnedMeshRenderer>();
147
- foreach (var smr in skinnedMeshes) {
148
- if (smr.sharedMesh != null) {
149
- var meshSimplifier = new MeshSimplifier();
150
- meshSimplifier.Initialize(smr.sharedMesh);
151
- meshSimplifier.SimplificationOptions = options;
152
- meshSimplifier.SimplifyMesh(0.1f);
 
 
 
153
 
154
- Mesh newMesh = meshSimplifier.ToMesh();
155
- newMesh.name = smr.sharedMesh.name + "_Internal";
156
-
157
- // RAHASIA UTAMA: Tanam mesh ke dalam file prefab
158
  AssetDatabase.AddObjectToAsset(newMesh, prefabPath);
159
- smr.sharedMesh = newMesh;
 
 
160
  }
161
  }
162
  }
 
3
  using System;
4
  using System.IO;
5
  using System.Linq;
6
+ using System.Collections.Generic;
7
  using System.IO.Compression;
8
  using UnityMeshSimplifier;
9
 
 
16
  {
17
  try
18
  {
19
+ Debug.Log("🛠️ [UNITY] Memulai Optimalisasi Biner & Anti-Bloat...");
20
  AssetDatabase.Refresh();
21
 
22
  if (Directory.Exists(ASSETBUNDLE_DIR)) Directory.Delete(ASSETBUNDLE_DIR, true);
23
  Directory.CreateDirectory(ASSETBUNDLE_DIR);
24
 
25
+ string[] allFiles = Directory.GetFiles(MAIN_FOLDER, "*.*", SearchOption.AllDirectories);
26
+ string[] prefabPaths = allFiles.Where(f => f.EndsWith(".prefab")).ToArray();
27
+ string[] sourceModels = allFiles.Where(f => f.EndsWith(".fbx") || f.EndsWith(".obj")).ToArray();
28
 
29
+ // 1. Matikan Label Bundle pada file sumber agar tidak terjadi Double Packing
30
+ StripSourceBundles(sourceModels);
31
 
32
+ // 2. Paksa Read/Write agar proses simplifikasi berjalan
33
+ ForceEnableMeshReadWrite(sourceModels);
34
+
35
+ // 3. Proses Embedding Mesh Ramping (Agresif 0.15f - Sisa 15% poligon)
36
  foreach (string path in prefabPaths)
37
  {
 
38
  GameObject go = PrefabUtility.LoadPrefabContents(path);
39
  if (go != null)
40
  {
41
+ SimplifyAndEmbedMeshAggressive(go, path);
 
 
 
42
  PrefabUtility.SaveAsPrefabAsset(go, path);
43
  PrefabUtility.UnloadPrefabContents(go);
44
+ Debug.Log($"✅ [OPTIMIZED] {Path.GetFileName(path)}");
45
  }
46
  }
47
 
48
+ // 4. Bersihkan file sumber fisik sebelum build biner
49
+ foreach (var source in sourceModels)
50
+ {
51
+ AssetDatabase.DeleteAsset(source);
52
+ }
53
+
54
  AssetDatabase.SaveAssets();
55
  AssetDatabase.Refresh();
56
 
57
  EditorUtility.UnloadUnusedAssetsImmediate();
58
  GC.Collect();
59
 
60
+ // 5. Build AssetBundles dengan Kompresi LZ4 Maksimal
61
  BuildTarget[] targets = {
62
  BuildTarget.Android,
63
  BuildTarget.iOS,
64
  BuildTarget.StandaloneLinux64,
65
+ BuildTarget.StandaloneWindows64
 
 
66
  };
67
 
68
  foreach (BuildTarget t in targets)
69
  {
70
  if (!BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Standalone, t) &&
71
+ !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Android, t)) continue;
 
 
 
 
 
72
 
73
  string platformName = t.ToString();
74
  foreach (string path in prefabPaths)
 
77
  if (importer != null) importer.assetBundleName = platformName;
78
  }
79
 
80
+ Debug.Log($"🚀 [BUILD] Kompresi Biner {platformName}...");
81
  BuildPipeline.BuildAssetBundles(
82
  ASSETBUNDLE_DIR,
83
  BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
 
85
  );
86
  }
87
 
88
+ // 6. Repacking .zepeto
89
  string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
90
+ string fileName = $"hairku_optimized_{timestamp}.zepeto";
91
  string repackPath = Path.Combine(Directory.GetParent(Application.dataPath).FullName, fileName);
92
 
93
  if (File.Exists(repackPath)) File.Delete(repackPath);
94
  ZipFile.CreateFromDirectory(MAIN_FOLDER, repackPath);
95
 
96
+ Debug.Log($"✅ [SUCCESS] Biner ramping berhasil dibuat: {repackPath}");
97
  EditorApplication.Exit(0);
98
  }
99
  catch (Exception e)
 
103
  }
104
  }
105
 
106
+ static void StripSourceBundles(string[] files)
107
  {
108
+ foreach (var path in files)
109
  {
110
+ AssetImporter importer = AssetImporter.GetAtPath(path);
111
+ if (importer != null)
112
  {
113
+ importer.assetBundleName = ""; // Hapus label agar tidak ikut dipack
114
+ importer.SaveAndReimport();
115
+ }
116
+ }
117
+ }
118
+
119
+ static void ForceEnableMeshReadWrite(string[] files)
120
+ {
121
+ foreach (var path in files)
122
+ {
123
+ ModelImporter mi = AssetImporter.GetAtPath(path) as ModelImporter;
124
+ if (mi != null)
125
+ {
126
+ mi.isReadable = true;
127
+ mi.meshCompression = ModelImporterMeshCompression.High;
128
+ mi.SaveAndReimport();
129
  }
130
  }
131
  }
132
 
133
+ static void SimplifyAndEmbedMeshAggressive(GameObject go, string prefabPath)
134
  {
135
  var options = SimplificationOptions.Default;
136
  options.PreserveBorderEdges = false;
137
+ options.PreserveUVSeamEdges = false;
138
 
139
+ var renderers = go.GetComponentsInChildren<Renderer>();
140
+ foreach (var ren in renderers)
141
+ {
142
+ Mesh sourceMesh = null;
143
+ if (ren is MeshRenderer mr) sourceMesh = ren.GetComponent<MeshFilter>()?.sharedMesh;
144
+ else if (ren is SkinnedMeshRenderer smr) sourceMesh = smr.sharedMesh;
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ if (sourceMesh != null)
147
+ {
148
+ var simplifier = new MeshSimplifier();
149
+ simplifier.Initialize(sourceMesh);
150
+ simplifier.SimplificationOptions = options;
151
+
152
+ // Target sangat agresif: 15% poligon asli
153
+ simplifier.SimplifyMesh(0.15f);
154
+
155
+ Mesh newMesh = simplifier.ToMesh();
156
+ newMesh.name = sourceMesh.name + "_Internal_Low";
157
 
 
 
 
 
158
  AssetDatabase.AddObjectToAsset(newMesh, prefabPath);
159
+
160
+ if (ren is MeshRenderer) ren.GetComponent<MeshFilter>().sharedMesh = newMesh;
161
+ else if (ren is SkinnedMeshRenderer s) s.sharedMesh = newMesh;
162
  }
163
  }
164
  }