-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExportLuaUI.cs
199 lines (175 loc) · 6.4 KB
/
ExportLuaUI.cs
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
/*
lua UI 脚本导出工具 by glegoo
*/
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class ExportLuaUI : Editor
{
delegate bool Filter(Component component);
[MenuItem("Assets/Export Lua UI Script")]
static void ExportSelect()
{
if (Selection.objects != null)
{
Object[] prefabs = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
if (prefabs != null)
{
string outPath = Application.dataPath + "/../../../../../TulongQPProject/Assets/Tlqp/Lua/View";
string savePath = EditorUtility.SaveFolderPanel("选择导出位置", Path.GetFullPath(outPath), "");
if (!string.IsNullOrEmpty(savePath))
{
foreach (Object obj in prefabs)
{
ExportLua(obj as GameObject, savePath);
}
}
}
}
}
static void ExportLua(GameObject prefab, string savePath)
{
Component[] components = GetFiltered<Component>(prefab, (component) =>
{
if (component.transform.parent == null || component.transform.parent.name == "static")
return false;
if (component is UILabel && component.name.Contains("lab"))
return true;
if (component is UISprite && component.name.Contains("spr"))
return true;
if (component is BoxCollider && component.name.Contains("btn"))
return true;
return false;
});
if (components != null)
{
Dictionary<Transform, List<Component>> map = new Dictionary<Transform, List<Component>>();
foreach (Component item in components)
{
Transform parent = item.transform.parent;
if (!map.ContainsKey(parent))
{
map.Add(parent, new List<Component>());
}
map[parent].Add(item);
}
WriteLuaFile(map, savePath, prefab.name);
}
}
static void WriteLuaFile(Dictionary<Transform, List<Component>> map, string savePath, string name)
{
savePath = string.Format("{0}/{1}.lua", savePath, name);
Debug.Log("SavePath: " + savePath);
if (File.Exists(savePath))
{
int option = EditorUtility.DisplayDialogComplex("文件已存在",
savePath + "已存在, 是否替换?",
"替换",
"取消",
"保留两者");
switch (option)
{
case 0:
File.Delete(savePath);
break;
case 1:
return;
case 2:
savePath = savePath.Replace(".lua", "_auto.lua");
if (File.Exists(savePath))
File.Delete(savePath);
break;
default:
Debug.LogError("Unrecognized option.");
break;
}
}
FileStream fs = new FileStream(savePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.WriteLine("--[[Created by lua UI script exporter.]]");
sw.WriteLine(string.Format("{0} = {{}}", name));
sw.WriteLine(string.Format("local this = {0}", name));
sw.WriteLine();
sw.WriteLine("local m_transform");
sw.WriteLine("local m_luaBehaviour");
// 局部变量
sw.WriteLine();
foreach (KeyValuePair<Transform, List<Component>> pair in map)
{
foreach (Component component in pair.Value)
{
sw.WriteLine(string.Format("local m_{0}", component.name));
}
}
// Awake方法
sw.WriteLine();
sw.WriteLine("function this.Awake(obj)");
sw.WriteLine("\tm_luaBehaviour = obj:GetComponent('LuaBehaviour')");
sw.WriteLine("\tm_transform = obj.transform");
foreach (KeyValuePair<Transform, List<Component>> pair in map)
{
sw.WriteLine("\t");
sw.WriteLine(string.Format("\tlocal {0} = m_transform:Find('{1}')", pair.Key.name, GetNodePath(pair.Key)));
foreach (Component component in pair.Value)
{
if (component is UIWidget)
{
sw.WriteLine(string.Format("\tm_{0} = {1}:Find('{0}'):GetComponent('{2}')", component.name, pair.Key.name, component.GetType()));
}
else if (component is BoxCollider)
{
sw.WriteLine(string.Format("\tm_{0} = {1}:Find('{0}').gameObject", component.name, pair.Key.name));
sw.WriteLine(string.Format("\tm_luaBehaviour:AddClick({0}, this.On{1}Clicked);", "m_" + component.name, component.name.Replace("btn", "")));
}
}
}
sw.WriteLine("end");
sw.WriteLine();
sw.WriteLine("function this.Start()");
sw.WriteLine("\t");
sw.WriteLine("end");
foreach (KeyValuePair<Transform, List<Component>> pair in map)
{
foreach (Component component in pair.Value)
{
if (component is BoxCollider)
{
sw.WriteLine();
sw.WriteLine("function this.On{0}Clicked()", component.name.Replace("btn", ""));
sw.WriteLine("\t");
sw.WriteLine("end");
}
}
}
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
static T[] GetFiltered<T>(GameObject prefab, Filter filter) where T : Component
{
T[] components = prefab.GetComponentsInChildren<T>(true);
if (components == null)
return null;
List<T> result = new List<T>(components);
result = result.FindAll((component) =>
{
return filter(component);
});
return result.ToArray();
}
static string GetNodePath(Transform trans)
{
string result = trans.name;
while (trans.parent.parent != null)
{
result = trans.parent.name + "/" + result;
trans = trans.parent;
}
return result;
}
}