Spaces:
Sleeping
Sleeping
Update UnityProject/Assets/Editor/TestBuilder.cs
Browse files
UnityProject/Assets/Editor/TestBuilder.cs
CHANGED
|
@@ -4,133 +4,66 @@ using System;
|
|
| 4 |
using System.IO;
|
| 5 |
using System.Linq;
|
| 6 |
using System.Collections.Generic;
|
| 7 |
-
using System.IO.Compression;
|
| 8 |
using UnityMeshSimplifier;
|
| 9 |
|
| 10 |
public class TestBuilder
|
| 11 |
{
|
| 12 |
-
static readonly string
|
| 13 |
-
static readonly string
|
| 14 |
|
| 15 |
public static void ManualConvert()
|
| 16 |
{
|
| 17 |
try
|
| 18 |
{
|
| 19 |
-
Debug.Log("🛠️ [
|
| 20 |
AssetDatabase.Refresh();
|
| 21 |
|
| 22 |
-
|
| 23 |
-
Directory.
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
string[]
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
PrefabUtility.UnloadPrefabContents(go);
|
| 44 |
-
Debug.Log($"✅ [
|
| 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 |
-
|
| 74 |
-
|
| 75 |
-
{
|
| 76 |
-
AssetImporter importer = AssetImporter.GetAtPath(path);
|
| 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,
|
| 84 |
-
t
|
| 85 |
-
);
|
| 86 |
-
}
|
| 87 |
|
| 88 |
-
|
| 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)
|
| 100 |
{
|
| 101 |
-
Debug.LogError($"💀 [FATAL] {e.Message}");
|
| 102 |
EditorApplication.Exit(1);
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
-
static void
|
| 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;
|
|
@@ -148,12 +81,10 @@ public class TestBuilder
|
|
| 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 + "
|
| 157 |
|
| 158 |
AssetDatabase.AddObjectToAsset(newMesh, prefabPath);
|
| 159 |
|
|
@@ -162,4 +93,16 @@ public class TestBuilder
|
|
| 162 |
}
|
| 163 |
}
|
| 164 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
}
|
|
|
|
| 4 |
using System.IO;
|
| 5 |
using System.Linq;
|
| 6 |
using System.Collections.Generic;
|
|
|
|
| 7 |
using UnityMeshSimplifier;
|
| 8 |
|
| 9 |
public class TestBuilder
|
| 10 |
{
|
| 11 |
+
static readonly string EXPORT_DIR = "Assets/ZepetoExport";
|
| 12 |
+
static readonly string BUNDLE_DIR = "Assets/BundleOutput";
|
| 13 |
|
| 14 |
public static void ManualConvert()
|
| 15 |
{
|
| 16 |
try
|
| 17 |
{
|
| 18 |
+
Debug.Log("🛠️ [ZEPETO] Memulai optimasi mesh standar 2020...");
|
| 19 |
AssetDatabase.Refresh();
|
| 20 |
|
| 21 |
+
// Bersihkan folder output
|
| 22 |
+
if (Directory.Exists(EXPORT_DIR)) Directory.Delete(EXPORT_DIR, true);
|
| 23 |
+
if (Directory.Exists(BUNDLE_DIR)) Directory.Delete(BUNDLE_DIR, true);
|
| 24 |
+
Directory.CreateDirectory(EXPORT_DIR);
|
| 25 |
+
Directory.CreateDirectory(BUNDLE_DIR);
|
| 26 |
|
| 27 |
+
string[] prefabPaths = Directory.GetFiles("Assets/InputRaw", "*.prefab", SearchOption.AllDirectories);
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
if (prefabPaths.Length == 0) {
|
| 30 |
+
Debug.LogError("❌ [ERROR] Tidak ada file prefab di Assets/InputRaw");
|
| 31 |
+
EditorApplication.Exit(1);
|
| 32 |
+
}
|
|
|
|
| 33 |
|
|
|
|
| 34 |
foreach (string path in prefabPaths)
|
| 35 |
{
|
| 36 |
GameObject go = PrefabUtility.LoadPrefabContents(path);
|
| 37 |
if (go != null)
|
| 38 |
{
|
| 39 |
+
// Gunakan Ultra Low (0.05f) agar m_DataSize kecil seperti Crgh_3
|
| 40 |
+
SimplifyAndEmbed(go, path);
|
| 41 |
+
|
| 42 |
+
string fileName = Path.GetFileName(path);
|
| 43 |
+
string exportPath = Path.Combine(EXPORT_DIR, fileName);
|
| 44 |
+
PrefabUtility.SaveAsPrefabAsset(go, exportPath);
|
| 45 |
PrefabUtility.UnloadPrefabContents(go);
|
| 46 |
+
Debug.Log($"✅ [EMBEDDED] {fileName}");
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
AssetDatabase.SaveAssets();
|
| 51 |
AssetDatabase.Refresh();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
// Jalankan Build AssetBundle (Biner Android untuk ZEPETO)
|
| 54 |
+
BuildZepetoBundle();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
Debug.Log("🚀 [UNITY] Proses selesai. Menyerahkan packing ke Python.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
EditorApplication.Exit(0);
|
| 58 |
}
|
| 59 |
catch (Exception e)
|
| 60 |
{
|
| 61 |
+
Debug.LogError($"💀 [FATAL] {e.Message}\n{e.StackTrace}");
|
| 62 |
EditorApplication.Exit(1);
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
+
static void SimplifyAndEmbed(GameObject go, string prefabPath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
{
|
| 68 |
var options = SimplificationOptions.Default;
|
| 69 |
options.PreserveBorderEdges = false;
|
|
|
|
| 81 |
var simplifier = new MeshSimplifier();
|
| 82 |
simplifier.Initialize(sourceMesh);
|
| 83 |
simplifier.SimplificationOptions = options;
|
| 84 |
+
simplifier.SimplifyMesh(0.05f);
|
|
|
|
|
|
|
| 85 |
|
| 86 |
Mesh newMesh = simplifier.ToMesh();
|
| 87 |
+
newMesh.name = sourceMesh.name + "_Internal";
|
| 88 |
|
| 89 |
AssetDatabase.AddObjectToAsset(newMesh, prefabPath);
|
| 90 |
|
|
|
|
| 93 |
}
|
| 94 |
}
|
| 95 |
}
|
| 96 |
+
|
| 97 |
+
static void BuildZepetoBundle()
|
| 98 |
+
{
|
| 99 |
+
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
|
| 100 |
+
buildMap[0].assetBundleName = "zepeto.bundle";
|
| 101 |
+
buildMap[0].assetNames = AssetDatabase.FindAssets("t:Prefab", new[] { EXPORT_DIR })
|
| 102 |
+
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).ToArray();
|
| 103 |
+
|
| 104 |
+
BuildPipeline.BuildAssetBundles(BUNDLE_DIR, buildMap,
|
| 105 |
+
BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
|
| 106 |
+
BuildTarget.Android);
|
| 107 |
+
}
|
| 108 |
}
|