自动创建预设脚本

自动创建预设脚本

再日常游戏开发过程中,有些模块的文件夹,以及脚本都是固定格式的。

为了方便,可以写一个编辑器工具,根据模块名字,自动创建文件夹和所必须的脚本。

根据 demo脚本 自动创建生成脚本

	// 核心逻辑,读取demo脚本,修改脚本名字 
	void CreateLuaFiles() {
        string pathFile = demoPath + "/" + keys.Current.Value + ".lua";
        string savePath = rootPath + "/" + moduleName + "/" + savePathInfo[keys.Current.Key] + ".lua";
        savePath = string.Format(savePath, moduleName, otherName);

        FileStream fileStream = File.OpenRead(pathFile);
        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);
        fileStream.Close();
        string content = System.Text.Encoding.UTF8.GetString(bytes);
        content = ReplaceInfo(content); // 替换
        bytes = System.Text.Encoding.UTF8.GetBytes(content);

        if (File.Exists(savePath)) {
            File.Delete(savePath);
        }
        File.WriteAllBytes(savePath, bytes);
    }

    string ReplaceInfo(string info) {
        while (info.IndexOf("{0}") != -1) {
            info = info.Replace("{0}", scriptName);
        }
        while (info.IndexOf("{1}") != -1)
        {
            info = info.Replace("{1}", otherName);
        }
        return info;
    }

Demo 脚本

module ('m__' .. ..., package.seeall)

local {
    
    0} = newClass("{0}", LuaWindowModule)

function {
    
    0}:ModuleAwake( ... )
    self:SuperCall({
    
    0}, "ModuleAwake");
    self:ConstructMember()
	self:InitMember()
end

完整代码可以关注私信我哦~,欢迎交流学习。

猜你喜欢

转载自blog.csdn.net/wang_lvril/article/details/126425907