Python实现C#代码生成器应用服务于Unity示例解析
作者:刘家坑 发布时间:2023-01-18 00:33:44
标签:Python,C#,Unity
开发目标:实现小红帽所挂脚本的自动生成
下图为生成的最终目标
本项目是从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纯代码实现,场景切换。
来源:https://blog.csdn.net/weixin_43795683/article/details/109545051
0
投稿
猜你喜欢
- 那么,现在如果给出一个权限编号,要去检索出用后这个权限的用户集合,就会需要在逗号分隔的多个权限编号中去匹配给出的这个权限编号。如果使用lik
- 异常的传递性在 Python 中,异常的传递性指的是,当一个异常没有被处理时,它会沿着调用栈向上抛出,直到被处理或者导致程序崩溃。具体来说,
- 界面的制作一直是 Python 的痛!使用 Python 制作桌面端界面是非常痛苦的过程(又难学又难看)。不过,Python 已经出现了几个
- 监测主机存活的端口#!/usr/bin/env python# coding-utfimport argparseimport socket
- asp抓取网页。偶要实现实实更新天气预报。利用了XMLHTTP组件,抓取网页的指定部分。很多小偷查询都是使用这个方法来实现的。需要分件htm
- 有件东西我观察了很多年,那就是很少有开发者会去使用SQL Server中的一个非常有用的东西——EX
- 今天遇到一个需要用javascript将url中的某些参数替换的需求,想起了不久前从网上淘到了一个parseUrl函数,正好可以借此实现,代
- 本文的JS效果是在鼠标点击ITEM标签的时候,实现下方的内容跟随滚动切换的效果,我们先来看下运行后的效果图。以下是脚本之家原创的运行代码:&
- 这是写给web设计者和前端开发者的教程,我们将演示如何使用Photoshop创建按钮的sprite图,然后是如何使用jQurey打造动态渐变
- 平时在写asp代码的时候有很多重复的内容要写,麻烦的要命,比如在收集表单提交的数据时,特别是表单的输入域比较多时,要不断写好多的reques
- prototype框架最早是出于方便Ruby开发人员进行JavaScript开发所构建的,从这个版本上更加体现的淋漓尽致。比起1.3.1版本
- 本文介绍基于Python中gdal模块,实现对大量栅格图像批量绘制直方图的方法。首先,明确一下本文需要实现的需求:现需对多幅栅格数据文件进行
- 一个动态载入asp树源码。把 node.htc, style.css 保存与 css 目录下. index.asp subtree.asp
- 一、urllib 和 urllib2的关系在python2中,主要使用urllib和urllib2,而python3对urllib和urll
- 桑基图,它的核心是对不同点之间,通过线来连接。线的粗细代表流量的大小。很多工具都能实现桑基图,比如:Excel、tableau,我们今天要用
- 但是有时候,可以视看处进逻辑程度,可以把三者写成一个触发器,只是在其中稍作判断而已。 你可以根据从下面方法判断触发器是是处理了插入,删除还是
- isNaN函数 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字)。 NaN 即 Not a Number isN
- 在使用django-rest-framework开发项目的时候我们总是避免不了跨域的问题,因为现在大多数的项目都是前后端分离,前后端项目部署
- 1.简介celery(芹菜)是一个异步任务队列/基于分布式消息传递的作业队列。它侧重于实时操作,但对调度支持也很好。celery用于生产系统
- 这段时间我一直在说设计需要有层次感,这种层次感可能有很多类型,比如色彩的层次感,或是元素的层次感。当一个设计缺乏层次感的时候页面所表现出来的