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


猜你喜欢
- tensorflow在1.4版本引入了keras,封装成库。现想将keras版本的GRU代码移植到TensorFlow中,看到TensorF
- 如下所示:alist=[1,2]] >>>[1,2] alist.append([3,4]) >>>[1
- 项目介绍:Golang100行代码实现高并发聊天室,其中实现的功能有:上下线广播,私聊,用户改名,超时强踢,在线用户检测等在开始项目前,我们
- mysql数据库没有增量备份的机制,当数据量太大的时候备份是一个很大的问题。还好mysql数据库提供了一种主从备份的机制,其实就是把主数据库
- 这篇文章主要介绍了python解析命令行参数的三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 今天我和中国著名画家"渔人"谈了一个关于"怎样才能设计好"的问题,他给我说了一句话,得益不浅,那句话
- 需求是根据当前登录用户来显示某个choice字段不同的选择项。先放现在的实现版本。1、重写PushRuleForm的__init__方法,让
- 近期,一直在研究MySQL数据库,经常修改配置文件,导致MySQL数据库无法使用,不得不反复重装MySQL数据库。以下是在Windows7
- Python的命名空间是Python程序猿必须了解的内容,对Python命名空间的学习,将使我们在本质上掌握一些Python中的琐碎的规则。
- 微信小程序request请求后台接口php的实例详解后台php接口:http://www.vueyun.com/good/info没有处理数
- Python 操作文件编程语言对文件系统的操作是一项必不可少的功能,各种编程语言基本上都有对文件系统的操作,最简洁的莫过于linux里面sh
- 序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。Pyt
- 本文实例讲述了Python实现列表删除重复元素的三种常用方法。分享给大家供大家参考,具体如下:给定一个列表,要求删除列表中重复元素。list
- 一、 存储过程的概念,优点,语法 在写笔记之前,首先需要整理好这些概念性的东西,否则的话,就会在概念上产生陌生或者是混淆的感觉。 概念:将常
- 毫无疑问,Google是当今世界上最成功的互联网公司之一,但是Google也曾推出过一些失败的实验品。还记得Google Accelerat
- 本文实例讲述了Python实现替换文件中指定内容的方法。分享给大家供大家参考,具体如下:这里使用python编写的程序,实现如下功能:将文件
- 1.1.propety动态属性在面向对象编程中,我们一般把名词性的东西映射成属性,动词性的东西映射成方法。在python中他们对应的分别是属
- ASP.NET利用它可以实现在线备份、还原数据库等各种功能。由于客户的数据库和WEB服务不再同一台服务器,把网站部署在服务器上以后,运行程序
- 一、前言在近半年的 Python 命令行旅程中,我们依次学习了 argparse 、 docopt 、 click 和 fire 库的特点和
- 一、安装配置PHP1、下载Php的版本zip包之后,解压缩到指定目录。下载地址:http://www.php.net/downl