日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

unity可以用python编写吗_基于python的Cා代码生成器(用于服务并应用于unity),一个,c,为了,并且,Unity...

發布時間:2025/3/15 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity可以用python编写吗_基于python的Cා代码生成器(用于服务并应用于unity),一个,c,为了,并且,Unity... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為了滿足項目需要,本人需要實現一個c#代碼生成器(使用python開發),為此設計了一個語法模板適用于Unity的代碼生成器。本次使用了Python的Template模板,使用python開發。

## 開發目標:實現小紅帽所掛腳本的自動生成,下圖為生成的最終目標

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

(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

(IsTrigger){ transform.Translate(-0.04f, 0, 0);}

}

}

仔細觀察生成的結果,代碼與目標生成的代碼基本一致,(注釋暫時只能使用英文編輯。)

隨即把生成的代碼放在unity中,觀察運行情況。

分------------------------------------------------------------------------界 ------------------------------------------------------------------------------ 線

運行前:

運行后:

可見,小紅帽的控制器實現基本無誤。

具體視頻已放在b站:(記得一鍵三連(逃~))

unity的2d的animation純代碼實現,場景切換。

一分一毛也是情,支持一下行不行~~~~

總結

以上是生活随笔為你收集整理的unity可以用python编写吗_基于python的Cා代码生成器(用于服务并应用于unity),一个,c,为了,并且,Unity...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。