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

运行前:

Python实现C#代码生成器应用服务于Unity示例解析

运行后:

Python实现C#代码生成器应用服务于Unity示例解析 

可见,小红帽的控制器实现基本无误。 具体视频已放在b站:

unity的2d的animation纯代码实现,场景切换。

来源:https://blog.csdn.net/weixin_43795683/article/details/109545051

标签:Python,C#,Unity
0
投稿

猜你喜欢

  • IE6 iframe 横向滚动条问题

    2009-01-18 13:31:00
  • python中的psutil模块详解(cpu、内存、磁盘情况、结束指定进程)

    2021-12-19 23:33:04
  • 如何拒绝同一张表单被多次提交?

    2009-12-16 18:46:00
  • 关于django python manage.py startapp 应用名出错异常原因解析

    2023-04-04 08:57:28
  • defineProperty和Proxy基础功能及性能对比

    2024-06-05 09:19:42
  • Python使用Pillow添加水印

    2021-01-28 08:30:50
  • 利用Python实现RSA加密解密方法实例

    2023-10-07 03:10:58
  • python定时任务sched库用法简单实例

    2023-11-07 07:26:14
  • ASP名次排列函数

    2008-07-20 13:42:00
  • 使用xtrabackup实现mysql备份

    2024-01-21 07:57:57
  • JavaScript语法约定和程序调试原理解析

    2024-04-18 09:46:22
  • Python设计模式中的结构型适配器模式

    2023-07-19 11:59:54
  • MySQL 四种连接和多表查询详解

    2024-01-14 17:34:29
  • pandas数据预处理之dataframe的groupby操作方法

    2022-07-07 19:27:56
  • tensorflow之获取tensor的shape作为max_pool的ksize实例

    2022-10-09 01:41:53
  • python如何将一个四位数反向输出

    2023-03-21 16:42:40
  • python3去掉string中的标点符号方法

    2021-06-27 01:29:45
  • 一文带你吃透什么是PHP中的序列化

    2023-06-12 19:44:20
  • 如何在数据库中限制检索行数?

    2010-06-22 21:04:00
  • Python中的元组介绍

    2021-04-09 19:56:37
  • asp之家 网络编程 m.aspxhome.com