Spaces:
Sleeping
Sleeping
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]
|
| 19 |
AssetDatabase.Refresh();
|
| 20 |
|
| 21 |
if (Directory.Exists(ASSETBUNDLE_DIR)) Directory.Delete(ASSETBUNDLE_DIR, true);
|
| 22 |
Directory.CreateDirectory(ASSETBUNDLE_DIR);
|
| 23 |
|
| 24 |
-
string[]
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
// 1.
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
foreach (string path in prefabPaths)
|
| 31 |
{
|
| 32 |
-
// Load Prefab secara mendalam
|
| 33 |
GameObject go = PrefabUtility.LoadPrefabContents(path);
|
| 34 |
if (go != null)
|
| 35 |
{
|
| 36 |
-
|
| 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($"✅ [
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
AssetDatabase.SaveAssets();
|
| 47 |
AssetDatabase.Refresh();
|
| 48 |
|
| 49 |
EditorUtility.UnloadUnusedAssetsImmediate();
|
| 50 |
GC.Collect();
|
| 51 |
|
| 52 |
-
//
|
| 53 |
BuildTarget[] targets = {
|
| 54 |
BuildTarget.Android,
|
| 55 |
BuildTarget.iOS,
|
| 56 |
BuildTarget.StandaloneLinux64,
|
| 57 |
-
BuildTarget.
|
| 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]
|
| 80 |
BuildPipeline.BuildAssetBundles(
|
| 81 |
ASSETBUNDLE_DIR,
|
| 82 |
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
|
|
@@ -84,15 +85,15 @@ public class TestBuilder
|
|
| 84 |
);
|
| 85 |
}
|
| 86 |
|
| 87 |
-
//
|
| 88 |
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
| 89 |
-
string fileName = $"
|
| 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]
|
| 96 |
EditorApplication.Exit(0);
|
| 97 |
}
|
| 98 |
catch (Exception e)
|
|
@@ -102,61 +103,62 @@ public class TestBuilder
|
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
-
static void
|
| 106 |
{
|
| 107 |
-
foreach (var path in
|
| 108 |
{
|
| 109 |
-
|
| 110 |
-
|
| 111 |
{
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
-
static void
|
| 123 |
{
|
| 124 |
var options = SimplificationOptions.Default;
|
| 125 |
options.PreserveBorderEdges = false;
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
var
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 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 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 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 |
}
|