summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XEditor/ReplaceEquip.cs
blob: 2161ccbbae2596905394517bcdd21e197b305021 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using XUtliPoolLib;
using XEditor;
using System.IO;

public class ReplaceEquip : MonoBehaviour
{
    [Serializable]
    public class ReplacePair
    {
        public string srcMeshName;
        public string replaceEquipName;
        public Texture2D replaceTex;
        public bool isShareTex;
    }
    public GameObject fbx;
    public string srcName;
    public string replaceName;
    public string srcDir;
    public string targetDir;
    [SerializeField]
    public ReplacePair[] ReplaceMeshList;
    static List<SkinnedMeshRenderer> smrList = new List<SkinnedMeshRenderer>();
    static List<MeshFilter> mfList = new List<MeshFilter>();
    static List<ReplacePair> rpList = new List<ReplacePair>();
    public void Refresh()
    {
        if (fbx == null)
            return;
        string fbxPath = AssetDatabase.GetAssetPath(fbx).ToLower();

        int profession = AssetModify.GetProfession(fbxPath);
        if (profession < 0)
        {
            Debug.LogError("profession not found:"+ fbxPath);
            return;
        }
        string srcDirName = AssetModify.MakeEquipDir(fbx.name, profession);
        if (string.IsNullOrEmpty(srcDirName))
        {
            Debug.LogError("equip dir not generate");
            return;
        }
        srcDir = "Equipments/" + srcDirName;
        string targetDirName = srcDirName.Replace(srcName, replaceName);
        targetDir = "Equipments/" + targetDirName;

        GameObject fbxIns = GameObject.Instantiate<GameObject>(fbx);
        rpList.Clear();
        smrList.Clear();
        fbxIns.GetComponentsInChildren<SkinnedMeshRenderer>(smrList);
        
        foreach (SkinnedMeshRenderer smr in smrList)
        {
            ReplacePair rp = new ReplacePair();
            rp.srcMeshName = smr.name;
            if(smr.name.ToLower().EndsWith("_weapon"))
            {
                rp.replaceEquipName = AssetModify.MakeWeaponName(profession);
            }
            else
            {
                rp.replaceEquipName = AssetModify.MakeEquipName(smr.sharedMesh.name, profession, targetDirName);
            }            
            if (smr.sharedMaterial != null)
            {
                Texture2D tex = smr.sharedMaterial.mainTexture as Texture2D;
                string texPath = AssetDatabase.GetAssetPath(tex).ToLower();
                string targetTexPath = texPath.Replace(srcName, replaceName);
                rp.replaceTex = AssetDatabase.LoadAssetAtPath<Texture2D>(targetTexPath);
            }
            rpList.Add(rp);
        }
        smrList.Clear();
        mfList.Clear();
        fbxIns.GetComponentsInChildren<MeshFilter>(mfList);
        foreach (MeshFilter mf in mfList)
        {
            ReplacePair rp = new ReplacePair();
            rp.srcMeshName = mf.name;
            rp.replaceEquipName = AssetModify.MakeWeaponName(profession);
            MeshRenderer mr = mf.GetComponent<MeshRenderer>();
            if (mr != null && mr.sharedMaterial != null)
            {
                Texture2D tex = mr.sharedMaterial.mainTexture as Texture2D;
                string texPath = AssetDatabase.GetAssetPath(tex).ToLower();
                string targetTexPath = texPath.Replace(srcName, replaceName);
                rp.replaceTex = AssetDatabase.LoadAssetAtPath<Texture2D>(targetTexPath);
            }
            rpList.Add(rp);
        }
        mfList.Clear();
        GameObject.DestroyImmediate(fbxIns);
        ReplaceMeshList = rpList.ToArray();
        rpList.Clear();
    }
    
    [MenuItem(@"Assets/Tool/Equipment/InitReplaceEquip")]
    public static void Init()
    {
        GameObject[] objs = Selection.gameObjects;
        AssetModify.InitCombineConfig();
        foreach (GameObject obj in objs)
        {
            string path = AssetDatabase.GetAssetPath(obj).ToLower();
            ReplaceEquip re = null;
            GameObject replaceEquip = null;
            bool save = false;
            if (path.EndsWith(".prefab"))
            {
                re = obj.GetComponent<ReplaceEquip>();
            }
            else if (path.EndsWith(".fbx"))
            {
                replaceEquip = new GameObject(obj.name);
                re = replaceEquip.AddComponent<ReplaceEquip>();
                re.srcName = "01";
                re.replaceName = "02";
                re.fbx = obj;
                save = true;
            }
            if (re == null)
            {
                if (replaceEquip != null)
                {
                    GameObject.DestroyImmediate(replaceEquip);
                }
                continue;
            }
            re.Refresh();
            if (save)
            {
                int index = path.LastIndexOf(".");
                if (index > 0)
                {
                    path = path.Substring(0, index) + ".prefab";
                    XEditor.AssetModify.CreateOrReplacePrefab(replaceEquip, path);
                }
            }

            if (replaceEquip != null)
            {
                GameObject.DestroyImmediate(replaceEquip);
            }
        }
    }
    public void Process(bool make = true)
    {
        if (ReplaceMeshList != null)
        {
            string saveRootPath = "Assets/Resources/" + targetDir + "/";
            if (make && !Directory.Exists(saveRootPath))
            {
                Directory.CreateDirectory(saveRootPath);
            }
            AssetModify.usedTex.Clear();
            foreach (ReplacePair rp in ReplaceMeshList)
            {
                Texture2D tex = rp.replaceTex;
                if (tex != null)
                {
                    if (rp.replaceEquipName.EndsWith("_weapon"))
                    {
                        if(make)
                        {
                            string srcWeaponPrefab = string.Format("Assets/Resources/{0}/{1}.prefab", srcDir, rp.replaceEquipName);
                            string targetWeaponPrefab = string.Format("Assets/Resources/{0}/{1}.prefab", targetDir, rp.replaceEquipName);
                            GameObject srcWeapon = AssetDatabase.LoadAssetAtPath<GameObject>(srcWeaponPrefab);
                            if (srcWeapon != null)
                            {

                                Renderer render = srcWeapon.GetComponent<Renderer>();
                                if (render != null)
                                {
                                    GameObject targetWeapon = GameObject.Instantiate<GameObject>(srcWeapon);
                                    Renderer targetRender = targetWeapon.GetComponent<Renderer>();
                                    targetWeapon.name = rp.replaceEquipName;
                                    Material desMat = new Material(render.sharedMaterial);
                                    desMat.name = rp.replaceTex.name;
                                    desMat.mainTexture = rp.replaceTex;
                                    AssetModify.DefaultCompressTex(rp.replaceTex, AssetDatabase.GetAssetPath(rp.replaceTex), true, true);
                                    string srcMatPath = AssetDatabase.GetAssetPath(render.sharedMaterial);
                                    int index = srcMatPath.LastIndexOf("/");
                                    string targetMatPath = string.Format("{0}/{1}.mat", srcMatPath.Substring(0, index), desMat.name);
                                    Material newMat = AssetModify.CreateOrReplaceAsset<Material>(desMat, targetMatPath);
                                    targetRender.sharedMaterial = newMat;
                                    AssetModify.CreateOrReplacePrefab(targetWeapon, targetWeaponPrefab);
                                    GameObject.DestroyImmediate(targetWeapon);
                                }
                            }
                            else
                            {
                                Debug.LogError("null prefab:" + srcWeaponPrefab);
                            }
                        }                        
                    }
                    else
                    {
                        string srcMeshPath = srcDir + "/" + rp.replaceEquipName;
                        string replaceMeshPath = targetDir + "/" + rp.replaceEquipName;
                        AssetModify.PartTexInfo partTexInfo = null;
                        if (!AssetModify.usedTex.TryGetValue(tex.GetHashCode(), out partTexInfo))
                        {
                            string srcTexPath = AssetDatabase.GetAssetPath(tex);
                            partTexInfo = new AssetModify.PartTexInfo();
                            if (rp.isShareTex)
                            {                                
                                partTexInfo.texPath = "Equipments/" + targetDir + "/" + rp.replaceEquipName;
                                partTexInfo.isAlpha = File.Exists(partTexInfo.texPath + "_A.png");
                            }
                            else
                            {
                                bool isAlpha = false;
                                if(make)
                                {
                                    string targetTexPath = saveRootPath + rp.replaceEquipName + ".tga";
                                    AssetDatabase.CopyAsset(srcTexPath, targetTexPath);
                                    Texture2D mainTex = AssetDatabase.LoadAssetAtPath<Texture2D>(targetTexPath);
                                    Texture2D alphaTex = null;
                                    if (File.Exists("Assets/Resources/Equipments/" + targetDir + "/" + rp.replaceEquipName + "_A.png"))
                                    {
                                        alphaTex = AssetModify.ConvertTexRtex(mainTex);
                                    }
                                    AssetModify.DefaultCompressTex(mainTex, targetTexPath, true, true);
                                    isAlpha = alphaTex != null;
                                }
                                else
                                {
                                    isAlpha = File.Exists(replaceMeshPath + "_A.png");
                                }

                                partTexInfo.texPath = replaceMeshPath;
                                partTexInfo.isAlpha = isAlpha;
                                AssetModify.usedTex.Add(tex.GetHashCode(), partTexInfo);
                            }
                        }
                        string prefix = "";
                        int index = rp.replaceEquipName.IndexOf("_");
                        if (index >= 0)
                        {
                            prefix = rp.replaceEquipName.Substring(0, index);
                        }
                        AssetModify.AddPart(srcMeshPath, replaceMeshPath, srcDir.Replace("Equipments/",""), partTexInfo.texPath, tex.width == 1024, partTexInfo.isAlpha, rp.isShareTex, prefix);
                    }
                }
                else
                {
                    Debug.LogError("null tex:" + rp.srcMeshName);
                }
            }
        }
    }
    [MenuItem(@"Assets/Tool/Equipment/MakeReplaceEquip")]
    public static void MakeReplaceEquip()
    {
        AssetModify.InitCombineConfig();
        AssetModify.LoadMeshPartInfo();
        GameObject[] objs = Selection.gameObjects;

        for (int i = 0; i < objs.Length; ++i)
        {
            GameObject obj = objs[i];
            EditorUtility.DisplayProgressBar(string.Format("{0}-{1}/{2}", "Process GameObject", i, objs.Length), obj.name, (float)i / objs.Length);
            string path = AssetDatabase.GetAssetPath(obj).ToLower();
            ReplaceEquip re = null;
            if (path.EndsWith(".prefab"))
            {
                re = obj.GetComponent<ReplaceEquip>();
            }
            if (re == null)
            {
                continue;
            }
            re.Process();
        }
        AssetModify.SaveEquipInfo();       
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("Finish", "All objects processed finish", "OK");
    }
    //public void RefreshConfig()
    //{
    //    if (ReplaceMeshList != null)
    //    {
    //        string saveRootPath = "Assets/Resources/" + targetDir + "/";
    //        AssetModify.usedTex.Clear();
    //        foreach (ReplacePair rp in ReplaceMeshList)
    //        {
    //            Texture2D tex = rp.replaceTex;
    //            if (tex != null)
    //            {
    //                if (!rp.replaceEquipName.EndsWith("_weapon"))
    //                {
    //                    string srcMeshPath = srcDir + "/" + rp.srcMeshName;
    //                    string replaceMeshPath = targetDir + "/" + rp.replaceEquipName;
    //                    AssetModify.PartTexInfo partTexInfo = null;
    //                    if (!AssetModify.usedTex.TryGetValue(tex.GetHashCode(), out partTexInfo))
    //                    {
    //                        string srcTexPath = AssetDatabase.GetAssetPath(tex);
    //                        partTexInfo = new AssetModify.PartTexInfo();
    //                        if (rp.isShareTex)
    //                        {                                
    //                            partTexInfo.texPath = "Equipments/" + srcDir + "/" + rp.replaceEquipName;
    //                            partTexInfo.isAlpha = File.Exists(partTexInfo.texPath + "_A.png");
    //                        }
    //                        else
    //                        {
    //                            partTexInfo.texPath = replaceMeshPath;
    //                            partTexInfo.isAlpha = File.Exists(partTexInfo.texPath + "_A.png");
    //                            AssetModify.usedTex.Add(tex.GetHashCode(), partTexInfo);
    //                        }
    //                    }
    //                    string prefix = "";
    //                    int index = rp.srcMeshName.IndexOf("_");
    //                    if(index>=0)
    //                    {
    //                        prefix = rp.srcMeshName.Substring(0, index);
    //                    }
    //                    AssetModify.AddPart(srcMeshPath, replaceMeshPath, rp.replaceEquipName, partTexInfo.texPath, tex.width == 1024, partTexInfo.isAlpha, rp.isShareTex, prefix);
    //                }
    //            }
    //            else
    //            {
    //                Debug.LogError("null tex:" + fbx.name);
    //            }
    //        }
    //    }
    //}
}

[CanEditMultipleObjects, CustomEditor(typeof(ReplaceEquip))]
public class ReplaceEquipEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (GUILayout.Button("Refresh", GUILayout.MaxWidth(100)))
        {
            AssetModify.InitCombineConfig();
            for (int i = 0; i < targets.Length; ++i)
            {
                ReplaceEquip re = targets[i] as ReplaceEquip;
                if (re != null)
                {
                    re.Refresh();
                    GameObject go = GameObject.Instantiate<GameObject>(re.gameObject);
                    PrefabUtility.ReplacePrefab(go, re.gameObject, ReplacePrefabOptions.ReplaceNameBased);
                    GameObject.DestroyImmediate(go);
                }

            }

        }
        if (GUILayout.Button("Make", GUILayout.MaxWidth(100)))
        {           
            if(targets.Length>0)
            {
                AssetModify.InitCombineConfig();
                AssetModify.LoadMeshPartInfo();
                for (int i = 0; i < targets.Length; ++i)
                {
                    ReplaceEquip re = targets[i] as ReplaceEquip;
                    EditorUtility.DisplayProgressBar(string.Format("{0}-{1}/{2}", "Process GameObject", i, targets.Length), "", (float)i / targets.Length);
                    if (re != null)
                    {
                        re.Process();
                    }
                }
                AssetModify.SaveEquipInfo();
                AssetDatabase.Refresh();
                EditorUtility.ClearProgressBar();
                EditorUtility.DisplayDialog("Finish", "All objects processed finish", "OK");
            }
           
        }
       
    }
}
public class EquipPathInfo : IComparable<EquipPathInfo>
{
    public string name = "";
    public string texPath = "";
    public byte type = 0;
    public string newName = "";
    public string newTexPath = "";
    public Texture2D replaceTex = null;
    public int CompareTo(EquipPathInfo other)
    {
        if (other == null)
            return 1;
        return name.CompareTo(other.name);
    }
}

#endif