File size: 6,334 Bytes
fd1820b
 
 
 
9057489
 
1f5565a
fd1820b
 
 
9057489
ac5a419
9057489
fd1820b
 
 
 
0a0c060
eadb69c
d8fa142
376c05f
0a0c060
376c05f
c1b8c2f
6f0ead1
fd1820b
0a0c060
fd1820b
 
 
 
 
 
 
376c05f
 
 
0a0c060
fd1820b
 
 
 
 
 
 
 
 
c6b7e40
cb090b4
fd1820b
376c05f
 
9057489
 
 
 
 
 
6c4fc7c
9057489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd1820b
 
 
 
0a0c060
376c05f
fd1820b
376c05f
 
fd1820b
 
 
d8fa142
376c05f
6057360
 
 
 
 
 
 
 
 
 
 
fd1820b
 
0a0c060
fd1820b
b32fa12
6057360
 
 
 
 
1f5565a
fd1820b
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityMeshSimplifier; 

public class TestBuilder
{
    // ==========================================
    // FASE 1: MINIFY MESH
    // ==========================================
    public static void ManualConvert()
    {
        try
        {
            // --- WORKER LIMIT ---
            Unity.Jobs.LowLevel.Unsafe.JobsUtility.JobWorkerCount = 4;
            Debug.Log($"βš™οΈ [CLI] Job Worker Count: {Unity.Jobs.LowLevel.Unsafe.JobsUtility.JobWorkerCount}");

            // --- BACKEND FIX ---
            PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.Mono2x);
            PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.Mono2x);
            PlayerSettings.SetScriptingBackend(BuildTargetGroup.iOS, ScriptingImplementation.IL2CPP);

            // --- PROSES PERAMPINGAN MESH ---
            string inputPath = "Assets/InputRaw";
            if (Directory.Exists(inputPath))
            {
                string[] prefabs = Directory.GetFiles(inputPath, "*.prefab", SearchOption.AllDirectories);
                foreach (string path in prefabs)
                {
                    GameObject go = PrefabUtility.LoadPrefabContents(path);
                    if (go != null)
                    {
                        Debug.Log($"πŸ“¦ Merampingkan Mesh: {path}");
                        SimplifyMesh(go, path);
                        PrefabUtility.SaveAsPrefabAsset(go, path);
                        PrefabUtility.UnloadPrefabContents(go);
                    }
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            Debug.Log("βœ… [CLI] Fase 1 (Minify) selesai dengan sukses. Menutup instance untuk masuk Fase Upload.");
            EditorApplication.Exit(0);
        }
        catch (Exception e)
        {
            Debug.LogError($"πŸ’€ [ERROR FATAL FASE 1] {e.ToString()}");
            EditorApplication.Exit(1);
        }
    }

    // ==========================================
    // FASE 2: UPLOAD ZEPETO 
    // ==========================================
    public static void ZepetoUploadPhase()
    {
        try
        {
            Debug.Log("πŸš€ [CLI] Memulai Fase 2: Eksekusi Pengepakan & Upload ZEPETO...");
            
            Type zepetoType = null;
            
            // Pencarian Aman (Safe Reflection)
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    var types = assembly.GetTypes();
                    var foundType = types.FirstOrDefault(t => t != null && t.Name == "ZepetoPackageCliBuilder");
                    if (foundType != null)
                    {
                        zepetoType = foundType;
                        break;
                    }
                }
                catch (ReflectionTypeLoadException e)
                {
                    var foundType = e.Types.FirstOrDefault(t => t != null && t.Name == "ZepetoPackageCliBuilder");
                    if (foundType != null)
                    {
                        zepetoType = foundType;
                        break;
                    }
                }
                catch (Exception) {}
            }

            if (zepetoType != null)
            {
                MethodInfo buildMethod = zepetoType.GetMethod("BuildWithArgs", BindingFlags.Public | BindingFlags.Static);
                if (buildMethod != null)
                {
                    Debug.Log("βœ… [CLI] Memanggil ZepetoPackageCliBuilder.BuildWithArgs()...");
                    buildMethod.Invoke(null, null);
                    // Dibiarkan tanpa Exit(0) agar thread asinkron ZEPETO bisa berjalan untuk Upload
                }
                else
                {
                    Debug.LogError("πŸ’€ [ERROR FASE 2] Method BuildWithArgs tidak ditemukan di class ZepetoPackageCliBuilder!");
                    EditorApplication.Exit(1);
                }
            }
            else
            {
                Debug.LogError("πŸ’€ [ERROR FASE 2] Class ZepetoPackageCliBuilder tidak ditemukan! Pastikan library Zepeto sudah ter-load di Fase 2.");
                EditorApplication.Exit(1);
            }
        }
        catch (Exception e)
        {
            Debug.LogError($"πŸ’€ [ERROR FATAL FASE 2 ASLI] {e.ToString()}");
            EditorApplication.Exit(1);
        }
    }

    static void SimplifyMesh(GameObject go, string path)
    {
        var renderers = go.GetComponentsInChildren<Renderer>();
        foreach (var ren in renderers)
        {
            Mesh src = (ren is MeshRenderer mr) ? ren.GetComponent<MeshFilter>()?.sharedMesh :
                      (ren is SkinnedMeshRenderer smr) ? smr.sharedMesh : null;

            if (src != null && !src.name.Contains("_Internal"))
            {
                string assetPath = AssetDatabase.GetAssetPath(src);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                    if (importer != null && !importer.isReadable)
                    {
                        importer.isReadable = true;
                        importer.SaveAndReimport();
                    }
                }

                var simplifier = new MeshSimplifier();
                simplifier.Initialize(src);
                simplifier.SimplifyMesh(0.05f); // Sisa 5% poligon
                Mesh newMesh = simplifier.ToMesh();
                newMesh.name = src.name + "_Internal";
                
                string directory = Path.GetDirectoryName(path);
                string newMeshPath = Path.Combine(directory, newMesh.name + "_" + Guid.NewGuid().ToString().Substring(0, 5) + ".asset");
                newMeshPath = newMeshPath.Replace("\\", "/");
                AssetDatabase.CreateAsset(newMesh, newMeshPath);
                
                if (ren is MeshRenderer) ren.GetComponent<MeshFilter>().sharedMesh = newMesh;
                else if (ren is SkinnedMeshRenderer s) s.sharedMesh = newMesh;
            }
        }
    }
}