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

 更新时间:2021年10月03日 11:24:56   作者:刘家坑  
为了满足项目需要,需要实现一个c#代码生成器,为此设计了一个语法模板适用于Unity的代码生成器。本次使用了Python的Template模板,使用python开发

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

下图为生成的最终目标

在这里插入图片描述

本项目是从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的资料请关注脚本之家其它相关文章!

相关文章

  • python 内置库wsgiref的使用(WSGI基础入门)

    python 内置库wsgiref的使用(WSGI基础入门)

    WSGI(web服务器网关接口)主要规定了服务器端和应用程序之间的接口,即规定了请求的URL到后台处理函数之间的映射该如何实现。wsgiref是一个帮助开发者开发测试的Python内置库,程序员可以通过这个库了解WSGI的基本运行原理,但是不能把它用在生产环境上。
    2021-06-06
  • 对DataFrame数据中的重复行,利用groupby累加合并的方法详解

    对DataFrame数据中的重复行,利用groupby累加合并的方法详解

    今天小编就为大家分享一篇对DataFrame数据中的重复行,利用groupby累加合并的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 跨平台python异步回调机制实现和使用方法

    跨平台python异步回调机制实现和使用方法

    这篇文章主要介绍了python异步回调机制的实现方法,提供了使用方法代码
    2013-11-11
  • 通过conda把已有虚拟环境的python版本进行降级操作指南

    通过conda把已有虚拟环境的python版本进行降级操作指南

    当使用conda创建虚拟环境时,有时候可能会遇到python版本不对的问题,下面这篇文章主要给大家介绍了关于如何通过conda把已有虚拟环境的python版本进行降级操作的相关资料,需要的朋友可以参考下
    2024-05-05
  • Centos 升级到python3后pip 无法使用的解决方法

    Centos 升级到python3后pip 无法使用的解决方法

    今天小编就为大家分享一篇Centos 升级到python3后pip 无法使用的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • Python实现简单文本字符串处理的方法

    Python实现简单文本字符串处理的方法

    这篇文章主要介绍了Python实现简单文本字符串处理的方法,涉及Python针对文本字符串的切割、计算、转换等相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • Python 自动补全(vim)

    Python 自动补全(vim)

    Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全
    2014-11-11
  • Python内建类型int源码学习

    Python内建类型int源码学习

    这篇文章主要为大家介绍了Python内建类型int源码学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • 使用python更改Word文档字体的操作代码

    使用python更改Word文档字体的操作代码

    更改文字字体是编辑和美化Word文档时的一项常见需求,使用合适的字体不仅可以提升文档的整体视觉效果,还能突显关键信息,本文将介绍如何通过Python代码更改Word文档字体,实现批量操作与自动化,需要的朋友可以参考下
    2024-08-08
  • OpenCV霍夫圆变换cv2.HoughCircles()

    OpenCV霍夫圆变换cv2.HoughCircles()

    这篇博客将学习如何使用霍夫圆变换在图像中找到圆圈,OpenCV使用cv2.HoughCircles()实现霍夫圆变换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07

最新评论