千家信息网

Python如何实现C#代码生成器应用服务于Unity

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章将为大家详细讲解有关Python如何实现C#代码生成器应用服务于Unity,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。开发目标:实现小红帽所挂脚本的自动生
千家信息网最后更新 2025年11月07日Python如何实现C#代码生成器应用服务于Unity

这篇文章将为大家详细讲解有关Python如何实现C#代码生成器应用服务于Unity,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

开发目标:实现小红帽所挂脚本的自动生成

下图为生成的最终目标

本项目是从json中读取角色场景等信息,因此为了更好地判断所用属性是否需要,设置为bool类型,False表示在c#代码中注释掉该类属性,True代表使用该属性(属性暂时设置为)

Timer = True # 计时器    speed = False # 速度    IsTrigger = True # 触发器    start_point = True # 起始位置    localScale = True # 起始大小

主程序具体python代码如下:

from string import Templateclass 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中,观察运行情况。

运行前:

运行后:

关于"Python如何实现C#代码生成器应用服务于Unity"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

代码 生成 属性 文件 模板 注释 目标 篇文章 运行 应用服务 生成器 C# 应用 服务 更多 自动生成 c# 观察 起始 不错 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 booking机票数据库 电脑未连接上服务器怎么解决 网络安全法应当充分运用 数据库技术中什么是集合操作 科技互联网电商大会 阿里云服务器 vnc 公主岭云服务器试用价格 大数据网络安全感受 二道区网络技术咨询诚信服务 视频服务器同步播放 深圳市臻航网络技术有限公司 战意怎么看自己玩的服务器 网络技术还有应用题吗 硬件开发与软件开发的区别 数据库还原时 山东网络技术转让厂家批发价 方舟服务器到期后建筑消失吗 济宁ios软件开发外包公司 四川python软件开发价钱 数据库可以直接备份数据目录吗 263 imap服务器 网络安全动态防御能力指标 规模大的pc软件开发电话 实施工程师转数据库工程师 oracle数据库半数配置 济南网络安全培训机构学费 数据库服务器的ip 前端部署服务器的哪个文件 数据库可以直接备份数据目录吗 软件开发与源代码
0