Python实现C#代码生成器应用服务于Unity示例解析
程序员文章站
2022-03-25 13:48:18
目录所设置的tmpl文件如下:开发目标:实现小红帽所挂脚本的自动生成下图为生成的最终目标本项目是从json中读取角色场景等信息,因此为了更好地判断所用属性是否需要,设置为bool类型,false表示在...
开发目标:实现小红帽所挂脚本的自动生成
下图为生成的最终目标
本项目是从json中读取角色场景等信息,因此为了更好地判断所用属性是否需要,设置为bool类型,false表示在c#代码中注释掉该类属性,true代表使用该属性(属性暂时设置为)
timer = true # 计时器 speed = false # 速度 istrigger = true # 触发器 start_point = true # 起始位置 localscale = true # 起始大小
主程序具体python代码如下:
from string import template class builddata: def init(self): # 初始化各类$ timer = true speed = false istrigger = true start_point = true localscale = true # 输出a.cs文件 filepath = 'a.cs' class_file = open(filepath, 'w') # mycode用来存放生成的代码 mycode = [] # 加载模板文件 template_file = open('tmpl1.tmpl', 'rb') template_file = template_file.read().decode('utf-8') tmpl = template(template_file) ## 模板替换 # 1.需要判断是否使用的模板,不使用的给他注释掉 if(timer): timercontent = ' ' else: timercontent = '///' if (speed): speedcontent = ' ' else: speedcontent = '///' if (istrigger): istriggercontent =' ' else: istriggercontent ='///' if (start_point): start_pointcontent= ' ' else: start_pointcontent= '///' if (localscale): localscalecontent = ' ' else: localscalecontent='///' # 2.固定的模板值更替 mycode.append(tmpl.safe_substitute( timercontent=timercontent, speedcontent=speedcontent, istriggercontent=istriggercontent, start_pointcontent=start_pointcontent, localscalecontent=localscalecontent, role='small_red_hat', x_start_point='12', y_start_point='-2', z_start_point='0', x_scale='0.45f', y_scale='0.5f', z_scale='1' )) # 将代码写入文件 class_file.writelines(mycode) class_file.close() print('代码已生成') if __name__ == '__main__': build = builddata() build.init()
所设置的tmpl文件如下:
using system.collections; using system.collections.generic; using unityengine; public class ${role} : monobehaviour { ${timercontent} public float timer; //set a timer ${speedcontent} public float speed; //speed ${istriggercontent} public bool istrigger; //set a trigger void start() { //the start_point of ${role} ${start_pointcontent}transform.position = new vector3(${x_start_point}, ${y_start_point}, ${z_start_point}); //the scale of ${role} ${localscalecontent}transform.localscale = new vector3(${x_scale},${y_scale}, ${z_scale}); } void update() { //timer countdown ${timercontent} timer += time.deltatime; //when to move ${timercontent} if (timer >= 2f && timer <= 4f) { istrigger = true;} //when to stop ${timercontent} else if (timer > 3.5f){ istrigger = false;} //the speed of ${role} ${istriggercontent}if(istrigger){ transform.translate(-0.04f, 0, 0);} } }
自动生成的c#代码展示如下:
using system.collections; using system.collections.generic; using unityengine; public class small_red_hat : monobehaviour { public float timer; //set a timer /// public float speed; //speed public bool istrigger; //set a trigger void start() { //the start_point of small_red_hat transform.position = new vector3(12, -2, 0); //the scale of small_red_hat transform.localscale = new vector3(0.45f,0.5f, 1); } void update() { //timer countdown timer += time.deltatime; //when to move if (timer >= 2f && timer <= 4f) { istrigger = true;} //when to stop else if (timer > 3.5f){ istrigger = false;} //the speed of small_red_hat if (istrigger){ transform.translate(-0.04f, 0, 0);} } }
仔细观察生成的结果,代码与目标生成的代码基本一致,(注释暂时只能使用英文编辑。) 随即把生成的代码放在unity中,观察运行情况。
运行前:
运行后:
可见,小红帽的控制器实现基本无误。 具体视频已放在b站:
unity的2d的animation纯代码实现,场景切换。
以上就是python实现c#代码生成器应用服务于unity示例解析的详细内容,更多关于python实现c#代码生成器应用unity的资料请关注其它相关文章!