static void CreateItemLua() { //获取在Project视图中选择的所有游戏对象 Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); List <GameObject> prefabList = new List <GameObject>(); foreach (Object obj in SelectedAsset) { if (obj is GameObject) { prefabList.Add((GameObject)obj); } } for (int i = 0; i < prefabList.Count; i++) { GameObject obj = prefabList[i]; string sourcePath = AssetDatabase.GetAssetPath(obj); sourcePath = sourcePath.Replace("Assets/Prefabs/", ""); sourcePath = sourcePath.Replace("/" + obj.name + ".prefab", ""); string path = Application.dataPath.ToLower() + "/LuaFramework/Lua/UI/" + sourcePath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string luaStr = "local " + obj.name + " = class(\"" + obj.name + "\")\n\n"; luaStr += "function " + obj.name + ":ctor(parent, info, luaBehaviour)\n\t" + "self.parent = parent\n\tself.info = info\n\tself.luaBehaviour = luaBehaviour\n\tself:InitView()\nend\n\n"; luaStr += "function " + obj.name + ":InitView()\n\tself.gameObject = LuaToCSFunction.LoadPrefabByName(" + "\"Prefabs/" + sourcePath + "/" + obj.name + "\", self.parent)\n\tself.transform = self.gameObject.transform\n\t" + "LuaToCSFunction.SetGameObjectLocalPosition(self.gameObject, 0, 0, 0)\n"; List <LuaObj> luaObjList = GetLuaObjList(obj); List <string> btnNameList = new List <string>(); List <string> labelNameList = new List <string>(); for (int j = 0; j < luaObjList.Count; j++) { LuaObj luaObj = luaObjList[j]; string name = GetGameObjectPath(luaObj.obj).Replace("/" + obj.name + "/", ""); if (name.EndsWith("-") || name.EndsWith("_")) { continue; } if (!name.Equals("/" + obj.name)) { if (luaObj.type == "BoxCollider") { luaStr += "\tself." + name.Replace("/", "_") + "_Obj" + GetEndComponentStr(luaObj.type, name); } else if (luaObj.type == "UISprite") { luaStr += "\tself." + name.Replace("/", "_") + "_Sprite" + GetEndComponentStr(luaObj.type, name); } else { luaStr += "\tself." + name.Replace("/", "_") + GetEndComponentStr(luaObj.type, name); } } if (luaObj.type == "BoxCollider") { if (name.Equals("/" + obj.name)) { btnNameList.Add("self.gameObject"); } else { btnNameList.Add("self." + name.Replace("/", "_") + "_Obj"); } } else if (luaObj.type == "UILabel") { labelNameList.Add("self." + name.Replace("/", "_")); } } if (labelNameList.Count > 0) { for (int o = 0; o < labelNameList.Count; o++) { string labelName = labelNameList[o]; luaStr += "\t" + labelName + ".text = tables.LanguageDic[\"Bow06\"]" + "\n"; } } luaStr += btnNameList.Count > 0 ? "\n" : ""; for (int m = 0; m < btnNameList.Count; m++) { string btnName = btnNameList[m]; luaStr += "\tself.parent.luaBehaviour:RemoveClick(" + btnName + ")\n"; luaStr += "\tself.parent.luaBehaviour:AddClick(" + btnName + ", self, self.OnClickHandler)" + "\n"; } luaStr += btnNameList.Count == 0 ? "end\n\n" : "" + "end" + "\n\n"; if (btnNameList.Count > 0) { luaStr += "function " + obj.name + ":OnClickHandler(go)\n"; for (int o = 0; o < btnNameList.Count; o++) { string btnName = btnNameList[o]; luaStr += (o == 0 ? "\tif" : "\n\telseif") + " go == " + btnName + " then\n"; } luaStr += "\n\tend\nend\n\n"; } luaStr += "function " + obj.name + ":Dispose()\n\tLuaToCSFunction.PoolDestroy(self.gameObject)\nend" + "\n\n"; luaStr += "return " + obj.name; if (File.Exists(path + "/" + obj.name + ".lua")) { File.Delete(path + "/" + obj.name + ".lua"); } File.WriteAllText(path + "/" + obj.name + ".lua", luaStr); Debug.Log("生成lua文件完成!"); } }
static void CreateLuaFiles() { //获取在Project视图中选择的所有游戏对象 Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); List <GameObject> prefabList = new List <GameObject>(); foreach (Object obj in SelectedAsset) { if (obj is GameObject) { prefabList.Add((GameObject)obj); } } for (int i = 0; i < prefabList.Count; i++) { GameObject obj = prefabList[i]; string sourcePath = AssetDatabase.GetAssetPath(obj); sourcePath = sourcePath.Replace("Assets/Prefabs/", ""); sourcePath = sourcePath.Replace("/" + obj.name + ".prefab", ""); string path = Application.dataPath.ToLower() + "/LuaFramework/Lua/UI/" + sourcePath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string luaStr = InitPreString(obj); List <LuaObj> luaObjList = GetLuaObjList(obj); Dictionary <string, List <string> > componentsDic = new Dictionary <string, List <string> >(); componentsDic["BoxCollider"] = new List <string>(); componentsDic["UILabel"] = new List <string>(); componentsDic["UISlider"] = new List <string>(); for (int j = 0; j < luaObjList.Count; j++) { LuaObj luaObj = luaObjList[j]; string name = GetGameObjectPath(luaObj.obj).Replace("/" + obj.name + "/", ""); if (name.EndsWith("-") || name.EndsWith("_")) { continue; } if (!name.Equals("/" + obj.name)) { if (luaObj.type == "BoxCollider") { luaStr += "\tself." + name.Replace("/", "_") + "_Obj" + GetEndComponentStr(luaObj.type, name); } else if (luaObj.type == "UISprite") { luaStr += "\tself." + name.Replace("/", "_") + "_Sprite" + GetEndComponentStr(luaObj.type, name); } else { luaStr += "\tself." + name.Replace("/", "_") + GetEndComponentStr(luaObj.type, name); } } if (luaObj.type == "BoxCollider") { if (name.Equals("/" + obj.name)) { componentsDic["BoxCollider"].Add("self.gameObject"); } else { componentsDic["BoxCollider"].Add("self." + name.Replace("/", "_") + "_Obj"); } } else if (luaObj.type == "UILabel") { componentsDic["UILabel"].Add("self." + name.Replace("/", "_")); } else if (componentsDic.ContainsKey(luaObj.type)) { componentsDic[luaObj.type].Add("self." + name.Replace("/", "_")); } } luaStr += "\n\tself:InitEvent()\n"; luaStr += "end" + "\n\n"; luaStr += "function " + obj.name + ":InitEvent()\n"; for (int m = 0; m < componentsDic["BoxCollider"].Count; m++) { string btnName = componentsDic["BoxCollider"][m]; luaStr += "\tself.luaBehaviour:AddClick(" + btnName + ", self, self.OnClickHandler)" + "\n"; } luaStr += (componentsDic["BoxCollider"].Count == 0 ? "\nend" : "" + "end") + "\n\n"; if (componentsDic["BoxCollider"].Count > 0) { luaStr += "function " + obj.name + ":OnClickHandler(go)\n"; for (int o = 0; o < componentsDic["BoxCollider"].Count; o++) { string btnName = componentsDic["BoxCollider"][o]; luaStr += (o == 0 ? "\tif" : "\n\telseif") + " go == " + btnName + " then\n"; } luaStr += "\n\tend\nend\n\n"; } luaStr += "function " + obj.name + ":UpdateView()\n"; if (componentsDic["UILabel"].Count > 0) { for (int o = 0; o < componentsDic["UILabel"].Count; o++) { string labelName = componentsDic["UILabel"][o]; luaStr += "\t" + labelName + ".text = tables.LanguageDic[\"Bow06\"]" + "\n"; } } if (componentsDic["UISlider"].Count > 0) { for (int sl = 0; sl < componentsDic["UISlider"].Count; sl++) { string sliderName = componentsDic["UISlider"][sl]; luaStr += "\t" + sliderName + ".value = 0" + "\n"; } } luaStr += "end\n\n"; luaStr += "function " + obj.name + ":RemoveEvent()\n"; for (int n = 0; n < componentsDic["BoxCollider"].Count; n++) { string btnName = componentsDic["BoxCollider"][n]; luaStr += "\tself.luaBehaviour:RemoveClick(" + btnName + ")" + "\n"; } luaStr += (componentsDic["BoxCollider"].Count == 0 ? "\nend" : "" + "end") + "\n\n"; luaStr += "function " + obj.name + ":OnCloseHandler()\n\tself:RemoveEvent()\n\tself:UnloadAssetBundle()\nend" + "\n\n"; luaStr += "function " + obj.name + ":OnDestroy()\n\tself:RemoveEvent()\n\tself.super.OnDestroy(self)\nend" + "\n\n"; luaStr += "return " + obj.name; if (File.Exists(path + "/" + obj.name + ".lua")) { File.Delete(path + "/" + obj.name + ".lua"); } File.WriteAllText(path + "/" + obj.name + ".lua", luaStr); //生成Ctrl CreateCtrlFile(path, obj.name, sourcePath); //生成Model CreateModelFile(path, obj.name); Debug.Log("生成lua文件完成!"); } }