欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity示例解析

 更新時(shí)間:2021年10月03日 11:24:56   作者:劉家坑  
為了滿足項(xiàng)目需要,需要實(shí)現(xiàn)一個(gè)c#代碼生成器,為此設(shè)計(jì)了一個(gè)語法模板適用于Unity的代碼生成器。本次使用了Python的Template模板,使用python開發(fā)

開發(fā)目標(biāo):實(shí)現(xiàn)小紅帽所掛腳本的自動(dòng)生成

下圖為生成的最終目標(biāo)

在這里插入圖片描述

本項(xiàng)目是從json中讀取角色場(chǎng)景等信息,因此為了更好地判斷所用屬性是否需要,設(shè)置為bool類型,F(xiàn)alse表示在c#代碼中注釋掉該類屬性,True代表使用該屬性(屬性暫時(shí)設(shè)置為)

    Timer = True # 計(jì)時(shí)器
    speed = False # 速度
    IsTrigger = True # 觸發(fā)器
    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()

所設(shè)置的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);}

    }
}

自動(dòng)生成的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);}
    }
}

仔細(xì)觀察生成的結(jié)果,代碼與目標(biāo)生成的代碼基本一致,(注釋暫時(shí)只能使用英文編輯。) 隨即把生成的代碼放在unity中,觀察運(yùn)行情況。

運(yùn)行前:

在這里插入圖片描述

運(yùn)行后:

在這里插入圖片描述 

可見,小紅帽的控制器實(shí)現(xiàn)基本無誤。 具體視頻已放在b站:

unity的2d的animation純代碼實(shí)現(xiàn),場(chǎng)景切換。

以上就是Python實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity示例解析的詳細(xì)內(nèi)容,更多關(guān)于Python實(shí)現(xiàn)C#代碼生成器應(yīng)用Unity的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python第三方庫face_recognition在windows上的安裝過程

    Python第三方庫face_recognition在windows上的安裝過程

    今天為大家介紹下face recognition在Windows系統(tǒng)上安裝與使用,但在Windows平臺(tái)上face recognition性能會(huì)有所下降
    2019-05-05
  • Tensorflow的DataSet的使用詳解

    Tensorflow的DataSet的使用詳解

    本文主要介紹了Tensorflow的DataSet的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具

    Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具

    其實(shí)使用pangu做文本格式標(biāo)準(zhǔn)化的業(yè)務(wù)代碼在之前就實(shí)現(xiàn)了,主要能夠?qū)⒅形奈谋疚臋n中的文字、標(biāo)點(diǎn)符號(hào)等進(jìn)行標(biāo)準(zhǔn)化。但是為了方便起來我們這里使用了Qt5將其做成了一個(gè)可以操作的頁面應(yīng)用,需要的可以了解一下
    2022-10-10
  • PyTorch平方根報(bào)錯(cuò)的處理方案

    PyTorch平方根報(bào)錯(cuò)的處理方案

    這篇文章主要介紹了PyTorch平方根報(bào)錯(cuò)的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 在python中調(diào)用C/C++的三種方法

    在python中調(diào)用C/C++的三種方法

    這篇文章主要給大家介紹了關(guān)于在python中調(diào)用C/C++的三種方法,Python可以通過調(diào)用C/C++接口來實(shí)現(xiàn)與C/C++語言的交互,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • Django CBV模型源碼運(yùn)行流程詳解

    Django CBV模型源碼運(yùn)行流程詳解

    這篇文章主要介紹了Django CBV模型源碼運(yùn)行流程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 如何在Python中捕獲finally語句中異常消息

    如何在Python中捕獲finally語句中異常消息

    正常情況下,finally語句不會(huì)捕獲異常,而是在異常處理完成后執(zhí)行,那么如何在Python中捕獲finally語句中異常消息呢,下面小編就來和大家詳細(xì)聊聊
    2024-02-02
  • Python升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法

    Python升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了因?yàn)镻ython升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法,文中通過示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。
    2017-09-09
  • eclipse創(chuàng)建python項(xiàng)目步驟詳解

    eclipse創(chuàng)建python項(xiàng)目步驟詳解

    在本篇內(nèi)容里小編給大家分享了關(guān)于eclipse創(chuàng)建python項(xiàng)目的具體步驟和方法,需要的朋友們跟著學(xué)習(xí)下。
    2019-05-05
  • python數(shù)據(jù)可視化之日期折線圖畫法

    python數(shù)據(jù)可視化之日期折線圖畫法

    這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)可視化之日期折線圖畫法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評(píng)論