溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-10-08 09:42:03 來源:億速云 閱讀:192 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Python如何實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

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

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

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

本項(xiàng)目是從json中讀取角色場景等信息,因此為了更好地判斷所用屬性是否需要,設(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);}

    }
}

自動生成的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)行前:

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

運(yùn)行后:

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

關(guān)于“Python如何實(shí)現(xiàn)C#代碼生成器應(yīng)用服務(wù)于Unity”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI