最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 內(nèi)置庫wsgiref的使用(WSGI基礎(chǔ)入門)

    python 內(nèi)置庫wsgiref的使用(WSGI基礎(chǔ)入門)

    WSGI(web服務(wù)器網(wǎng)關(guān)接口)主要規(guī)定了服務(wù)器端和應(yīng)用程序之間的接口,即規(guī)定了請(qǐng)求的URL到后臺(tái)處理函數(shù)之間的映射該如何實(shí)現(xiàn)。wsgiref是一個(gè)幫助開發(fā)者開發(fā)測(cè)試的Python內(nèi)置庫,程序員可以通過這個(gè)庫了解WSGI的基本運(yùn)行原理,但是不能把它用在生產(chǎn)環(huán)境上。
    2021-06-06
  • 對(duì)DataFrame數(shù)據(jù)中的重復(fù)行,利用groupby累加合并的方法詳解

    對(duì)DataFrame數(shù)據(jù)中的重復(fù)行,利用groupby累加合并的方法詳解

    今天小編就為大家分享一篇對(duì)DataFrame數(shù)據(jù)中的重復(fù)行,利用groupby累加合并的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 跨平臺(tái)python異步回調(diào)機(jī)制實(shí)現(xiàn)和使用方法

    跨平臺(tái)python異步回調(diào)機(jī)制實(shí)現(xiàn)和使用方法

    這篇文章主要介紹了python異步回調(diào)機(jī)制的實(shí)現(xiàn)方法,提供了使用方法代碼
    2013-11-11
  • 通過conda把已有虛擬環(huán)境的python版本進(jìn)行降級(jí)操作指南

    通過conda把已有虛擬環(huán)境的python版本進(jìn)行降級(jí)操作指南

    當(dāng)使用conda創(chuàng)建虛擬環(huán)境時(shí),有時(shí)候可能會(huì)遇到python版本不對(duì)的問題,下面這篇文章主要給大家介紹了關(guān)于如何通過conda把已有虛擬環(huán)境的python版本進(jìn)行降級(jí)操作的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • Centos 升級(jí)到python3后pip 無法使用的解決方法

    Centos 升級(jí)到python3后pip 無法使用的解決方法

    今天小編就為大家分享一篇Centos 升級(jí)到python3后pip 無法使用的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python實(shí)現(xiàn)簡(jiǎn)單文本字符串處理的方法

    Python實(shí)現(xiàn)簡(jiǎn)單文本字符串處理的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單文本字符串處理的方法,涉及Python針對(duì)文本字符串的切割、計(jì)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Python 自動(dòng)補(bǔ)全(vim)

    Python 自動(dòng)補(bǔ)全(vim)

    Python自動(dòng)補(bǔ)全有vim編輯下和python交互模式下,下面分別介紹如何在這2種情況下實(shí)現(xiàn)Tab鍵自動(dòng)補(bǔ)全
    2014-11-11
  • Python內(nèi)建類型int源碼學(xué)習(xí)

    Python內(nèi)建類型int源碼學(xué)習(xí)

    這篇文章主要為大家介紹了Python內(nèi)建類型int源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 使用python更改Word文檔字體的操作代碼

    使用python更改Word文檔字體的操作代碼

    更改文字字體是編輯和美化Word文檔時(shí)的一項(xiàng)常見需求,使用合適的字體不僅可以提升文檔的整體視覺效果,還能突顯關(guān)鍵信息,本文將介紹如何通過Python代碼更改Word文檔字體,實(shí)現(xiàn)批量操作與自動(dòng)化,需要的朋友可以參考下
    2024-08-08
  • OpenCV霍夫圓變換cv2.HoughCircles()

    OpenCV霍夫圓變換cv2.HoughCircles()

    這篇博客將學(xué)習(xí)如何使用霍夫圓變換在圖像中找到圓圈,OpenCV使用cv2.HoughCircles()實(shí)現(xiàn)霍夫圓變換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論

东光县| 乐清市| 肥乡县| 明溪县| 美姑县| 山西省| 漯河市| 曲沃县| 孝昌县| 新余市| 昆明市| 贺兰县| 德州市| 青海省| 宁国市| 卫辉市| 和政县| 弥渡县| 丹寨县| 榆中县| 斗六市| 红桥区| 吴旗县| 松桃| 瓦房店市| 纳雍县| 盐津县| 东乡族自治县| 德化县| 凌源市| 苗栗市| 漾濞| 永川市| 岳池县| 彰化县| 揭东县| 廊坊市| 璧山县| 镇沅| 武穴市| 榆林市|